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 "MTGCard.h"
#ifdef WIN32
#define CACHE_SIZE_PIXELS 20000000
#define MAX_CACHE_OBJECTS 2000
#else
#define CACHE_SIZE_PIXELS 2000000
#define MAX_CACHE_OBJECTS 200
#endif
class WCachedResource{
public:
@@ -111,8 +117,11 @@ public:
private:
bool RemoveOldestTexture();
bool RemoveOldestSample();
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 * getCachedCard(MTGCard * card, int type = CACHE_CARD, bool makenew = true);
WCachedSample * getCachedSample(string filename, bool makenew = true);
+36 -7
View File
@@ -238,7 +238,8 @@ WCachedTexture * WResourceManager::getCachedTexture(string filename, bool makene
//Space in cache, make new texture
if(cleanup()){
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){
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;
}
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){
string filename = card->getImageName();
if(type == CACHE_THUMB)
@@ -273,17 +300,19 @@ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool
return NULL; //We've found a cache miss, so return null.
WCachedTexture * ctex = textureCache[filename];
//Failed to cache it!
//Failed to find it in cache!
if(!ctex && makenew){
//Space in cache, make new texture
if(cleanup()){
//Space in cache, make new texture
ctex = NEW WCachedTexture();
string cardfile = cardFile(filename,card->getSetName());
if(cardfile != "")
ctex->texture = JRenderer::GetInstance()->LoadTexture(cardfile.c_str());
string cfile = cardFile(filename,card->getSetName());
if(cfile != ""){
//Within limits, keep removing items from cache until we can create this
ctex->texture = attemptTexture(cfile);
}
else
ctex->texture = NULL;
//Couldn't create texture, so fail. Leave failure in cache, so we don't try again later.
if(!ctex->texture){
SAFE_DELETE(ctex);