mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-05-11 03:23:50 +00:00
Texture loading, part 2: make TextureMgr work with special paths; some fixes
Ie. explicit relativs paths (./) or absolute paths that are not part of the regular texture search dirs Also forgot to clear a free()'d pointer.
This commit is contained in:
parent
4e632f9f6c
commit
7041143ef8
2 changed files with 44 additions and 25 deletions
|
@ -34,6 +34,17 @@ static ImageData loadGeneric(const char *fn)
|
||||||
return imageLoadGeneric(fn, false);
|
return imageLoadGeneric(fn, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This handles unix/win32 relative paths: ./rel/path
|
||||||
|
// Unix abs paths: /home/user/...
|
||||||
|
// Win32 abs paths: C:/Stuff/.. and also C:\Stuff\...
|
||||||
|
// -- We want to keep those paths untouched and just load as-is.
|
||||||
|
// This is mainly used for special things like savegame thumbnails and mod preview images,
|
||||||
|
// that exist outside of the game's known texture directories and are also not part of a running mod.
|
||||||
|
static bool isSpecialPath(const std::string& s)
|
||||||
|
{
|
||||||
|
return s[0] == '.' || s[0] == '/' || (s.length() > 1 && s[1] == ':');
|
||||||
|
}
|
||||||
|
|
||||||
static const std::string fixup(const std::string& fn)
|
static const std::string fixup(const std::string& fn)
|
||||||
{
|
{
|
||||||
std::string file = localisePathInternalModpath(fn);
|
std::string file = localisePathInternalModpath(fn);
|
||||||
|
@ -95,31 +106,38 @@ static TexLoader getFullnameAndLoader(const std::string& name, const std::string
|
||||||
return TexLoader(NULL, std::string());
|
return TexLoader(NULL, std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool locateAndLoad(TexLoadTmp& tt, const std::string& basedir)
|
||||||
|
{
|
||||||
|
std::string filename = basedir + tt.name;
|
||||||
|
TexLoader ldr = getFullnameAndLoader(filename, basedir);
|
||||||
|
if(!ldr.name.empty()) // name was cleaned up, use the updated one
|
||||||
|
tt.name = ldr.name;
|
||||||
|
if(ldr.loader)
|
||||||
|
{
|
||||||
|
if(tt.loadmode < TextureMgr::OVERWRITE && tt.curTex && tt.curTex->success && tt.curTex->filename == ldr.fn)
|
||||||
|
{
|
||||||
|
// still referring to the same file that was already loaded, so we wouldn't gain anything from loading it again
|
||||||
|
tt.success = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tt.filename = ldr.fn;
|
||||||
|
tt.img = ldr.load();
|
||||||
|
tt.success = !!tt.img.pixels;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void TextureMgr::th_loadFromFile(TexLoadTmp& tt) const
|
void TextureMgr::th_loadFromFile(TexLoadTmp& tt) const
|
||||||
{
|
{
|
||||||
std::string withoutExt;
|
if(isSpecialPath(tt.name))
|
||||||
for(size_t i = 0; i < loadFromPaths.size(); ++i)
|
if(locateAndLoad(tt, ""))
|
||||||
{
|
|
||||||
withoutExt = loadFromPaths[i] + tt.name;
|
|
||||||
TexLoader ldr = getFullnameAndLoader(withoutExt, loadFromPaths[i]);
|
|
||||||
if(!ldr.name.empty()) // name was cleaned up, use the updated one
|
|
||||||
tt.name = ldr.name;
|
|
||||||
if(ldr.loader)
|
|
||||||
{
|
|
||||||
if(tt.loadmode < OVERWRITE && tt.curTex && tt.curTex->success && tt.curTex->filename == ldr.fn)
|
|
||||||
{
|
|
||||||
tt.success = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
tt.filename = ldr.fn;
|
|
||||||
tt.img = ldr.load();
|
|
||||||
tt.success = !!tt.img.pixels;
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tt.img.pixels = NULL;
|
for(size_t i = 0; i < loadFromPaths.size(); ++i)
|
||||||
|
if(locateAndLoad(tt, loadFromPaths[i]))
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -214,7 +232,7 @@ void TextureMgr::thMain()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture *TextureMgr::finalize(const TexLoadTmp& tt)
|
Texture *TextureMgr::finalize(TexLoadTmp& tt)
|
||||||
{
|
{
|
||||||
Texture *tex = tt.curTex;
|
Texture *tex = tt.curTex;
|
||||||
if(!tex)
|
if(!tex)
|
||||||
|
@ -236,9 +254,10 @@ Texture *TextureMgr::finalize(const TexLoadTmp& tt)
|
||||||
}
|
}
|
||||||
if(tt.img.pixels)
|
if(tt.img.pixels)
|
||||||
{
|
{
|
||||||
//debugLog("LOADED TEXTURE FROM DISK: [" + tt.name + "]");
|
debugLog("LOADED TEXTURE FROM DISK: [" + tt.filename + "]");
|
||||||
tex->upload(tt.img, /*tt.mipmap*/ true);
|
tex->upload(tt.img, /*tt.mipmap*/ true);
|
||||||
free(tt.img.pixels);
|
free(tt.img.pixels);
|
||||||
|
tt.img.pixels = NULL;
|
||||||
}
|
}
|
||||||
return tex;
|
return tex;
|
||||||
}
|
}
|
||||||
|
@ -287,7 +306,7 @@ void TextureMgr::loadBatch(Texture * pdst[], const std::string texnames[], size_
|
||||||
{
|
{
|
||||||
void *p;
|
void *p;
|
||||||
workdone.pop(p);
|
workdone.pop(p);
|
||||||
const TexLoadTmp& tt = *(const TexLoadTmp*)p;
|
TexLoadTmp& tt = *(TexLoadTmp*)p;
|
||||||
Texture *tex = finalize(tt);
|
Texture *tex = finalize(tt);
|
||||||
if(pdst)
|
if(pdst)
|
||||||
pdst[i] = tex;
|
pdst[i] = tex;
|
||||||
|
|
|
@ -47,7 +47,7 @@ private:
|
||||||
void thMain(); // for int
|
void thMain(); // for int
|
||||||
|
|
||||||
void th_loadFromFile(TexLoadTmp& tt) const;
|
void th_loadFromFile(TexLoadTmp& tt) const;
|
||||||
Texture *finalize(const TexLoadTmp& tt);
|
Texture *finalize(TexLoadTmp& tt);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue