Files
itgmania212121/stepmania/src/FontManager.cpp
T

124 lines
3.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: FontManager
Desc: Interface for loading and releasing textures.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
-----------------------------------------------------------------------------
*/
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "FontManager.h"
#include "Font.h"
#include "RageUtil.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "RageTimer.h"
2002-06-14 22:25:22 +00:00
FontManager* FONT = NULL;
//-----------------------------------------------------------------------------
// constructor/destructor
//-----------------------------------------------------------------------------
FontManager::FontManager()
{
}
FontManager::~FontManager()
{
2002-09-07 08:20:10 +00:00
for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
2002-09-07 08:20:10 +00:00
Font* pFont = i->second;
LOG->Trace( "FONT LEAK: '%s', RefCount = %d.", i->first.GetString(), pFont->m_iRefCount );
SAFE_DELETE( pFont );
}
}
//-----------------------------------------------------------------------------
// Load/Unload textures from disk
//-----------------------------------------------------------------------------
Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
{
sFontOrTextureFilePath.MakeLower();
// LOG->Trace( "FontManager::LoadFont(%s).", sFontFilePath.GetString() );
// Convert the path to lowercase so that we don't load duplicates.
// Really, this does not solve the duplicate problem. We could have to copies
// of the same bitmap if there are equivalent but different paths
// (e.g. "graphics\blah.png" and "..\stepmania\graphics\blah.png" ).
Font* pFont = NULL;
2002-09-07 08:20:10 +00:00
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.GetString(), pFont->m_iRefCount) );
2002-09-07 08:20:10 +00:00
pFont=p->second;
pFont->m_iRefCount++;
}
else // the texture is not already loaded
{
CString sDrive, sDir, sFName, sExt;
splitpath( false, sFontOrTextureFilePath, sDrive, sDir, sFName, sExt );
if( sChars == "" )
pFont = (Font*) new Font( sFontOrTextureFilePath );
else
pFont = (Font*) new Font( sFontOrTextureFilePath, sChars );
// LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath.GetString());
2002-09-07 08:20:10 +00:00
m_mapPathToFont[sFontOrTextureFilePath] = pFont;
}
return pFont;
}
bool FontManager::IsFontLoaded( CString sFontFilePath )
{
sFontFilePath.MakeLower();
2002-09-07 08:20:10 +00:00
return m_mapPathToFont.find(sFontFilePath) != m_mapPathToFont.end();
}
void FontManager::UnloadFont( CString sFontFilePath )
{
sFontFilePath.MakeLower();
// LOG->Trace( "FontManager::UnloadTexture(%s).", sFontFilePath.GetString() );
if( sFontFilePath == "" )
{
// LOG->Trace( "FontManager::UnloadTexture(): tried to Unload a blank" );
return;
}
Font* pFont;
2002-09-07 08:20:10 +00:00
std::map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontFilePath);
if(p == m_mapPathToFont.end())
2002-12-21 19:32:38 +00:00
RageException::Throw( "Tried to Unload a font that wasn't loaded. '%s'", sFontFilePath.GetString() );
2002-09-07 08:20:10 +00:00
pFont=p->second;
pFont->m_iRefCount--;
if( pFont->m_iRefCount != 0 )
{
// LOG->Trace( "FontManager: '%s' will not be deleted. It still has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
2002-09-07 08:20:10 +00:00
return;
}
2002-09-07 08:20:10 +00:00
// There are no more references to this texture.
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
2002-09-07 08:20:10 +00:00
SAFE_DELETE( pFont ); // free the texture
m_mapPathToFont.erase( p ); // and remove the key in the map
}