Jeck - It wasn't emptying the cache properly, so this is a step in the right direction. I'll keep looking for more issues, though.

This commit is contained in:
wagic.jeck
2009-09-03 18:21:57 +00:00
parent 3db0969c0e
commit 8340fdbd5b
4 changed files with 45 additions and 30 deletions

View File

@@ -7,6 +7,7 @@
#include "MTGCard.h"
#define CACHE_SIZE_PIXELS 2000000
#define MAX_CACHE_OBJECTS 100
class WCachedResource{
public:
@@ -106,6 +107,7 @@ private:
bool RemoveOldestTexture();
bool RemoveOldestSample();
bool cleanup();
void clearSamples();
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);

View File

@@ -105,6 +105,7 @@ JQuad * CardGui::alternateThumbQuad(MTGCard * card){
}
if(q && q->mTex)
q->SetHotSpot(q->mTex->mWidth/2,q->mTex->mHeight/2);
else q = q;
return q;
}
@@ -127,7 +128,7 @@ void CardGui::alternateRender(MTGCard * card, const Pos& pos){
float scale = pos.actZ * 250 / q->mHeight;
q->SetColor(ARGB((int)pos.actA,255,255,255));
renderer->RenderQuad(q, pos.actX, pos.actY, pos.actT, scale, scale);
}
}else q = q;
// Write the title
JLBFont * font = resources.GetJLBFont("magic");
float backup_scale = font->GetScale();

View File

@@ -142,8 +142,8 @@ void GameStateMenu::Start(){
bgTexture = resources.RetrieveTexture("menutitle.png", RETRIEVE_VRAM);
movingWTexture = resources.RetrieveTexture("movingW.png", RETRIEVE_VRAM);
mBg = resources.RetrieveQuad("menutitle.png"); // Create background quad for rendering.
mMovingW = resources.RetrieveQuad("movingW.png");
mBg = resources.RetrieveQuad("menutitle.png", 0, 0, 256, 166); // Create background quad for rendering.
mMovingW = resources.RetrieveQuad("movingW.png", 2, 2, 84, 62);
mBg->SetHotSpot(105,50);
mMovingW->SetHotSpot(72,16);

View File

@@ -52,7 +52,6 @@ JTexture * WCachedTexture::GetTexture(){
}
bool WCachedTexture::ReleaseQuad(JQuad* quad){
//SAFE_DELETE(texture); WTF?
for(vector<JQuad*>::iterator i = trackedQuads.begin();i!=trackedQuads.end();i++){
if((*i) == quad){
SAFE_DELETE(quad);
@@ -119,7 +118,8 @@ JSample * WCachedSample::GetSample(){
bool WResourceManager::cleanup(){
int maxSize = options[Options::CACHESIZE].number * 100000;
if (!maxSize) maxSize = CACHE_SIZE_PIXELS;
while (totalsize > maxSize){
while (textureCache.size() > MAX_CACHE_OBJECTS - 1 || totalsize > maxSize){
int result = RemoveOldestTexture();
if (!result) return false;
}
@@ -134,23 +134,33 @@ unsigned int WResourceManager::nowTime(){
return ++lastTime;
}
void WResourceManager::clearSamples(){
for(map<string,WCachedSample*>::iterator it = sampleCache.begin();it!=sampleCache.end();it++)
SAFE_DELETE(it->second);
sampleCache.clear();
}
WCachedSample * WResourceManager::getCachedSample(string filename, bool makenew){
WCachedSample * csample = sampleCache[filename];
//Failed to cache it!
if(!csample && makenew){
csample = NEW WCachedSample();
//Space in cache, make new sample
if(cleanup()){
csample = NEW WCachedSample();
csample->sample = ssLoadSample(filename.c_str());
//Clean the cache
if(!csample->sample && fileExists(sfxFile(filename).c_str())){
clearSamples();
csample->sample = ssLoadSample(filename.c_str());
}
//Failed.
if(!csample->sample){
for(map<string,WCachedSample*>::iterator it=sampleCache.begin();it!=sampleCache.end();it++)
if(it->second == NULL){
sampleCache.erase(it);
break;
}
if(it->second == NULL){
sampleCache.erase(it);
break;
}
SAFE_DELETE(csample);
return NULL;
}
@@ -168,22 +178,21 @@ WCachedTexture * WResourceManager::getCachedTexture(string filename, bool makene
WCachedTexture * ctex = textureCache[filename];
//Failed to cache it!
if(!ctex && makenew){
ctex = NEW WCachedTexture();
//Space in cache, make new texture
if(cleanup()){
ctex->texture = JRenderer::GetInstance()->LoadTexture(graphicsFile(filename).c_str(),mode,format);
//Couldn't create texture, so fail.
if(!ctex->texture){
ctex = NEW WCachedTexture();
ctex->texture = JRenderer::GetInstance()->LoadTexture(graphicsFile(filename).c_str(),mode,format);
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);
return NULL;
}
if(it->second == NULL){
textureCache.erase(it);
break;
}
SAFE_DELETE(ctex);
return NULL;
}
totalsize+=ctex->texture->mTexHeight *ctex->texture->mTexWidth;
ctex->hit();
textureCache[filename] = ctex;
}
@@ -202,22 +211,22 @@ WCachedTexture * WResourceManager:: getCachedCard(MTGCard * card, int type, bool
WCachedTexture * ctex = textureCache[filename];
//Failed to cache it!
if(!ctex && makenew){
ctex = NEW WCachedTexture();
//Space in cache, make new texture
if(cleanup()){
ctex = NEW WCachedTexture();
ctex->texture = JRenderer::GetInstance()->LoadTexture(cardFile(filename,card->getSetName()).c_str());
//Couldn't create texture, so fail.
if(!ctex->texture){
for(map<string,WCachedTexture*>::iterator it=textureCache.begin();it!=textureCache.end();it++)
if(it->second == NULL){
textureCache.erase(it);
break;
}
if(it->second == NULL){
textureCache.erase(it);
break;
}
SAFE_DELETE(ctex);
return NULL;
}
totalsize+=ctex->texture->mTexHeight *ctex->texture->mTexWidth;
ctex->hit();
textureCache[filename] = ctex;
}
@@ -264,6 +273,8 @@ bool WResourceManager::RemoveOldestTexture(){
}
if(oldest != textureCache.end()){
if(oldest->second && oldest->second->texture)
totalsize-=oldest->second->texture->mTexHeight * oldest->second->texture->mTexWidth;
SAFE_DELETE(oldest->second)
textureCache.erase(oldest);
return true;
@@ -423,6 +434,7 @@ JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY,
return tc->GetQuad(offX,offY,width,height);
}
//Texture doesn't exist, so no quad.
return NULL;
}
void WResourceManager::Release(JTexture * tex){