fix repeated texture allocation bug
This commit is contained in:
parent
7526315d12
commit
9cf40d7f53
|
@ -68,7 +68,7 @@ bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFron
|
||||||
|
|
||||||
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append)
|
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append)
|
||||||
{
|
{
|
||||||
auto files = listDirectoryFiles(checkPath(packagesDir));
|
auto files = listDirectoryFiles(resolvePath(packagesDir));
|
||||||
for(const std::string& file : files) {
|
for(const std::string& file : files) {
|
||||||
if(boost::ends_with(file, packageExt))
|
if(boost::ends_with(file, packageExt))
|
||||||
addToSearchPath(packagesDir + "/" + file, !append);
|
addToSearchPath(packagesDir + "/" + file, !append);
|
||||||
|
@ -77,17 +77,17 @@ void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const
|
||||||
|
|
||||||
bool ResourceManager::fileExists(const std::string& fileName)
|
bool ResourceManager::fileExists(const std::string& fileName)
|
||||||
{
|
{
|
||||||
return (PHYSFS_exists(checkPath(fileName).c_str()) && !PHYSFS_isDirectory(checkPath(fileName).c_str()));
|
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ResourceManager::directoryExists(const std::string& directoryName)
|
bool ResourceManager::directoryExists(const std::string& directoryName)
|
||||||
{
|
{
|
||||||
return (PHYSFS_isDirectory(checkPath(directoryName).c_str()));
|
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
|
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
|
||||||
{
|
{
|
||||||
std::string fullPath = checkPath(fileName);
|
std::string fullPath = resolvePath(fileName);
|
||||||
out.clear(std::ios::goodbit);
|
out.clear(std::ios::goodbit);
|
||||||
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
|
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
|
||||||
if(!file) {
|
if(!file) {
|
||||||
|
@ -144,7 +144,7 @@ bool ResourceManager::saveFile(const std::string& fileName, const std::string& d
|
||||||
|
|
||||||
FileStreamPtr ResourceManager::openFile(const std::string& fileName)
|
FileStreamPtr ResourceManager::openFile(const std::string& fileName)
|
||||||
{
|
{
|
||||||
std::string fullPath = checkPath(fileName);
|
std::string fullPath = resolvePath(fileName);
|
||||||
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
|
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
|
||||||
if(!file) {
|
if(!file) {
|
||||||
logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError());
|
logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError());
|
||||||
|
@ -175,13 +175,13 @@ FileStreamPtr ResourceManager::createFile(const std::string& fileName)
|
||||||
|
|
||||||
bool ResourceManager::deleteFile(const std::string& fileName)
|
bool ResourceManager::deleteFile(const std::string& fileName)
|
||||||
{
|
{
|
||||||
return PHYSFS_delete(checkPath(fileName).c_str()) != 0;
|
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
|
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
|
||||||
{
|
{
|
||||||
std::list<std::string> files;
|
std::list<std::string> files;
|
||||||
auto rc = PHYSFS_enumerateFiles(checkPath(directoryPath).c_str());
|
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
|
||||||
|
|
||||||
for(int i = 0; rc[i] != NULL; i++)
|
for(int i = 0; rc[i] != NULL; i++)
|
||||||
files.push_back(rc[i]);
|
files.push_back(rc[i]);
|
||||||
|
@ -190,7 +190,7 @@ std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& di
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ResourceManager::checkPath(const std::string& path)
|
std::string ResourceManager::resolvePath(const std::string& path)
|
||||||
{
|
{
|
||||||
std::string fullPath;
|
std::string fullPath;
|
||||||
if(boost::starts_with(path, "/"))
|
if(boost::starts_with(path, "/"))
|
||||||
|
@ -203,6 +203,7 @@ std::string ResourceManager::checkPath(const std::string& path)
|
||||||
}
|
}
|
||||||
if(!(boost::starts_with(fullPath, "/")))
|
if(!(boost::starts_with(fullPath, "/")))
|
||||||
logTraceWarning("the following file path is not fully resolved: ", path);
|
logTraceWarning("the following file path is not fully resolved: ", path);
|
||||||
|
boost::replace_all(fullPath, "//", "/");
|
||||||
return fullPath;
|
return fullPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
|
|
||||||
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
|
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
|
||||||
|
|
||||||
std::string checkPath(const std::string& path);
|
std::string resolvePath(const std::string& path);
|
||||||
std::string getBaseDir();
|
std::string getBaseDir();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -29,12 +29,15 @@
|
||||||
|
|
||||||
TextureManager g_textures;
|
TextureManager g_textures;
|
||||||
|
|
||||||
TexturePtr TextureManager::getTexture(const std::string& textureFile)
|
TexturePtr TextureManager::getTexture(const std::string& fileName)
|
||||||
{
|
{
|
||||||
TexturePtr texture;
|
TexturePtr texture;
|
||||||
|
|
||||||
|
// before must resolve filename to full path
|
||||||
|
std::string filePath = g_resources.resolvePath(fileName);
|
||||||
|
|
||||||
// check if the texture is already loaded
|
// check if the texture is already loaded
|
||||||
auto it = m_textures.find(textureFile);
|
auto it = m_textures.find(filePath);
|
||||||
if(it != m_textures.end()) {
|
if(it != m_textures.end()) {
|
||||||
if(it->second.expired())
|
if(it->second.expired())
|
||||||
m_textures.erase(it);
|
m_textures.erase(it);
|
||||||
|
@ -46,17 +49,18 @@ TexturePtr TextureManager::getTexture(const std::string& textureFile)
|
||||||
if(!texture) {
|
if(!texture) {
|
||||||
try {
|
try {
|
||||||
// currently only png textures are supported
|
// currently only png textures are supported
|
||||||
if(!boost::ends_with(textureFile, ".png"))
|
if(!boost::ends_with(filePath, ".png"))
|
||||||
Fw::throwException("texture file format no supported");
|
Fw::throwException("texture file format no supported");
|
||||||
|
|
||||||
// load texture file data
|
// load texture file data
|
||||||
std::stringstream fin;
|
std::stringstream fin;
|
||||||
g_resources.loadFile(textureFile, fin);
|
g_resources.loadFile(filePath, fin);
|
||||||
texture = loadPNG(fin);
|
texture = loadPNG(fin);
|
||||||
} catch(Exception& e) {
|
} catch(Exception& e) {
|
||||||
logError("unable to load texture '", textureFile, "': ", e.what());
|
logError("unable to load texture '", fileName, "': ", e.what());
|
||||||
texture = g_graphics.getEmptyTexture();
|
texture = g_graphics.getEmptyTexture();
|
||||||
}
|
}
|
||||||
|
m_textures[filePath] = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
return texture;
|
return texture;
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
class TextureManager
|
class TextureManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TexturePtr getTexture(const std::string& textureFile);
|
TexturePtr getTexture(const std::string& fileName);
|
||||||
|
|
||||||
static TexturePtr loadPNG(std::stringstream& file);
|
static TexturePtr loadPNG(std::stringstream& file);
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ OTMLDocumentPtr OTMLDocument::create()
|
||||||
OTMLDocumentPtr OTMLDocument::parse(const std::string& fileName)
|
OTMLDocumentPtr OTMLDocument::parse(const std::string& fileName)
|
||||||
{
|
{
|
||||||
std::stringstream fin;
|
std::stringstream fin;
|
||||||
std::string source = g_resources.checkPath(fileName);
|
std::string source = g_resources.resolvePath(fileName);
|
||||||
g_resources.loadFile(source, fin);
|
g_resources.loadFile(source, fin);
|
||||||
return parse(fin, source);
|
return parse(fin, source);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue