summaryrefslogtreecommitdiff
path: root/src/resources/resourcemanager.cpp
diff options
context:
space:
mode:
authorEric Scrivner <zenogais@gmail.com>2005-03-12 18:06:53 +0000
committerEric Scrivner <zenogais@gmail.com>2005-03-12 18:06:53 +0000
commitd0c5ab3fe88e1248f755a764248037960f7bed35 (patch)
tree7e43cfe622c20b704e7bccfd88e26aa3a79bb826 /src/resources/resourcemanager.cpp
parent3a0e16802b9838ad77d96276bfaa213d8285b76e (diff)
downloadmana-d0c5ab3fe88e1248f755a764248037960f7bed35.tar.gz
mana-d0c5ab3fe88e1248f755a764248037960f7bed35.tar.bz2
mana-d0c5ab3fe88e1248f755a764248037960f7bed35.tar.xz
mana-d0c5ab3fe88e1248f755a764248037960f7bed35.zip
Made PhysFS code a bit more generic for later additions.
Diffstat (limited to 'src/resources/resourcemanager.cpp')
-rw-r--r--src/resources/resourcemanager.cpp59
1 files changed, 41 insertions, 18 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index e40aa723..12123c0a 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -109,24 +109,17 @@ Resource* ResourceManager::get(const E_RESOURCE_TYPE &type,
// If the object is invalid, try using PhysicsFS
if (resource == NULL) {
std::cout << "Check if exists: " << filePath << std::endl;
- // If the file is in the PhysicsFS search path
- if (PHYSFS_exists(filePath.c_str()) != 0) {
- // Open the file for read
- PHYSFS_file* imageFile = PHYSFS_openRead(filePath.c_str());
- std::cout << filePath << " - " <<
- PHYSFS_fileLength(imageFile) << " bytes" << std::endl;
-
- // Read the file data into a buffer
- char* buffer = new char[PHYSFS_fileLength(imageFile)];
- PHYSFS_read(imageFile, buffer, 1,
- PHYSFS_fileLength(imageFile));
-
- // Cast the file to a valid resource
- resource = reinterpret_cast<Resource*>(Image::load(buffer,
- PHYSFS_fileLength(imageFile)));
-
- // Close the file
- PHYSFS_close(imageFile);
+
+ // Load the image resource file
+ void* buffer = NULL;
+ int fileSize = loadFile(filePath, buffer);
+
+ // Let the image class load it
+ resource = reinterpret_cast<Resource*>(Image::load(buffer,
+ fileSize));
+
+ // Cleanup
+ if(buffer != NULL) {
delete[] buffer;
}
}
@@ -244,3 +237,33 @@ void ResourceManager::searchAndAddZipFiles()
closedir(dir);
#endif
}
+
+int ResourceManager::loadFile(const std::string& fileName, void* buffer)
+{
+ // If the file doesn't exist indicate failure
+ if (PHYSFS_exists(fileName.c_str()) != 0) return -1;
+
+ // Initialize the buffer value
+ buffer = NULL;
+
+ // Attempt to open the specified file using PhysicsFS
+ PHYSFS_file* file = PHYSFS_openRead(fileName.c_str());
+
+ // If the handler is an invalid pointer indicate failure
+ if (file == NULL) return -1;
+
+ // Print file information message
+ int fileLength = PHYSFS_fileLength(file);
+
+ std::cout << fileName << " - "
+ << fileLength << " bytes" << std::endl;
+
+ // Allocate memory in the buffer and load the file
+ buffer = (void*)new char[fileLength];
+ PHYSFS_read(file, buffer, 1, fileLength);
+
+ // Close the file and let the user deallocate the memory (safe?)
+ PHYSFS_close(file);
+
+ return fileLength;
+}