Jeck - WResourceManager now tracks filesystem misses for cards (up to MAX_CACHE_OBJECTS) and sfx, so disk use is much reduced.
This commit is contained in:
@@ -81,6 +81,8 @@ public:
|
|||||||
void Release(JQuad * quad);
|
void Release(JQuad * quad);
|
||||||
void Release(JSample * sample);
|
void Release(JSample * sample);
|
||||||
|
|
||||||
|
void ClearMisses();
|
||||||
|
|
||||||
unsigned int nowTime();
|
unsigned int nowTime();
|
||||||
|
|
||||||
//Our file redirect system.
|
//Our file redirect system.
|
||||||
@@ -101,7 +103,6 @@ public:
|
|||||||
|
|
||||||
//Wrapped from JSoundSystem. TODO: Privatize.
|
//Wrapped from JSoundSystem. TODO: Privatize.
|
||||||
JMusic * ssLoadMusic(const char *fileName);
|
JMusic * ssLoadMusic(const char *fileName);
|
||||||
JSample * ssLoadSample(const char *fileName);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool RemoveOldestTexture();
|
bool RemoveOldestTexture();
|
||||||
|
|||||||
@@ -114,12 +114,34 @@ WCachedSample::~WCachedSample(){
|
|||||||
JSample * WCachedSample::GetSample(){
|
JSample * WCachedSample::GetSample(){
|
||||||
return sample;
|
return sample;
|
||||||
}
|
}
|
||||||
|
void WResourceManager::ClearMisses(){
|
||||||
|
map<string,WCachedTexture*>::iterator itTex, nextTex;
|
||||||
|
map<string,WCachedSample*>::iterator itSfx, nextSfx;
|
||||||
|
|
||||||
|
for(itTex = textureCache.begin(); itTex != textureCache.end();itTex=nextTex){
|
||||||
|
nextTex = itTex;
|
||||||
|
nextTex++;
|
||||||
|
|
||||||
|
if(itTex->second == NULL){
|
||||||
|
textureCache.erase(itTex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(itSfx = sampleCache.begin(); itSfx != sampleCache.end();itSfx=nextSfx){
|
||||||
|
nextSfx = itSfx;
|
||||||
|
nextSfx++;
|
||||||
|
|
||||||
|
if(itSfx->second == NULL){
|
||||||
|
sampleCache.erase(itSfx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool WResourceManager::cleanup(){
|
bool WResourceManager::cleanup(){
|
||||||
int maxSize = options[Options::CACHESIZE].number * 100000;
|
int maxSize = options[Options::CACHESIZE].number * 100000;
|
||||||
if (!maxSize) maxSize = CACHE_SIZE_PIXELS;
|
if (!maxSize) maxSize = CACHE_SIZE_PIXELS;
|
||||||
|
|
||||||
while (textureCache.size() > MAX_CACHE_OBJECTS - 1 || totalsize > maxSize){
|
while (textureCache.size() > MAX_CACHE_OBJECTS - 1 || totalsize > maxSize){
|
||||||
|
ClearMisses();
|
||||||
int result = RemoveOldestTexture();
|
int result = RemoveOldestTexture();
|
||||||
if (!result) return false;
|
if (!result) return false;
|
||||||
}
|
}
|
||||||
@@ -141,6 +163,10 @@ void WResourceManager::clearSamples(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
WCachedSample * WResourceManager::getCachedSample(string filename, bool makenew){
|
WCachedSample * WResourceManager::getCachedSample(string filename, bool makenew){
|
||||||
|
map<string,WCachedSample*>::iterator miss = sampleCache.find(filename);
|
||||||
|
if(miss != sampleCache.end() && miss->second == NULL)
|
||||||
|
return NULL; //We've found a cache miss, so return null.
|
||||||
|
|
||||||
WCachedSample * csample = sampleCache[filename];
|
WCachedSample * csample = sampleCache[filename];
|
||||||
|
|
||||||
//Failed to cache it!
|
//Failed to cache it!
|
||||||
@@ -148,22 +174,24 @@ WCachedSample * WResourceManager::getCachedSample(string filename, bool makenew)
|
|||||||
//Space in cache, make new sample
|
//Space in cache, make new sample
|
||||||
if(cleanup()){
|
if(cleanup()){
|
||||||
csample = NEW WCachedSample();
|
csample = NEW WCachedSample();
|
||||||
csample->sample = ssLoadSample(filename.c_str());
|
string sfile = sfxFile(filename);
|
||||||
//Clean the cache
|
if(sfile != ""){
|
||||||
if(!csample->sample && fileExists(sfxFile(filename).c_str())){
|
csample->sample = JSoundSystem::GetInstance()->LoadSample(sfile.c_str());
|
||||||
clearSamples();
|
//Potential cache overflow- clean the cache
|
||||||
csample->sample = ssLoadSample(filename.c_str());
|
if(!csample->sample && fileExists(sfile.c_str())){
|
||||||
}
|
clearSamples();
|
||||||
//Failed.
|
csample->sample = JSoundSystem::GetInstance()->LoadSample(sfile.c_str());
|
||||||
if(!csample->sample){
|
|
||||||
for(map<string,WCachedSample*>::iterator it=sampleCache.begin();it!=sampleCache.end();it++)
|
|
||||||
if(it->second == NULL){
|
|
||||||
sampleCache.erase(it);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
csample->sample = NULL;
|
||||||
|
|
||||||
|
//Failed. Leave the cache miss alone.
|
||||||
|
if(!csample->sample){
|
||||||
SAFE_DELETE(csample);
|
SAFE_DELETE(csample);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
//Success!
|
||||||
csample->hit();
|
csample->hit();
|
||||||
sampleCache[filename] = csample;
|
sampleCache[filename] = csample;
|
||||||
}
|
}
|
||||||
@@ -185,7 +213,7 @@ WCachedTexture * WResourceManager::getCachedTexture(string filename, bool makene
|
|||||||
|
|
||||||
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++)
|
||||||
if(it->second == NULL){
|
if(it->first == filename){
|
||||||
textureCache.erase(it);
|
textureCache.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -208,6 +236,10 @@ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool
|
|||||||
if(type == CACHE_THUMB)
|
if(type == CACHE_THUMB)
|
||||||
filename = "thumbnails/"+filename;
|
filename = "thumbnails/"+filename;
|
||||||
|
|
||||||
|
map<string,WCachedTexture*>::iterator miss = textureCache.find(filename);
|
||||||
|
if(miss != textureCache.end() && miss->second == 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 cache it!
|
||||||
if(!ctex && makenew){
|
if(!ctex && makenew){
|
||||||
@@ -220,13 +252,8 @@ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool
|
|||||||
else
|
else
|
||||||
ctex->texture = NULL;
|
ctex->texture = NULL;
|
||||||
|
|
||||||
//Couldn't create texture, so fail.
|
//Couldn't create texture, so fail. Leave failure in cache, so we don't try again later.
|
||||||
if(!ctex->texture){
|
if(!ctex->texture){
|
||||||
for(map<string,WCachedTexture*>::iterator it=textureCache.begin();it!=textureCache.end();it++)
|
|
||||||
if(it->second == NULL){
|
|
||||||
textureCache.erase(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
SAFE_DELETE(ctex);
|
SAFE_DELETE(ctex);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -422,7 +449,7 @@ JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY,
|
|||||||
map<string,WCachedTexture*>::iterator it = textureCache.end();
|
map<string,WCachedTexture*>::iterator it = textureCache.end();
|
||||||
tc = textureCache[filename];
|
tc = textureCache[filename];
|
||||||
for(it = textureCache.begin();it!=textureCache.end();it++){
|
for(it = textureCache.begin();it!=textureCache.end();it++){
|
||||||
if(it->second == tc)
|
if(it->first == filename)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,12 +548,12 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style){
|
|||||||
if(style == RETRIEVE_LOCK || style == RETRIEVE_VRAM) tc->lock();
|
if(style == RETRIEVE_LOCK || style == RETRIEVE_VRAM) tc->lock();
|
||||||
else if(style == RETRIEVE_UNLOCK) tc->unlock();
|
else if(style == RETRIEVE_UNLOCK) tc->unlock();
|
||||||
}
|
}
|
||||||
else{
|
else if(style == RETRIEVE_MANAGE){
|
||||||
//Remove cache hit from cache
|
//Remove cache hit from cache
|
||||||
tc = textureCache[filename];
|
tc = textureCache[filename];
|
||||||
map<string,WCachedTexture*>::iterator it = textureCache.end();
|
map<string,WCachedTexture*>::iterator it = textureCache.end();
|
||||||
for(it = textureCache.begin();it!=textureCache.end();it++){
|
for(it = textureCache.begin();it!=textureCache.end();it++){
|
||||||
if(it->second == tc)
|
if(it->first == filename)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -540,7 +567,7 @@ JTexture * WResourceManager::RetrieveTexture(string filename, int style){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Texture exists! Get it.
|
//Texture exists! Get it.
|
||||||
if(tc->texture != NULL){
|
if(tc && tc->texture != NULL){
|
||||||
tc->hit();
|
tc->hit();
|
||||||
return tc->GetTexture();
|
return tc->GetTexture();
|
||||||
}
|
}
|
||||||
@@ -569,12 +596,12 @@ JSample * WResourceManager::RetrieveSample(string filename, int style){
|
|||||||
if(style == RETRIEVE_LOCK || style == RETRIEVE_VRAM) tc->lock();
|
if(style == RETRIEVE_LOCK || style == RETRIEVE_VRAM) tc->lock();
|
||||||
else if(style == RETRIEVE_UNLOCK) tc->unlock();
|
else if(style == RETRIEVE_UNLOCK) tc->unlock();
|
||||||
}
|
}
|
||||||
else{
|
else if(style == RETRIEVE_MANAGE){
|
||||||
//Remove cache hit from cache
|
//Remove cache hit from cache
|
||||||
tc = sampleCache[filename];
|
tc = sampleCache[filename];
|
||||||
map<string,WCachedSample*>::iterator it;
|
map<string,WCachedSample*>::iterator it;
|
||||||
for(it = sampleCache.begin();it!=sampleCache.end();it++){
|
for(it = sampleCache.begin();it!=sampleCache.end();it++){
|
||||||
if(it->second == tc)
|
if(it->first == filename)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,7 +615,7 @@ JSample * WResourceManager::RetrieveSample(string filename, int style){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Sample exists! Get it.
|
//Sample exists! Get it.
|
||||||
if(tc->sample != NULL){
|
if(tc && tc->sample != NULL){
|
||||||
tc->hit();
|
tc->hit();
|
||||||
return tc->GetSample();
|
return tc->GetSample();
|
||||||
}
|
}
|
||||||
@@ -774,7 +801,7 @@ string WResourceManager::sfxFile(const string filename, const string specific){
|
|||||||
return defdir;
|
return defdir;
|
||||||
|
|
||||||
//Complete abject failure. Probably a crash...
|
//Complete abject failure. Probably a crash...
|
||||||
return defdir;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
int WResourceManager::fileOK(string filename, bool relative){
|
int WResourceManager::fileOK(string filename, bool relative){
|
||||||
@@ -887,9 +914,6 @@ int WResourceManager::LoadSample(const string &sampleName){
|
|||||||
JMusic * WResourceManager::ssLoadMusic(const char *fileName){
|
JMusic * WResourceManager::ssLoadMusic(const char *fileName){
|
||||||
return JSoundSystem::GetInstance()->LoadMusic(musicFile(fileName).c_str());
|
return JSoundSystem::GetInstance()->LoadMusic(musicFile(fileName).c_str());
|
||||||
}
|
}
|
||||||
JSample * WResourceManager::ssLoadSample(const char *fileName){
|
|
||||||
return JSoundSystem::GetInstance()->LoadSample(sfxFile(fileName).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Unmodified from JResourceManager
|
//Unmodified from JResourceManager
|
||||||
|
|||||||
Reference in New Issue
Block a user