Jeck - Cache now pretty damn near guarantees successful retrieval.

* The only fail condition is if all resources are locked, or the file doesn't exist at all.
This commit is contained in:
wagic.jeck
2009-09-09 05:02:08 +00:00
parent e3d86f0824
commit 4993cc5dea
2 changed files with 45 additions and 7 deletions
+9
View File
@@ -6,8 +6,14 @@
#include "MTGDeck.h" #include "MTGDeck.h"
#include "MTGCard.h" #include "MTGCard.h"
#ifdef WIN32
#define CACHE_SIZE_PIXELS 20000000
#define MAX_CACHE_OBJECTS 2000
#else
#define CACHE_SIZE_PIXELS 2000000 #define CACHE_SIZE_PIXELS 2000000
#define MAX_CACHE_OBJECTS 200 #define MAX_CACHE_OBJECTS 200
#endif
class WCachedResource{ class WCachedResource{
public: public:
@@ -111,8 +117,11 @@ public:
private: private:
bool RemoveOldestTexture(); bool RemoveOldestTexture();
bool RemoveOldestSample(); bool RemoveOldestSample();
bool cleanup(); bool cleanup();
JTexture * attemptTexture(string filename, int mode = 0, int format = TEXTURE_FORMAT);
WCachedTexture * getCachedTexture(string filename, bool makenew = true, int mode = 0, int format = TEXTURE_FORMAT); WCachedTexture * getCachedTexture(string filename, bool makenew = true, int mode = 0, int format = TEXTURE_FORMAT);
WCachedTexture * getCachedCard(MTGCard * card, int type = CACHE_CARD, bool makenew = true); WCachedTexture * getCachedCard(MTGCard * card, int type = CACHE_CARD, bool makenew = true);
WCachedSample * getCachedSample(string filename, bool makenew = true); WCachedSample * getCachedSample(string filename, bool makenew = true);
+35 -6
View File
@@ -238,7 +238,8 @@ WCachedTexture * WResourceManager::getCachedTexture(string filename, bool makene
//Space in cache, make new texture //Space in cache, make new texture
if(cleanup()){ if(cleanup()){
ctex = NEW WCachedTexture(); ctex = NEW WCachedTexture();
ctex->texture = JRenderer::GetInstance()->LoadTexture(graphicsFile(filename).c_str(),mode,format); //Within limits, keep removing items from cache until we can create this
ctex->texture = attemptTexture(graphicsFile(filename));
if(!ctex->texture){ if(!ctex->texture){
for(map<string,WCachedTexture*>::iterator it=textureCache.begin();it!=textureCache.end();it++) for(map<string,WCachedTexture*>::iterator it=textureCache.begin();it!=textureCache.end();it++)
@@ -263,6 +264,32 @@ WCachedTexture * WResourceManager::getCachedTexture(string filename, bool makene
return ctex; return ctex;
} }
JTexture * WResourceManager::attemptTexture(string filename, int mode, int format){
JTexture * result = JRenderer::GetInstance()->LoadTexture(filename.c_str(),mode,format);
if(result == NULL){
if(!fileExists(filename.c_str()))
return NULL;
for(int attempt=0;attempt<10;attempt++){
if(!RemoveOldestTexture())
break;
result = JRenderer::GetInstance()->LoadTexture(filename.c_str(),mode,format);
if(result)
break;
}
//Still no result, so clear cache entirely, then try again.
if(!result){
ClearUnlocked();
result = JRenderer::GetInstance()->LoadTexture(filename.c_str(),mode,format);
}
}
return result;
}
WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool makenew){ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool makenew){
string filename = card->getImageName(); string filename = card->getImageName();
if(type == CACHE_THUMB) if(type == CACHE_THUMB)
@@ -273,14 +300,16 @@ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool
return NULL; //We've found a cache miss, so return null. return NULL; //We've found a cache miss, so return null.
WCachedTexture * ctex = textureCache[filename]; WCachedTexture * ctex = textureCache[filename];
//Failed to cache it! //Failed to find it in cache!
if(!ctex && makenew){ if(!ctex && makenew){
//Space in cache, make new texture
if(cleanup()){ if(cleanup()){
//Space in cache, make new texture
ctex = NEW WCachedTexture(); ctex = NEW WCachedTexture();
string cardfile = cardFile(filename,card->getSetName()); string cfile = cardFile(filename,card->getSetName());
if(cardfile != "") if(cfile != ""){
ctex->texture = JRenderer::GetInstance()->LoadTexture(cardfile.c_str()); //Within limits, keep removing items from cache until we can create this
ctex->texture = attemptTexture(cfile);
}
else else
ctex->texture = NULL; ctex->texture = NULL;