summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/resources/resourcemanager.cpp26
-rw-r--r--src/resources/resourcemanager.h11
-rw-r--r--src/sound.cpp14
3 files changed, 50 insertions, 1 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 33d5e3e5..e24ebafb 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -419,6 +419,32 @@ void *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
return buffer;
}
+bool ResourceManager::moveFile(const std::string &src, const std::string &dst)
+{
+ PHYSFS_file *srcFile = PHYSFS_openRead(src.c_str());
+ if (!srcFile)
+ {
+ logger->log("Read error: %s", PHYSFS_getLastError());
+ return false;
+ }
+ PHYSFS_file *dstFile = PHYSFS_openWrite(dst.c_str());
+ if (!dstFile)
+ {
+ logger->log("Write error: %s", PHYSFS_getLastError());
+ PHYSFS_close(srcFile);
+ return false;
+ }
+
+ int fileSize = PHYSFS_fileLength(srcFile);
+ void *buf = malloc(fileSize);
+ PHYSFS_read(srcFile, buf, 1, fileSize);
+ PHYSFS_write(dstFile, buf, 1, fileSize);
+
+ PHYSFS_close(srcFile);
+ PHYSFS_close(dstFile);
+ return true;
+}
+
std::vector<std::string> ResourceManager::loadTextFile(const std::string &fileName)
{
int contentsLength;
diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h
index ec60fa9a..19c0cb61 100644
--- a/src/resources/resourcemanager.h
+++ b/src/resources/resourcemanager.h
@@ -125,6 +125,17 @@ class ResourceManager
Resource *load(const std::string &path, loader fun);
/**
+ * Copies a file from one place to another (useful for extracting
+ * raw files from a zip archive, for example)
+ *
+ * @param src Source file name
+ * @param dst Destination file name
+ * @return true on success, false on failure. An error message should be
+ * in the log file.
+ */
+ bool moveFile(const std::string &src, const std::string &dst);
+
+ /**
* Convenience wrapper around ResourceManager::get for loading
* images.
*/
diff --git a/src/sound.cpp b/src/sound.cpp
index 3686d009..39651045 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -20,6 +20,7 @@
*/
#include <SDL.h>
+#include <physfs.h>
#include "log.h"
#include "sound.h"
@@ -141,7 +142,18 @@ static Mix_Music *loadMusic(const std::string &filename)
ResourceManager *resman = ResourceManager::getInstance();
std::string path = resman->getPath("music/" + filename);
- logger->log("Loading music \"%s\"", path.c_str());
+ if (path.find(".zip/") != std::string::npos ||
+ path.find(".zip\\") != std::string::npos)
+ {
+ // Music file is a virtual file inside a zip archive - we have to copy
+ // it to a temporary physical file so that SDL_mixer can stream it.
+ logger->log("Loading music \"%s\" from temporary file tempMusic.ogg",
+ path.c_str());
+ resman->moveFile("music/" + filename, "tempMusic.ogg");
+ path = resman->getPath("tempMusic.ogg");
+ } else {
+ logger->log("Loading music \"%s\"", path.c_str());
+ }
Mix_Music *music = Mix_LoadMUS(path.c_str());