diff --git a/projects/mtg/include/WResourceManager.h b/projects/mtg/include/WResourceManager.h index 446d4a03c..dad70d1bd 100644 --- a/projects/mtg/include/WResourceManager.h +++ b/projects/mtg/include/WResourceManager.h @@ -36,6 +36,7 @@ public: bool ReleaseQuad(JQuad* quad); //We're done with this quad, so delete and stop tracking. True if existed. protected: JTexture * texture; + bool bVRAM; vector trackedQuads; }; @@ -82,6 +83,8 @@ public: void Release(JSample * sample); void ClearMisses(); + void ClearUnlocked(); + void Refresh(); //Refreshes all files in cache, for when mode/profile changes. unsigned int nowTime(); diff --git a/projects/mtg/src/OptionItem.cpp b/projects/mtg/src/OptionItem.cpp index 0d26be1a8..f2a0f690b 100644 --- a/projects/mtg/src/OptionItem.cpp +++ b/projects/mtg/src/OptionItem.cpp @@ -319,6 +319,7 @@ void OptionProfile::acceptSubmode() options[Options::ACTIVE_PROFILE] = selections[value]; initialValue = value; populate(); + resources.Refresh(); //Update images, in case we've changed profiles, etc. bCheck = false; } diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index 73c75bc29..ca565e69b 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -39,6 +39,7 @@ void WCachedResource::hit(){ WCachedTexture::WCachedTexture(){ texture = NULL; + bVRAM = false; } WCachedTexture::~WCachedTexture(){ @@ -136,6 +137,28 @@ void WResourceManager::ClearMisses(){ } } +void WResourceManager::ClearUnlocked(){ + map::iterator itTex, nextTex; + map::iterator itSfx, nextSfx; + + for(itTex = textureCache.begin(); itTex != textureCache.end();itTex=nextTex){ + nextTex = itTex; + nextTex++; + + if(!itTex->second || (itTex->second && !itTex->second->isLocked())){ + textureCache.erase(itTex); + } + } + for(itSfx = sampleCache.begin(); itSfx != sampleCache.end();itSfx=nextSfx){ + nextSfx = itSfx; + nextSfx++; + + if(!itSfx->second || (itSfx->second && !itSfx->second->isLocked())){ + sampleCache.erase(itSfx); + } + } +} + bool WResourceManager::cleanup(){ int maxSize = options[Options::CACHESIZE].number * 100000; if (!maxSize) maxSize = CACHE_SIZE_PIXELS; @@ -475,6 +498,8 @@ JQuad * WResourceManager::RetrieveQuad(string filename, float offX, float offY, //Texture exists! Get quad. if(tc && tc->texture != NULL){ + if(style == RETRIEVE_VRAM) + tc->bVRAM = true; tc->hit(); return tc->GetQuad(offX,offY,width,height); } @@ -962,3 +987,102 @@ int WResourceManager::CreateQuad(const string &quadName, const string &textureNa else return itr->second; } + +void WResourceManager::Refresh(){ + map::iterator it; + vector::iterator q; + JTexture * oldtex; + + ClearMisses(); + + for(it = textureCache.begin();it!=textureCache.end();it++){ + if(it->second == NULL) + continue; + + oldtex = it->second->texture; + + //Reload the texture. + if(it->second->bVRAM) + it->second->texture = JRenderer::GetInstance()->LoadTexture(graphicsFile(it->first).c_str(),TEX_TYPE_USE_VRAM,TEXTURE_FORMAT); + else + it->second->texture = JRenderer::GetInstance()->LoadTexture(graphicsFile(it->first).c_str(),0,TEXTURE_FORMAT); + + //This texture doesn't exist in our current theme. Either use the old one, or record cache miss. + if(!it->second->texture){ + if(it->second->isLocked()) + it->second->texture = oldtex; + else{ + SAFE_DELETE(oldtex); + SAFE_DELETE(it->second); + } + continue; + } + + //Relink quads to new texture. + for(q = it->second->trackedQuads.begin(); q != it->second->trackedQuads.end(); q++){ + (*q)->mTex = it->second->texture; + } + } + + //Now do some juggling so that managed resources also reload. + map oldTextures; + map newNames; + map::iterator oldIt; + vector::iterator jtex; + map::iterator mapping; + JTexture * newtex; + + //Store old mappings. + for(mapping = mTextureMap.begin();mapping != mTextureMap.end();mapping++){ + if(oldTextures[mTextureList[mapping->second]] == NULL){ + newtex = JRenderer::GetInstance()->LoadTexture(graphicsFile(mapping->first).c_str(),0,TEXTURE_FORMAT); + oldtex = mTextureList[mapping->second]; + if(!newtex) + newNames[oldtex] = mapping->first; + else{ + newNames[newtex] = mapping->first; + JRenderer::GetInstance()->BindTexture(newtex); + } + + oldTextures[oldtex] = newtex; + } + } + + //Remap quads. + for(q = mQuadList.begin();q!=mQuadList.end();q++){ + newtex = oldTextures[(*q)->mTex]; + if(newtex != NULL) + (*q)->mTex = newtex; + } + + //Rebuild mTextureList and mapping. + mTextureList.clear(); + mTextureMap.clear(); + int x = 0; + for(oldIt = oldTextures.begin();oldIt!=oldTextures.end();oldIt++){ + + if(oldIt->second) + newtex = oldIt->second; + else + newtex = oldIt->first; + + mTextureList.push_back(newtex); + mTextureMap[newNames[newtex]] = x; + x++; + } + + //Rebuild mapping. + for(mapping = mTextureMap.begin();mapping != mTextureMap.end();mapping++){ + if(oldTextures[mTextureList[mapping->second]] == NULL) + continue; + } + + //Delete unused textures. + for(oldIt = oldTextures.begin();oldIt!=oldTextures.end();oldIt++){ + if(!oldIt->second || !oldIt->first ) + continue; + + oldtex = oldIt->first; + SAFE_DELETE(oldtex); + } +} \ No newline at end of file