From b40bfe580d2dc9038c00521a44fcd6edd352f239 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sat, 26 Mar 2005 01:54:25 +0000 Subject: Images are now exclusively loaded through PhysFS, and Tiled maps should load fine again. --- src/resources/image.cpp | 36 ++++---------------- src/resources/image.h | 18 +++------- src/resources/mapreader.cpp | 8 +++-- src/resources/resourcemanager.cpp | 69 ++++++++++++++++++--------------------- src/resources/resourcemanager.h | 4 +-- 5 files changed, 49 insertions(+), 86 deletions(-) (limited to 'src/resources') diff --git a/src/resources/image.cpp b/src/resources/image.cpp index a8af4e13..5114d1d3 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -50,18 +50,16 @@ Image::~Image() unload(); } -Image* Image::load(const std::string &filePath, int flags) +Image* Image::load(void* buffer, unsigned int bufferSize, int flags) { - logger.log("Image::load(%s)", filePath.c_str()); + // Load the raw file data from the buffer in an RWops structure + SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - // Attempt to use SDL_Image to load the file. - SDL_Surface *tmpImage = IMG_Load(filePath.c_str()); + // Use SDL_Image to load the raw image data + SDL_Surface* tmpImage = IMG_Load_RW(rw, 1); - // Check if the file was opened and return the appropriate value. - if (!tmpImage) { - logger.log("Error: Image load failed."); - return NULL; - } + // Now free the SDL_RWops data + //SDL_FreeRW(rw); #ifndef USE_OPENGL @@ -196,26 +194,6 @@ Image* Image::load(const std::string &filePath, int flags) } return new Image(texture, width, height, realWidth, realHeight); - -#endif -} - -Image* Image::load(void* buffer, unsigned int bufferSize) -{ - // Load the raw file data from the buffer in an RWops structure - SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); - - // Use SDL_Image to load the raw image data - SDL_Surface* texture = IMG_Load_RW(rw, 1); - - // Now free the SDL_RWops data - //SDL_FreeRW(rw); - -#ifndef USE_OPENGL - return new Image(texture); -#else - return new Image(0, 0, 0, 0, 0); - // Warning: need implementation to use with OpenGL #endif } diff --git a/src/resources/image.h b/src/resources/image.h index a257678b..bee60aab 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -49,29 +49,19 @@ class Image : public Resource */ virtual ~Image(); - /** - * Loads an image. - * - * @param filePath The path to the image file to load. - * @param flags These flags allow controlling the way the image is - * loaded. Currently only IMG_ALPHA is supported, which - * causes alpha layer to be preserved. - * - * @return NULL if the an error occurred, a - * valid pointer otherwise. - */ - static Image *load(const std::string &filePath, int flags); - /** * Loads an image from a buffer in memory. * * @param buffer The memory buffer containing the image data. * @param bufferSize The size of the memory buffer in bytes. + * @param flags These flags allow controlling the way the image is + * loaded. Currently only IMG_ALPHA is supported, which + * causes alpha layer to be preserved. * * @return NULL if the an error occurred, a valid pointer * otherwise. */ - static Image *load(void* buffer, unsigned int bufferSize); + static Image *load(void* buffer, unsigned int bufferSize, int flags); /** * Creates a new empty image with given height and width. diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 31048207..c656edba 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -236,9 +236,11 @@ Tileset* MapReader::readTileset( if (source) { + std::string sourceStr = std::string((const char*)source); + sourceStr.erase(0, 3); // Remove "../" + ResourceManager *resman = ResourceManager::getInstance(); - Image* tilebmp = resman->getImage(path + - std::string((const char*)source)); + Image* tilebmp = resman->getImage(sourceStr); if (tilebmp) { @@ -247,7 +249,7 @@ Tileset* MapReader::readTileset( return set; } else { - logger.log("Warning: Failed to load tileset (%s)\n", source); + logger.log("Warning: Failed to load tileset (%s)", source); } } diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 3733f128..cc5c9562 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -68,8 +68,8 @@ ResourceManager::~ResourceManager() } resources.clear(); - logger.log("ResourceManager::~ResourceManager() cleaned up %d references to %d" - " resources", danglingReferences, danglingResources); + logger.log("ResourceManager::~ResourceManager() cleaned up %d references " + "to %d resources", danglingReferences, danglingResources); } Resource* ResourceManager::get(const E_RESOURCE_TYPE &type, @@ -84,12 +84,7 @@ Resource* ResourceManager::get(const E_RESOURCE_TYPE &type, return resIter->second.resource; } - // The filePath string. - std::string filePath = ""; - - // Set the filePath variable to the appropriate value - // this is only if we're not using a packed file. - filePath = std::string("data/") + idPath; + logger.log("ResourceManager::get(%s)", idPath.c_str()); Resource *resource = NULL; @@ -103,26 +98,24 @@ Resource* ResourceManager::get(const E_RESOURCE_TYPE &type, logger.log("Warning: Music resource not supported."); break; case IMAGE: - // Attempt to create and load our image object. - resource = - reinterpret_cast(Image::load(filePath, flags)); - - // If the object is invalid, try using PhysicsFS - if (resource == NULL) { - std::cout << "Check if exists: " << filePath << std::endl; - + { // Load the image resource file - void* buffer = NULL; - int fileSize = loadFile(filePath, buffer); + int fileSize; + void *buffer = loadFile(idPath, fileSize); - // Let the image class load it - resource = reinterpret_cast(Image::load(buffer, - fileSize)); + if (buffer != NULL) + { + // Let the image class load it + resource = reinterpret_cast(Image::load(buffer, + fileSize, flags)); - // Cleanup - if (buffer != NULL) { - delete[] buffer; + // Cleanup + free(buffer); } + else { + logger.log("Warning: resource doesn't exist!"); + } + } break; @@ -145,7 +138,7 @@ Resource* ResourceManager::get(const E_RESOURCE_TYPE &type, // Create the resource entry for this object. ResourceEntry entry; - entry.filePath = filePath; + entry.filePath = idPath; entry.resource = resource; resources[idPath] = entry; @@ -177,6 +170,9 @@ void ResourceManager::deleteInstance() void ResourceManager::searchAndAddZipFiles() { + // Add the main data directory to our PhysicsFS search path + PHYSFS_addToSearchPath("data", 1); + // Define the path in which to search std::string searchString = std::string("data/*.zip"); @@ -239,32 +235,29 @@ void ResourceManager::searchAndAddZipFiles() #endif } -int ResourceManager::loadFile(const std::string& fileName, void* buffer) +void *ResourceManager::loadFile(const std::string &fileName, int &fileSize) { // If the file doesn't exist indicate failure - if (PHYSFS_exists(fileName.c_str()) != 0) return -1; - - // Initialize the buffer value - buffer = NULL; + if (!PHYSFS_exists(fileName.c_str())) { + logger.log("Warning: %s not found!", fileName.c_str()); + return 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; + if (file == NULL) return NULL; // Print file information message - int fileLength = PHYSFS_fileLength(file); - - std::cout << fileName << " - " - << fileLength << " bytes" << std::endl; + fileSize = PHYSFS_fileLength(file); // Allocate memory in the buffer and load the file - buffer = (void*)new char[fileLength]; - PHYSFS_read(file, buffer, 1, fileLength); + void *buffer = malloc(fileSize); + PHYSFS_read(file, buffer, 1, fileSize); // Close the file and let the user deallocate the memory (safe?) PHYSFS_close(file); - return fileLength; + return buffer; } diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 5c80b1a8..1933cc76 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -36,7 +36,7 @@ struct ResourceEntry { ResourceEntry(); - Resource* resource; + Resource *resource; std::string filePath; }; @@ -115,7 +115,7 @@ class ResourceManager * * @return The size of the file that was loaded */ - int loadFile(const std::string& fileName, void* buffer); + void *loadFile(const std::string &fileName, int &fileSize); static ResourceManager *instance; -- cgit v1.2.3-70-g09d2