Splitting up my threading changes into smaller pieces, as it's getting too large. This change: pass strings by reference, never by value; some minor formatting cleanup in WResourceManager.
This commit is contained in:
@@ -33,45 +33,45 @@
|
|||||||
|
|
||||||
|
|
||||||
enum ENUM_WRES_INFO{
|
enum ENUM_WRES_INFO{
|
||||||
WRES_UNLOCKED = 0, //Resource is unlocked.
|
WRES_UNLOCKED = 0, //Resource is unlocked.
|
||||||
WRES_MAX_LOCK = 250, //Maximum number of locks for a resource.
|
WRES_MAX_LOCK = 250, //Maximum number of locks for a resource.
|
||||||
WRES_PERMANENT = 251, //Resource is permanent (ie, managed)
|
WRES_PERMANENT = 251, //Resource is permanent (ie, managed)
|
||||||
WRES_UNDERLOCKED = 252, //Resource was released too many times.
|
WRES_UNDERLOCKED = 252, //Resource was released too many times.
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ENUM_RETRIEVE_STYLE{
|
enum ENUM_RETRIEVE_STYLE{
|
||||||
RETRIEVE_EXISTING, //Only returns a resource if it already exists. Does not lock or unlock.
|
RETRIEVE_EXISTING, //Only returns a resource if it already exists. Does not lock or unlock.
|
||||||
RETRIEVE_NORMAL, //Returns or creates a resource. Does not change lock status.
|
RETRIEVE_NORMAL, //Returns or creates a resource. Does not change lock status.
|
||||||
RETRIEVE_LOCK, //As above, locks cached resource. Not for quads.
|
RETRIEVE_LOCK, //As above, locks cached resource. Not for quads.
|
||||||
RETRIEVE_UNLOCK, //As above, unlocks cached resource. Not for quads.
|
RETRIEVE_UNLOCK, //As above, unlocks cached resource. Not for quads.
|
||||||
RETRIEVE_RESOURCE, //Only retrieves a managed resource. Does not make a new one.
|
RETRIEVE_RESOURCE, //Only retrieves a managed resource. Does not make a new one.
|
||||||
RETRIEVE_MANAGE, //Makes resource permanent.
|
RETRIEVE_MANAGE, //Makes resource permanent.
|
||||||
RETRIEVE_THUMB, //Retrieve it as a thumbnail.
|
RETRIEVE_THUMB, //Retrieve it as a thumbnail.
|
||||||
CACHE_THUMB = RETRIEVE_THUMB, //Backwords compatibility.
|
CACHE_THUMB = RETRIEVE_THUMB, //Backwords compatibility.
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ENUM_CACHE_SUBTYPE{
|
enum ENUM_CACHE_SUBTYPE{
|
||||||
CACHE_NORMAL = (1<<0), //Use default values. Not really a flag.
|
CACHE_NORMAL = (1<<0), //Use default values. Not really a flag.
|
||||||
CACHE_EXISTING = (1<<1), //Retrieve it only if it already exists
|
CACHE_EXISTING = (1<<1), //Retrieve it only if it already exists
|
||||||
|
|
||||||
//Because these bits only modify how a cached resource's Attempt() is called,
|
//Because these bits only modify how a cached resource's Attempt() is called,
|
||||||
//We can use them over and over for each resource type.
|
//We can use them over and over for each resource type.
|
||||||
TEXTURE_SUB_EXACT = (1<<2), //Don't do any fiddling with the filename.
|
TEXTURE_SUB_EXACT = (1<<2), //Don't do any fiddling with the filename.
|
||||||
TEXTURE_SUB_CARD = (1<<3), //Retrieve using cardFile, not graphicsFile.
|
TEXTURE_SUB_CARD = (1<<3), //Retrieve using cardFile, not graphicsFile.
|
||||||
TEXTURE_SUB_AVATAR = (1<<4), //Retrieve using avatarFile, not graphicsFile.
|
TEXTURE_SUB_AVATAR = (1<<4), //Retrieve using avatarFile, not graphicsFile.
|
||||||
TEXTURE_SUB_THUMB = (1<<5),//Retrieve prepending "thumbnails\" to the filename.
|
TEXTURE_SUB_THUMB = (1<<5),//Retrieve prepending "thumbnails\" to the filename.
|
||||||
TEXTURE_SUB_5551 = (1<<6), //For textures. If we have to allocate, use RGBA5551.
|
TEXTURE_SUB_5551 = (1<<6), //For textures. If we have to allocate, use RGBA5551.
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ENUM_CACHE_ERROR{
|
enum ENUM_CACHE_ERROR{
|
||||||
CACHE_ERROR_NONE = 0,
|
CACHE_ERROR_NONE = 0,
|
||||||
CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE,
|
CACHE_ERROR_NOT_CACHED = CACHE_ERROR_NONE,
|
||||||
CACHE_ERROR_404,
|
CACHE_ERROR_404,
|
||||||
CACHE_ERROR_BAD, //Something went wrong with item->attempt()
|
CACHE_ERROR_BAD, //Something went wrong with item->attempt()
|
||||||
CACHE_ERROR_BAD_ALLOC, //Couldn't allocate item
|
CACHE_ERROR_BAD_ALLOC, //Couldn't allocate item
|
||||||
CACHE_ERROR_LOST,
|
CACHE_ERROR_LOST,
|
||||||
CACHE_ERROR_NOT_MANAGED,
|
CACHE_ERROR_NOT_MANAGED,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WCacheSort{
|
struct WCacheSort{
|
||||||
@@ -79,147 +79,149 @@ struct WCacheSort{
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <class cacheItem, class cacheActual>
|
template <class cacheItem, class cacheActual>
|
||||||
class WCache{
|
class WCache
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
friend class WResourceManager;
|
friend class WResourceManager;
|
||||||
|
|
||||||
WCache();
|
WCache();
|
||||||
~WCache();
|
~WCache();
|
||||||
|
|
||||||
cacheItem* Retrieve(int id, string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Primary interface function.
|
cacheItem* Retrieve(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Primary interface function.
|
||||||
bool Release(cacheActual* actual); //Releases an item, and deletes it if unlocked.
|
bool Release(cacheActual* actual); //Releases an item, and deletes it if unlocked.
|
||||||
bool RemoveMiss(int id=0); //Removes a cache miss.
|
bool RemoveMiss(int id=0); //Removes a cache miss.
|
||||||
bool RemoveOldest(); //Remove oldest unlocked item.
|
bool RemoveOldest(); //Remove oldest unlocked item.
|
||||||
bool Cleanup(); //Repeats RemoveOldest() until cache fits in size limits
|
bool Cleanup(); //Repeats RemoveOldest() until cache fits in size limits
|
||||||
void ClearUnlocked(); //Remove all unlocked items.
|
void ClearUnlocked(); //Remove all unlocked items.
|
||||||
void Refresh(); //Refreshes all cache items.
|
void Refresh(); //Refreshes all cache items.
|
||||||
unsigned int Flatten(); //Ensures that the times don't loop. Returns new lastTime.
|
unsigned int Flatten(); //Ensures that the times don't loop. Returns new lastTime.
|
||||||
void Resize(unsigned long size, int items); //Sets new limits, then enforces them. Lock safe, so not a "hard limit".
|
void Resize(unsigned long size, int items); //Sets new limits, then enforces them. Lock safe, so not a "hard limit".
|
||||||
protected:
|
protected:
|
||||||
bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
|
bool RemoveItem(cacheItem * item, bool force = true); //Removes an item, deleting it. if(force), ignores locks / permanent
|
||||||
bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care.
|
bool UnlinkCache(cacheItem * item); //Removes an item from our cache, does not delete it. Use with care.
|
||||||
bool Delete(cacheItem * item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
|
bool Delete(cacheItem * item); //SAFE_DELETE and garbage collect. If maxCached == 0, nullify first. (This means you have to free that cacheActual later!)
|
||||||
cacheItem* Get(int id, string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve.
|
cacheItem* Get(int id, const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL); //Subordinate to Retrieve.
|
||||||
cacheItem* AttemptNew(string filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails.
|
cacheItem* AttemptNew(const string& filename, int submode); //Attempts a new cache item, progressively clearing cache if it fails.
|
||||||
|
|
||||||
int makeID(int id, string filename, int submode); //Makes an ID appropriate to the submode.
|
int makeID(int id, const string& filename, int submode); //Makes an ID appropriate to the submode.
|
||||||
|
|
||||||
map<string,int> ids;
|
map<string,int> ids;
|
||||||
map<int,cacheItem*> cache;
|
map<int,cacheItem*> cache;
|
||||||
map<int,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate.
|
map<int,cacheItem*> managed; //Cache can be arbitrarily large, so managed items are seperate.
|
||||||
unsigned long totalSize;
|
unsigned long totalSize;
|
||||||
unsigned long cacheSize;
|
unsigned long cacheSize;
|
||||||
|
|
||||||
//Applies to cacheSize only.
|
|
||||||
unsigned long maxCacheSize;
|
|
||||||
unsigned int maxCached;
|
|
||||||
|
|
||||||
unsigned int cacheItems;
|
//Applies to cacheSize only.
|
||||||
int mError;
|
unsigned long maxCacheSize;
|
||||||
|
unsigned int maxCached;
|
||||||
|
|
||||||
|
unsigned int cacheItems;
|
||||||
|
int mError;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct WManagedQuad {
|
struct WManagedQuad
|
||||||
WCachedTexture * texture;
|
{
|
||||||
string resname;
|
WCachedTexture * texture;
|
||||||
|
string resname;
|
||||||
};
|
};
|
||||||
|
|
||||||
//This class is a wrapper for JResourceManager
|
//This class is a wrapper for JResourceManager
|
||||||
class WResourceManager: public JResourceManager
|
class WResourceManager: public JResourceManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WResourceManager();
|
WResourceManager();
|
||||||
~WResourceManager();
|
~WResourceManager();
|
||||||
|
|
||||||
void Unmiss(string filename);
|
|
||||||
JQuad * RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL);
|
|
||||||
JSample * RetrieveSample(string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
|
||||||
JTexture * RetrieveTexture(string filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
|
||||||
JQuad * RetrieveQuad(string filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0);
|
|
||||||
JQuad * RetrieveTempQuad(string filename, int submode = CACHE_NORMAL);
|
|
||||||
hgeParticleSystemInfo * RetrievePSI(string filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
|
||||||
int RetrieveError();
|
|
||||||
|
|
||||||
void Release(JTexture * tex);
|
void Unmiss(string filename);
|
||||||
void Release(JSample * sample);
|
JQuad * RetrieveCard(MTGCard * card, int style = RETRIEVE_NORMAL,int submode = CACHE_NORMAL);
|
||||||
bool RemoveOldest();
|
JSample * RetrieveSample(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||||
|
JTexture * RetrieveTexture(const string& filename, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||||
bool Cleanup(); //Force a cleanup. Return false if nothing removed.
|
JQuad * RetrieveQuad(const string& filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_LOCK, int submode = CACHE_NORMAL, int id = 0);
|
||||||
void ClearUnlocked(); //Remove unlocked items.
|
JQuad * RetrieveTempQuad(const string& filename, int submode = CACHE_NORMAL);
|
||||||
void Refresh(); //Refreshes all files in cache, for when mode/profile changes.
|
hgeParticleSystemInfo * RetrievePSI(const string& filename, JQuad * texture, int style = RETRIEVE_NORMAL, int submode = CACHE_NORMAL);
|
||||||
|
int RetrieveError();
|
||||||
|
|
||||||
unsigned int nowTime();
|
void Release(JTexture * tex);
|
||||||
|
void Release(JSample * sample);
|
||||||
|
bool RemoveOldest();
|
||||||
|
|
||||||
unsigned long Size();
|
bool Cleanup(); //Force a cleanup. Return false if nothing removed.
|
||||||
unsigned long SizeCached();
|
void ClearUnlocked(); //Remove unlocked items.
|
||||||
unsigned long SizeManaged();
|
void Refresh(); //Refreshes all files in cache, for when mode/profile changes.
|
||||||
|
|
||||||
unsigned int Count();
|
unsigned int nowTime();
|
||||||
unsigned int CountCached();
|
|
||||||
unsigned int CountManaged();
|
|
||||||
|
|
||||||
int CreateQuad(const string &quadName, const string &textureName, float x, float y, float width, float height);
|
unsigned long Size();
|
||||||
JQuad* GetQuad(const string &quadName);
|
unsigned long SizeCached();
|
||||||
JQuad* GetQuad(int id);
|
unsigned long SizeManaged();
|
||||||
|
|
||||||
int AddQuadToManaged(const WManagedQuad& inManagedQuad);
|
unsigned int Count();
|
||||||
|
unsigned int CountCached();
|
||||||
|
unsigned int CountManaged();
|
||||||
|
|
||||||
//Our file redirect system.
|
int CreateQuad(const string &quadName, const string &textureName, float x, float y, float width, float height);
|
||||||
string graphicsFile(const string filename);
|
JQuad* GetQuad(const string &quadName);
|
||||||
string avatarFile(const string filename);
|
JQuad* GetQuad(int id);
|
||||||
string cardFile(const string filename);
|
|
||||||
string musicFile(const string filename);
|
|
||||||
string sfxFile(const string filename);
|
|
||||||
int fileOK(string filename, bool relative = false);
|
|
||||||
int dirOK(string dirname);
|
|
||||||
|
|
||||||
//For backwards compatibility with JResourceManager. Avoid using these, they're not optimal.
|
int AddQuadToManaged(const WManagedQuad& inManagedQuad);
|
||||||
int CreateTexture(const string &textureName);
|
|
||||||
JTexture* GetTexture(const string &textureName);
|
|
||||||
JTexture* GetTexture(int id);
|
|
||||||
|
|
||||||
// Font management functions
|
//Our file redirect system.
|
||||||
void InitFonts(const std::string& inLang);
|
string graphicsFile(const string& filename);
|
||||||
int ReloadWFonts();
|
string avatarFile(const string& filename);
|
||||||
WFont* LoadWFont(const string& inFontname, int inFontHeight, int inFontID);
|
string cardFile(const string& filename);
|
||||||
WFont* GetWFont(int id);
|
string musicFile(const string& filename);
|
||||||
void RemoveWFonts();
|
string sfxFile(const string& filename);
|
||||||
|
int fileOK(const string&, bool relative = false);
|
||||||
//Wrapped from JSoundSystem. TODO: Privatize.
|
int dirOK(const string& dirname);
|
||||||
JMusic * ssLoadMusic(const char *fileName);
|
|
||||||
|
|
||||||
//Resets the cache limits on when it starts to purge data.
|
//For backwards compatibility with JResourceManager. Avoid using these, they're not optimal.
|
||||||
void ResetCacheLimits();
|
int CreateTexture(const string &textureName);
|
||||||
|
JTexture* GetTexture(const string &textureName);
|
||||||
|
JTexture* GetTexture(int id);
|
||||||
|
|
||||||
void DebugRender();
|
// Font management functions
|
||||||
|
void InitFonts(const std::string& inLang);
|
||||||
|
int ReloadWFonts();
|
||||||
|
WFont* LoadWFont(const string& inFontname, int inFontHeight, int inFontID);
|
||||||
|
WFont* GetWFont(int id);
|
||||||
|
void RemoveWFonts();
|
||||||
|
|
||||||
|
//Wrapped from JSoundSystem. TODO: Privatize.
|
||||||
|
JMusic * ssLoadMusic(const char *fileName);
|
||||||
|
|
||||||
|
//Resets the cache limits on when it starts to purge data.
|
||||||
|
void ResetCacheLimits();
|
||||||
|
|
||||||
|
void DebugRender();
|
||||||
|
|
||||||
#ifdef DEBUG_CACHE
|
#ifdef DEBUG_CACHE
|
||||||
unsigned long menuCached;
|
unsigned long menuCached;
|
||||||
string debugMessage;
|
string debugMessage;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool bThemedCards; //Does the theme have a "sets" directory for overwriting cards?
|
bool bThemedCards; //Does the theme have a "sets" directory for overwriting cards?
|
||||||
void FlattenTimes(); //To prevent bad cache timing on int overflow
|
void FlattenTimes(); //To prevent bad cache timing on int overflow
|
||||||
|
|
||||||
//For cached stuff
|
//For cached stuff
|
||||||
WCache<WCachedTexture,JTexture> textureWCache;
|
WCache<WCachedTexture,JTexture> textureWCache;
|
||||||
WCache<WCachedSample,JSample> sampleWCache;
|
WCache<WCachedSample,JSample> sampleWCache;
|
||||||
WCache<WCachedParticles,hgeParticleSystemInfo> psiWCache;
|
WCache<WCachedParticles,hgeParticleSystemInfo> psiWCache;
|
||||||
|
|
||||||
typedef std::map<std::string, WManagedQuad> ManagedQuadMap;
|
typedef std::map<std::string, WManagedQuad> ManagedQuadMap;
|
||||||
ManagedQuadMap mManagedQuads;
|
ManagedQuadMap mManagedQuads;
|
||||||
|
|
||||||
typedef std::map<int, std::string> IDLookupMap;
|
typedef std::map<int, std::string> IDLookupMap;
|
||||||
IDLookupMap mIDLookupMap;
|
IDLookupMap mIDLookupMap;
|
||||||
|
|
||||||
//Statistics of record.
|
|
||||||
unsigned int lastTime;
|
|
||||||
int lastError;
|
|
||||||
|
|
||||||
typedef std::map<int, WFont*> FontMap;
|
//Statistics of record.
|
||||||
FontMap mWFontMap;
|
unsigned int lastTime;
|
||||||
std::string mFontFileExtension;
|
int lastError;
|
||||||
|
|
||||||
|
typedef std::map<int, WFont*> FontMap;
|
||||||
|
FontMap mWFontMap;
|
||||||
|
std::string mFontFileExtension;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern WResourceManager resources;
|
extern WResourceManager resources;
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ void WResourceManager::DebugRender()
|
|||||||
if (textureWCache.totalSize > textureWCache.cacheSize) man = textureWCache.totalSize - textureWCache.cacheSize;
|
if (textureWCache.totalSize > textureWCache.cacheSize) man = textureWCache.totalSize - textureWCache.cacheSize;
|
||||||
|
|
||||||
sprintf(buf, "Textures %u+%llu (of %u) items (%u misses), Pixels: %lu (of %lu) + %lu", textureWCache.cacheItems,
|
sprintf(buf, "Textures %u+%llu (of %u) items (%u misses), Pixels: %lu (of %lu) + %lu", textureWCache.cacheItems,
|
||||||
(long long unsigned int) textureWCache.managed.size(), textureWCache.maxCached, misses,
|
(long long unsigned int) textureWCache.managed.size(), textureWCache.maxCached, misses,
|
||||||
textureWCache.cacheSize, textureWCache.maxCacheSize, man);
|
textureWCache.cacheSize, textureWCache.maxCacheSize, man);
|
||||||
font->DrawString(buf, 10, 5);
|
font->DrawString(buf, 10, 5);
|
||||||
|
|
||||||
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
||||||
@@ -82,7 +82,7 @@ void WResourceManager::DebugRender()
|
|||||||
|
|
||||||
#ifdef DEBUG_CACHE
|
#ifdef DEBUG_CACHE
|
||||||
if(debugMessage.size())
|
if(debugMessage.size())
|
||||||
font->DrawString(debugMessage.c_str(), SCREEN_WIDTH-10,SCREEN_HEIGHT-25,JGETEXT_RIGHT);
|
font->DrawString(debugMessage.c_str(), SCREEN_WIDTH-10,SCREEN_HEIGHT-25,JGETEXT_RIGHT);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -116,6 +116,7 @@ unsigned long WResourceManager::SizeManaged()
|
|||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int WResourceManager::Count()
|
unsigned int WResourceManager::Count()
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
@@ -127,6 +128,7 @@ unsigned int WResourceManager::Count()
|
|||||||
count += psiWCache.managed.size();
|
count += psiWCache.managed.size();
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int WResourceManager::CountCached()
|
unsigned int WResourceManager::CountCached()
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
@@ -185,6 +187,7 @@ WResourceManager::WResourceManager()
|
|||||||
|
|
||||||
bThemedCards = false;
|
bThemedCards = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WResourceManager::~WResourceManager()
|
WResourceManager::~WResourceManager()
|
||||||
{
|
{
|
||||||
LOG("==Destroying WResourceManager==");
|
LOG("==Destroying WResourceManager==");
|
||||||
@@ -333,13 +336,13 @@ JQuad * WResourceManager::GetQuad(int id)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
JQuad * WResourceManager::RetrieveTempQuad(string filename, int submode)
|
JQuad * WResourceManager::RetrieveTempQuad(const string& filename, int submode)
|
||||||
{
|
{
|
||||||
return RetrieveQuad(filename, 0, 0, 0, 0, "temporary", RETRIEVE_NORMAL, submode);
|
return RetrieveQuad(filename, 0, 0, 0, 0, "temporary", RETRIEVE_NORMAL, submode);
|
||||||
}
|
}
|
||||||
|
|
||||||
JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY, float width, float height, string resname,
|
JQuad * WResourceManager::RetrieveQuad(const string& filename, float offX, float offY, float width, float height, string resname,
|
||||||
int style, int submode, int id)
|
int style, int submode, int id)
|
||||||
{
|
{
|
||||||
JQuad * jq = NULL;
|
JQuad * jq = NULL;
|
||||||
|
|
||||||
@@ -399,6 +402,7 @@ JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY,
|
|||||||
//Texture doesn't exist, so no quad.
|
//Texture doesn't exist, so no quad.
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WResourceManager::Release(JTexture * tex)
|
void WResourceManager::Release(JTexture * tex)
|
||||||
{
|
{
|
||||||
if (!tex) return;
|
if (!tex) return;
|
||||||
@@ -416,7 +420,7 @@ void WResourceManager::Release(JTexture * tex)
|
|||||||
{
|
{
|
||||||
it->second->unlock(); //Release one lock.
|
it->second->unlock(); //Release one lock.
|
||||||
if (it->second->locks != WRES_UNLOCKED) //Normally we'd call isLocked, but this way ignores quads.
|
if (it->second->locks != WRES_UNLOCKED) //Normally we'd call isLocked, but this way ignores quads.
|
||||||
return; //Locked
|
return; //Locked
|
||||||
}
|
}
|
||||||
|
|
||||||
textureWCache.Delete(it->second);
|
textureWCache.Delete(it->second);
|
||||||
@@ -454,10 +458,8 @@ void WResourceManager::Release(JSample * sample)
|
|||||||
sampleWCache.Release(sample);
|
sampleWCache.Release(sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
JTexture * WResourceManager::RetrieveTexture(string filename, int style, int submode)
|
JTexture * WResourceManager::RetrieveTexture(const string& filename, int style, int submode)
|
||||||
{
|
{
|
||||||
WCachedTexture * res = NULL;
|
|
||||||
|
|
||||||
//Aliases.
|
//Aliases.
|
||||||
if (style == RETRIEVE_THUMB)
|
if (style == RETRIEVE_THUMB)
|
||||||
{
|
{
|
||||||
@@ -465,7 +467,7 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style, int sub
|
|||||||
style = RETRIEVE_NORMAL;
|
style = RETRIEVE_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = textureWCache.Retrieve(0, filename, style, submode);
|
WCachedTexture* res = textureWCache.Retrieve(0, filename, style, submode);
|
||||||
lastError = textureWCache.mError;
|
lastError = textureWCache.mError;
|
||||||
|
|
||||||
if (res)
|
if (res)
|
||||||
@@ -478,25 +480,25 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style, int sub
|
|||||||
{
|
{
|
||||||
switch(textureWCache.mError)
|
switch(textureWCache.mError)
|
||||||
{
|
{
|
||||||
case CACHE_ERROR_NONE:
|
case CACHE_ERROR_NONE:
|
||||||
debugMessage = "Not in cache: ";
|
debugMessage = "Not in cache: ";
|
||||||
break;
|
break;
|
||||||
case CACHE_ERROR_404:
|
case CACHE_ERROR_404:
|
||||||
debugMessage = "File not found: ";
|
debugMessage = "File not found: ";
|
||||||
break;
|
break;
|
||||||
case CACHE_ERROR_BAD_ALLOC:
|
case CACHE_ERROR_BAD_ALLOC:
|
||||||
debugMessage = "Out of memory: ";
|
debugMessage = "Out of memory: ";
|
||||||
break;
|
break;
|
||||||
case CACHE_ERROR_BAD:
|
case CACHE_ERROR_BAD:
|
||||||
debugMessage = "Cache bad: ";
|
debugMessage = "Cache bad: ";
|
||||||
break;
|
break;
|
||||||
case CACHE_ERROR_NOT_MANAGED:
|
case CACHE_ERROR_NOT_MANAGED:
|
||||||
debugMessage = "Resource not managed: ";
|
debugMessage = "Resource not managed: ";
|
||||||
break;
|
break;
|
||||||
case CACHE_ERROR_LOST:
|
case CACHE_ERROR_LOST:
|
||||||
debugMessage = "Resource went bad, potential memory leak: ";
|
debugMessage = "Resource went bad, potential memory leak: ";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
debugMessage = "Unspecified error: ";
|
debugMessage = "Unspecified error: ";
|
||||||
}
|
}
|
||||||
debugMessage += filename;
|
debugMessage += filename;
|
||||||
@@ -540,9 +542,8 @@ JTexture* WResourceManager::GetTexture(int id)
|
|||||||
return jtex;
|
return jtex;
|
||||||
}
|
}
|
||||||
|
|
||||||
hgeParticleSystemInfo * WResourceManager::RetrievePSI(string filename, JQuad * texture, int style, int submode)
|
hgeParticleSystemInfo * WResourceManager::RetrievePSI(const string& filename, JQuad * texture, int style, int submode)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!texture) return NULL;
|
if (!texture) return NULL;
|
||||||
|
|
||||||
WCachedParticles * res = psiWCache.Retrieve(0, filename, style, submode);
|
WCachedParticles * res = psiWCache.Retrieve(0, filename, style, submode);
|
||||||
@@ -558,7 +559,7 @@ hgeParticleSystemInfo * WResourceManager::RetrievePSI(string filename, JQuad * t
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSample * WResourceManager::RetrieveSample(string filename, int style, int submode)
|
JSample * WResourceManager::RetrieveSample(const string& filename, int style, int submode)
|
||||||
{
|
{
|
||||||
WCachedSample * tc = NULL;
|
WCachedSample * tc = NULL;
|
||||||
tc = sampleWCache.Retrieve(0, filename, style, submode);
|
tc = sampleWCache.Retrieve(0, filename, style, submode);
|
||||||
@@ -574,7 +575,7 @@ JSample * WResourceManager::RetrieveSample(string filename, int style, int submo
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
string WResourceManager::graphicsFile(const string filename)
|
string WResourceManager::graphicsFile(const string& filename)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
||||||
@@ -598,16 +599,16 @@ string WResourceManager::graphicsFile(const string filename)
|
|||||||
if (fileOK(buf, true)) return buf;
|
if (fileOK(buf, true)) return buf;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
//FIXME Put back when we're using modes.
|
//FIXME Put back when we're using modes.
|
||||||
//Failure. Check mode graphics
|
//Failure. Check mode graphics
|
||||||
string mode = options[Options::ACTIVE_MODE].str;
|
string mode = options[Options::ACTIVE_MODE].str;
|
||||||
|
|
||||||
if(mode != "" && mode != "Default"){
|
if(mode != "" && mode != "Default"){
|
||||||
sprintf(buf,"modes/%s/graphics/%s",mode.c_str(),filename.c_str());
|
sprintf(buf,"modes/%s/graphics/%s",mode.c_str(),filename.c_str());
|
||||||
if(fileOK(buf,true))
|
if(fileOK(buf,true))
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//Failure. Check graphics
|
//Failure. Check graphics
|
||||||
char graphdir[512];
|
char graphdir[512];
|
||||||
sprintf(graphdir, "graphics/%s", filename.c_str());
|
sprintf(graphdir, "graphics/%s", filename.c_str());
|
||||||
@@ -625,7 +626,7 @@ string WResourceManager::graphicsFile(const string filename)
|
|||||||
return graphdir;
|
return graphdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
string WResourceManager::avatarFile(const string filename)
|
string WResourceManager::avatarFile(const string& filename)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
||||||
@@ -652,16 +653,16 @@ string WResourceManager::avatarFile(const string filename)
|
|||||||
if (fileOK(buf, true)) return buf;
|
if (fileOK(buf, true)) return buf;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
//FIXME Put back when we're using modes.
|
//FIXME Put back when we're using modes.
|
||||||
//Failure. Check mode graphics
|
//Failure. Check mode graphics
|
||||||
string mode = options[Options::ACTIVE_MODE].str;
|
string mode = options[Options::ACTIVE_MODE].str;
|
||||||
|
|
||||||
if(mode != "" && mode != "Default"){
|
if(mode != "" && mode != "Default"){
|
||||||
sprintf(buf,"modes/%s/graphics/%s",mode.c_str(),filename.c_str());
|
sprintf(buf,"modes/%s/graphics/%s",mode.c_str(),filename.c_str());
|
||||||
if(fileOK(buf,true))
|
if(fileOK(buf,true))
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Failure. Check Baka
|
//Failure. Check Baka
|
||||||
sprintf(buf, "ai/baka/avatars/%s", filename.c_str());
|
sprintf(buf, "ai/baka/avatars/%s", filename.c_str());
|
||||||
@@ -679,7 +680,7 @@ string WResourceManager::avatarFile(const string filename)
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
string WResourceManager::cardFile(const string filename)
|
string WResourceManager::cardFile(const string& filename)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
string::size_type i = 0;
|
string::size_type i = 0;
|
||||||
@@ -716,15 +717,15 @@ string WResourceManager::cardFile(const string filename)
|
|||||||
|
|
||||||
//FIXME Put back when we're using modes.
|
//FIXME Put back when we're using modes.
|
||||||
/*
|
/*
|
||||||
//Failure. Check mode
|
//Failure. Check mode
|
||||||
string mode = options[Options::ACTIVE_MODE].str;
|
string mode = options[Options::ACTIVE_MODE].str;
|
||||||
|
|
||||||
if(mode != "" && mode != "Default"){
|
if(mode != "" && mode != "Default"){
|
||||||
sprintf(buf,"modes/%s/sets/%s",mode.c_str(),filename.c_str());
|
sprintf(buf,"modes/%s/sets/%s",mode.c_str(),filename.c_str());
|
||||||
if(fileOK(buf,true))
|
if(fileOK(buf,true))
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//Failure. Assume it's in a zip file?
|
//Failure. Assume it's in a zip file?
|
||||||
if (!set.size())
|
if (!set.size())
|
||||||
{ //Didn't fill "set" string, so do it now.
|
{ //Didn't fill "set" string, so do it now.
|
||||||
@@ -752,7 +753,7 @@ string WResourceManager::cardFile(const string filename)
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
string WResourceManager::musicFile(const string filename)
|
string WResourceManager::musicFile(const string& filename)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
||||||
@@ -766,15 +767,15 @@ string WResourceManager::musicFile(const string filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
//FIXME Put back when we're using modes.
|
//FIXME Put back when we're using modes.
|
||||||
//Failure. Check mode
|
//Failure. Check mode
|
||||||
string mode = options[Options::ACTIVE_MODE].str;
|
string mode = options[Options::ACTIVE_MODE].str;
|
||||||
|
|
||||||
if(mode != "" && mode != "Default"){
|
if(mode != "" && mode != "Default"){
|
||||||
sprintf(buf,"modes/%s/sound/%s",mode.c_str(),filename.c_str());
|
sprintf(buf,"modes/%s/sound/%s",mode.c_str(),filename.c_str());
|
||||||
if(fileOK(buf,true))
|
if(fileOK(buf,true))
|
||||||
return buf;
|
return buf;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//Failure. Check sound
|
//Failure. Check sound
|
||||||
char defdir[512];
|
char defdir[512];
|
||||||
@@ -789,7 +790,7 @@ string WResourceManager::musicFile(const string filename)
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
string WResourceManager::sfxFile(const string filename)
|
string WResourceManager::sfxFile(const string& filename)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
||||||
@@ -803,15 +804,15 @@ string WResourceManager::sfxFile(const string filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
//FIXME: Put back when we're using modes.
|
//FIXME: Put back when we're using modes.
|
||||||
//Failure. Check mode
|
//Failure. Check mode
|
||||||
string mode = options[Options::ACTIVE_MODE].str;
|
string mode = options[Options::ACTIVE_MODE].str;
|
||||||
if(mode != "" && mode != "Default"){
|
if(mode != "" && mode != "Default"){
|
||||||
sprintf(buf,"modes/%s/sound/sfx/%s",mode.c_str(),filename.c_str());
|
sprintf(buf,"modes/%s/sound/sfx/%s",mode.c_str(),filename.c_str());
|
||||||
if(fileOK(buf,true))
|
if(fileOK(buf,true))
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
//Failure. Check sound
|
//Failure. Check sound
|
||||||
char defdir[512];
|
char defdir[512];
|
||||||
sprintf(defdir, "sound/sfx/%s", filename.c_str());
|
sprintf(defdir, "sound/sfx/%s", filename.c_str());
|
||||||
@@ -821,7 +822,7 @@ string WResourceManager::sfxFile(const string filename)
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
int WResourceManager::dirOK(string dirname)
|
int WResourceManager::dirOK(const string& dirname)
|
||||||
{
|
{
|
||||||
char fname[512];
|
char fname[512];
|
||||||
|
|
||||||
@@ -830,7 +831,7 @@ int WResourceManager::dirOK(string dirname)
|
|||||||
|
|
||||||
struct _stat statBuffer;
|
struct _stat statBuffer;
|
||||||
return (_stat(fname, &statBuffer) >= 0 && // make sure it exists
|
return (_stat(fname, &statBuffer) >= 0 && // make sure it exists
|
||||||
statBuffer.st_mode & S_IFDIR); // and it's not a file
|
statBuffer.st_mode & S_IFDIR); // and it's not a file
|
||||||
#else
|
#else
|
||||||
sprintf(fname, "%s", JGE_GET_RES(dirname).c_str());
|
sprintf(fname, "%s", JGE_GET_RES(dirname).c_str());
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@@ -839,7 +840,7 @@ int WResourceManager::dirOK(string dirname)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int WResourceManager::fileOK(string filename, bool relative)
|
int WResourceManager::fileOK(const string& filename, bool relative)
|
||||||
{
|
{
|
||||||
std::ifstream * fp = NULL;
|
std::ifstream * fp = NULL;
|
||||||
if (relative)
|
if (relative)
|
||||||
@@ -1068,10 +1069,10 @@ bool WCache<cacheItem, cacheActual>::RemoveOldest()
|
|||||||
{
|
{
|
||||||
typename map<int, cacheItem*>::iterator oldest = cache.end();
|
typename map<int, cacheItem*>::iterator oldest = cache.end();
|
||||||
|
|
||||||
for (typename map<int, cacheItem *>::iterator it = cache.begin(); it != cache.end(); it++)
|
for (typename map<int, cacheItem*>::iterator it = cache.begin(); it != cache.end(); ++it)
|
||||||
{
|
{
|
||||||
if (it->second && !it->second->isLocked() && (oldest == cache.end() || it->second->lastTime < oldest->second->lastTime)) oldest
|
if (it->second && !it->second->isLocked() && (oldest == cache.end() || it->second->lastTime < oldest->second->lastTime)) oldest
|
||||||
= it;
|
= it;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldest != cache.end() && oldest->second && !oldest->second->isLocked())
|
if (oldest != cache.end() && oldest->second && !oldest->second->isLocked())
|
||||||
@@ -1117,7 +1118,7 @@ void WCache<cacheItem, cacheActual>::Resize(unsigned long size, int items)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class cacheItem, class cacheActual>
|
template<class cacheItem, class cacheActual>
|
||||||
cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submode)
|
cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(const string& filename, int submode)
|
||||||
{
|
{
|
||||||
if (submode & CACHE_EXISTING)
|
if (submode & CACHE_EXISTING)
|
||||||
{ //Should never get this far.
|
{ //Should never get this far.
|
||||||
@@ -1162,7 +1163,7 @@ cacheItem* WCache<cacheItem, cacheActual>::AttemptNew(string filename, int submo
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class cacheItem, class cacheActual>
|
template<class cacheItem, class cacheActual>
|
||||||
cacheItem * WCache<cacheItem, cacheActual>::Retrieve(int id, string filename, int style, int submode)
|
cacheItem* WCache<cacheItem, cacheActual>::Retrieve(int id, const string& filename, int style, int submode)
|
||||||
{
|
{
|
||||||
//Check cache.
|
//Check cache.
|
||||||
cacheItem * tc = NULL;
|
cacheItem * tc = NULL;
|
||||||
@@ -1220,12 +1221,13 @@ cacheItem * WCache<cacheItem, cacheActual>::Retrieve(int id, string filename, in
|
|||||||
|
|
||||||
//Record managed failure. Cache failure is recorded in Get().
|
//Record managed failure. Cache failure is recorded in Get().
|
||||||
if ((style == RETRIEVE_MANAGE || style == RETRIEVE_RESOURCE) && mError == CACHE_ERROR_404) managed[makeID(id, filename, submode)]
|
if ((style == RETRIEVE_MANAGE || style == RETRIEVE_RESOURCE) && mError == CACHE_ERROR_404) managed[makeID(id, filename, submode)]
|
||||||
= NULL;
|
= NULL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class cacheItem, class cacheActual>
|
template<class cacheItem, class cacheActual>
|
||||||
int WCache<cacheItem, cacheActual>::makeID(int id, string filename, int submode)
|
int WCache<cacheItem, cacheActual>::makeID(int id, const string& filename, int submode)
|
||||||
{
|
{
|
||||||
int mId = id;
|
int mId = id;
|
||||||
if (!mId)
|
if (!mId)
|
||||||
@@ -1250,7 +1252,7 @@ int WCache<cacheItem, cacheActual>::makeID(int id, string filename, int submode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class cacheItem, class cacheActual>
|
template<class cacheItem, class cacheActual>
|
||||||
cacheItem * WCache<cacheItem, cacheActual>::Get(int id, string filename, int style, int submode)
|
cacheItem* WCache<cacheItem, cacheActual>::Get(int id, const string& filename, int style, int submode)
|
||||||
{
|
{
|
||||||
typename map<int, cacheItem*>::iterator it;
|
typename map<int, cacheItem*>::iterator it;
|
||||||
int lookup = makeID(id, filename, submode);
|
int lookup = makeID(id, filename, submode);
|
||||||
@@ -1385,9 +1387,9 @@ bool WCache<cacheItem, cacheActual>::Cleanup()
|
|||||||
while (cacheItems > MAX_CACHE_OBJECTS || cacheItems > maxCached || cacheSize > maxCacheSize
|
while (cacheItems > MAX_CACHE_OBJECTS || cacheItems > maxCached || cacheSize > maxCacheSize
|
||||||
#if defined WIN32 || defined LINUX || defined (IOS)
|
#if defined WIN32 || defined LINUX || defined (IOS)
|
||||||
#else
|
#else
|
||||||
|| ramAvailableLineareMax() < MIN_LINEAR_RAM
|
|| ramAvailableLineareMax() < MIN_LINEAR_RAM
|
||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (!RemoveOldest())
|
if (!RemoveOldest())
|
||||||
{
|
{
|
||||||
@@ -1505,7 +1507,7 @@ bool WCache<cacheItem, cacheActual>::Delete(cacheItem * item)
|
|||||||
cacheSize -= isize;
|
cacheSize -= isize;
|
||||||
#ifdef DEBUG_CACHE
|
#ifdef DEBUG_CACHE
|
||||||
if(cacheItems == 0)
|
if(cacheItems == 0)
|
||||||
DebugTrace("cacheItems out of sync.");
|
DebugTrace("cacheItems out of sync.");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cacheItems--;
|
cacheItems--;
|
||||||
|
|||||||
Reference in New Issue
Block a user