2002-03-30 20:00:13 +00:00
|
|
|
#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.
|
2002-03-30 20:00:13 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// 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
|
|
|
|
2002-03-30 20:00:13 +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-03-30 20:00:13 +00:00
|
|
|
{
|
2002-09-07 08:20:10 +00:00
|
|
|
Font* pFont = i->second;
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "FONT LEAK: '%s', RefCount = %d.", i->first.GetString(), pFont->m_iRefCount );
|
2002-03-30 20:00:13 +00:00
|
|
|
SAFE_DELETE( pFont );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Load/Unload textures from disk
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-08-13 23:26:46 +00:00
|
|
|
Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
2002-08-13 23:26:46 +00:00
|
|
|
sFontOrTextureFilePath.MakeLower();
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "FontManager::LoadFont(%s).", sFontFilePath.GetString() );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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()) {
|
2002-10-29 07:58:44 +00:00
|
|
|
// 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;
|
2002-03-30 20:00:13 +00:00
|
|
|
pFont->m_iRefCount++;
|
|
|
|
|
}
|
|
|
|
|
else // the texture is not already loaded
|
|
|
|
|
{
|
|
|
|
|
CString sDrive, sDir, sFName, sExt;
|
2002-09-07 05:04:04 +00:00
|
|
|
splitpath( false, sFontOrTextureFilePath, sDrive, sDir, sFName, sExt );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-08-13 23:26:46 +00:00
|
|
|
if( sChars == "" )
|
|
|
|
|
pFont = (Font*) new Font( sFontOrTextureFilePath );
|
|
|
|
|
else
|
|
|
|
|
pFont = (Font*) new Font( sFontOrTextureFilePath, sChars );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath.GetString());
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
m_mapPathToFont[sFontOrTextureFilePath] = pFont;
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FontManager::UnloadFont( CString sFontFilePath )
|
|
|
|
|
{
|
|
|
|
|
sFontFilePath.MakeLower();
|
|
|
|
|
|
2002-10-29 07:58:44 +00:00
|
|
|
// LOG->Trace( "FontManager::UnloadTexture(%s).", sFontFilePath.GetString() );
|
2002-03-30 20:00:13 +00:00
|
|
|
|
|
|
|
|
if( sFontFilePath == "" )
|
|
|
|
|
{
|
2002-08-03 18:40:09 +00:00
|
|
|
// LOG->Trace( "FontManager::UnloadTexture(): tried to Unload a blank" );
|
2002-03-30 20:00:13 +00:00
|
|
|
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-03-30 20:00:13 +00:00
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
pFont=p->second;
|
|
|
|
|
pFont->m_iRefCount--;
|
|
|
|
|
if( pFont->m_iRefCount != 0 )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
// 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-03-30 20:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
2002-09-07 08:20:10 +00:00
|
|
|
// There are no more references to this texture.
|
2002-10-29 07:58:44 +00:00
|
|
|
// 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
|
2002-03-30 20:00:13 +00:00
|
|
|
}
|