Switch remaining CMaps to std::map.

This commit is contained in:
Glenn Maynard
2002-09-07 08:20:10 +00:00
parent 5041ee2159
commit 2e3cde821b
4 changed files with 55 additions and 83 deletions
+19 -34
View File
@@ -32,19 +32,13 @@ FontManager::FontManager()
FontManager::~FontManager() FontManager::~FontManager()
{ {
// delete all textures for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
POSITION pos = m_mapPathToFont.GetStartPosition(); i != m_mapPathToFont.end(); ++i)
CString sFontFilePath;
Font* pFont;
while( pos != NULL ) // iterate over all k/v pairs in map
{ {
m_mapPathToFont.GetNextAssoc( pos, sFontFilePath, pFont ); Font* pFont = i->second;
LOG->Trace( "FONT LEAK: '%s', RefCount = %d.", sFontFilePath, pFont->m_iRefCount ); LOG->Trace( "FONT LEAK: '%s', RefCount = %d.", i->first, pFont->m_iRefCount );
SAFE_DELETE( pFont ); SAFE_DELETE( pFont );
} }
m_mapPathToFont.RemoveAll();
} }
@@ -65,9 +59,10 @@ Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
Font* pFont = NULL; Font* pFont = NULL;
if( m_mapPathToFont.Lookup( sFontOrTextureFilePath, pFont ) ) // if the texture already exists in the map std::map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontOrTextureFilePath);
{ if(p != m_mapPathToFont.end()) {
// LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontFilePath, pFont->m_iRefCount) ); // LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontFilePath, pFont->m_iRefCount) );
pFont=p->second;
pFont->m_iRefCount++; pFont->m_iRefCount++;
} }
else // the texture is not already loaded else // the texture is not already loaded
@@ -82,7 +77,7 @@ Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
// LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath); // LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath);
m_mapPathToFont.SetAt( sFontOrTextureFilePath, pFont ); m_mapPathToFont[sFontOrTextureFilePath] = pFont;
} }
return pFont; return pFont;
@@ -93,12 +88,7 @@ bool FontManager::IsFontLoaded( CString sFontFilePath )
{ {
sFontFilePath.MakeLower(); sFontFilePath.MakeLower();
Font* pFont; return m_mapPathToFont.find(sFontFilePath) != m_mapPathToFont.end();
if( m_mapPathToFont.Lookup( sFontFilePath, pFont ) ) // if the texture exists in the map
return true;
else
return false;
} }
void FontManager::UnloadFont( CString sFontFilePath ) void FontManager::UnloadFont( CString sFontFilePath )
@@ -114,25 +104,20 @@ void FontManager::UnloadFont( CString sFontFilePath )
} }
Font* pFont; Font* pFont;
std::map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontFilePath);
if(p == m_mapPathToFont.end())
throw RageException( ssprintf("Tried to Unload a font that wasn't loaded. '%s'", sFontFilePath) );
if( m_mapPathToFont.Lookup( sFontFilePath, pFont ) ) // if the texture exists in the map pFont=p->second;
{
pFont->m_iRefCount--; pFont->m_iRefCount--;
if( pFont->m_iRefCount == 0 ) // there are no more references to this texture if( pFont->m_iRefCount != 0 )
{
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath, pFont->m_iRefCount );
SAFE_DELETE( pFont ); // free the texture
m_mapPathToFont.RemoveKey( sFontFilePath ); // and remove the key in the map
}
else
{ {
// LOG->Trace( ssprintf("FontManager: '%s' will not be deleted. It still has %d references.", sFontFilePath, pFont->m_iRefCount) ); // LOG->Trace( ssprintf("FontManager: '%s' will not be deleted. It still has %d references.", sFontFilePath, pFont->m_iRefCount) );
return;
} }
} // There are no more references to this texture.
else // lookup failed // LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath, pFont->m_iRefCount );
{ SAFE_DELETE( pFont ); // free the texture
throw RageException( ssprintf("Tried to Unload a font that wasn't loaded. '%s'", sFontFilePath) ); m_mapPathToFont.erase( p ); // and remove the key in the map
}
} }
+2 -1
View File
@@ -12,6 +12,7 @@
#include "Font.h" #include "Font.h"
#include <map>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// FontManager Class Declarations // FontManager Class Declarations
@@ -28,7 +29,7 @@ public:
protected: protected:
// map from file name to a texture holder // map from file name to a texture holder
CTypedPtrMap<CMapStringToPtr, CString, Font*> m_mapPathToFont; std::map<CString, Font*> m_mapPathToFont;
}; };
extern FontManager* FONT; // global and accessable from anywhere in our program extern FontManager* FONT; // global and accessable from anywhere in our program
+26 -41
View File
@@ -21,7 +21,6 @@
#include "RageLog.h" #include "RageLog.h"
#include "RageException.h" #include "RageException.h"
RageTextureManager* TEXTUREMAN = NULL; RageTextureManager* TEXTUREMAN = NULL;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -37,20 +36,13 @@ RageTextureManager::RageTextureManager( RageDisplay* pScreen )
RageTextureManager::~RageTextureManager() RageTextureManager::~RageTextureManager()
{ {
// delete all textures for( std::map<CString, RageTexture*>::iterator i = m_mapPathToTexture.begin();
POSITION pos = m_mapPathToTexture.GetStartPosition(); i != m_mapPathToTexture.end(); ++i)
while( pos != NULL ) // iterate over all k/v pairs in map
{ {
RageTexture* pTexture; RageTexture* pTexture = i->second;
CString sPath; LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", i->first, pTexture->m_iRefCount );
m_mapPathToTexture.GetNextAssoc( pos, sPath, pTexture );
LOG->Trace( "TEXTUREMAN LEAK: '%s', RefCount = %d.", sPath, pTexture->m_iRefCount );
SAFE_DELETE( pTexture ); SAFE_DELETE( pTexture );
} }
m_mapPathToTexture.RemoveAll();
} }
@@ -71,8 +63,10 @@ RageTexture* RageTextureManager::LoadTexture( CString sTexturePath, bool bForceR
// of the same bitmap if there are equivalent but different paths // of the same bitmap if there are equivalent but different paths
// (e.g. "Bitmaps\me.bmp" and "..\Rage PC Edition\Bitmaps\me.bmp" ). // (e.g. "Bitmaps\me.bmp" and "..\Rage PC Edition\Bitmaps\me.bmp" ).
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture already exists in the map std::map<CString, RageTexture*>::iterator p = m_mapPathToTexture.find(sTexturePath);
{ if(p != m_mapPathToTexture.end()) {
pTexture = p->second;
pTexture->m_iRefCount++; pTexture->m_iRefCount++;
if( bForceReload ) if( bForceReload )
pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch ); pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
@@ -93,7 +87,7 @@ RageTexture* RageTextureManager::LoadTexture( CString sTexturePath, bool bForceR
LOG->Trace( "RageTextureManager: Finished loading '%s'.", sTexturePath ); LOG->Trace( "RageTextureManager: Finished loading '%s'.", sTexturePath );
m_mapPathToTexture.SetAt( sTexturePath, pTexture ); m_mapPathToTexture[sTexturePath] = pTexture;
} }
// LOG->Trace( "Display: %.2f KB video memory left", DISPLAY->GetDevice()->GetAvailableTextureMem()/1000000.0f ); // LOG->Trace( "Display: %.2f KB video memory left", DISPLAY->GetDevice()->GetAvailableTextureMem()/1000000.0f );
@@ -106,12 +100,7 @@ bool RageTextureManager::IsTextureLoaded( CString sTexturePath )
{ {
sTexturePath.MakeLower(); sTexturePath.MakeLower();
RageTexture* pTexture; return m_mapPathToTexture.find(sTexturePath) != m_mapPathToTexture.end();
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture exists in the map
return true;
else
return false;
} }
void RageTextureManager::UnloadTexture( CString sTexturePath ) void RageTextureManager::UnloadTexture( CString sTexturePath )
@@ -128,36 +117,32 @@ void RageTextureManager::UnloadTexture( CString sTexturePath )
RageTexture* pTexture; RageTexture* pTexture;
if( m_mapPathToTexture.Lookup( sTexturePath, pTexture ) ) // if the texture exists in the map std::map<CString, RageTexture*>::iterator p = m_mapPathToTexture.find(sTexturePath);
{ if(p == m_mapPathToTexture.end())
throw RageException( "Tried to Unload texture '%s' that wasn't loaded.", sTexturePath );
pTexture = p->second;
pTexture->m_iRefCount--; pTexture->m_iRefCount--;
if( pTexture->m_iRefCount == 0 ) // there are no more references to this texture if( pTexture->m_iRefCount != 0 )
{
// LOG->Trace( "RageTextureManager: '%s' will be deleted. It has %d references.", sTexturePath, pTexture->m_iRefCount );
SAFE_DELETE( pTexture ); // free the texture
m_mapPathToTexture.RemoveKey( sTexturePath ); // and remove the key in the map
}
else
{ {
// LOG->Trace( "RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath, pTexture->m_iRefCount ); // LOG->Trace( "RageTextureManager: '%s' will not be deleted. It still has %d references.", sTexturePath, pTexture->m_iRefCount );
} return;
}
else // texture not found
{
throw RageException( "Tried to Unload texture '%s' that wasn't loaded.", sTexturePath );
} }
// There are no more references to this texture.
// LOG->Trace( "RageTextureManager: '%s' will be deleted. It has %d references.", sTexturePath, pTexture->m_iRefCount );
SAFE_DELETE( pTexture ); // free the texture
m_mapPathToTexture.erase(p); // and remove the key in the map
} }
void RageTextureManager::ReloadAll() void RageTextureManager::ReloadAll()
{ {
for( POSITION pos = m_mapPathToTexture.GetStartPosition(); pos != NULL; ) for( std::map<CString, RageTexture*>::iterator i = m_mapPathToTexture.begin();
i != m_mapPathToTexture.end(); ++i)
{ {
CString sPath; RageTexture* pTexture = i->second;
RageTexture* pTexture;
m_mapPathToTexture.GetNextAssoc( pos, sPath, pTexture ); // this is not entirely correct. Hints are lost!
pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, 0 );
pTexture->Reload( m_iMaxTextureSize, m_iTextureColorDepth, 0 ); // this not entirely correct. Hints are lost!
} }
} }
+2 -1
View File
@@ -11,6 +11,7 @@
*/ */
#include "RageTexture.h" #include "RageTexture.h"
#include <map>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// RageTextureManager Class Declarations // RageTextureManager Class Declarations
@@ -45,7 +46,7 @@ protected:
DWORD m_iTextureColorDepth; DWORD m_iTextureColorDepth;
// map from file name to a texture holder // map from file name to a texture holder
CTypedPtrMap<CMapStringToPtr, CString, RageTexture*> m_mapPathToTexture; std::map<CString, RageTexture*> m_mapPathToTexture;
}; };
extern RageTextureManager* TEXTUREMAN; // global and accessable from anywhere in our program extern RageTextureManager* TEXTUREMAN; // global and accessable from anywhere in our program