Files
itgmania212121/stepmania/src/FontManager.cpp
T

115 lines
3.0 KiB
C++
Raw Normal View History

#include "stdafx.h"
/*
-----------------------------------------------------------------------------
Class: FontManager
2003-01-04 07:55:42 +00:00
Desc: Interface for loading and releasing fonts.
2003-01-04 07:55:42 +00:00
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
Chris Danford
Glenn Maynard
-----------------------------------------------------------------------------
*/
#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;
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 );
delete pFont;
}
2003-01-06 02:50:25 +00:00
}
2003-01-06 02:50:25 +00:00
/* A longchar is at least 32 bits. If c & 0xFF000000, it's a game-custom
2003-01-28 00:45:01 +00:00
* character; game 0 is 0x0100nnnn, game 1 is 0x0200nnnn, and so on. */
longchar FontManager::MakeGameGlyph(wchar_t c, Game g)
2003-01-06 02:50:25 +00:00
{
2003-01-28 00:45:01 +00:00
ASSERT(c >= 0 && c <= 0xFFFF);
ASSERT(g >= 0 && g <= 0xFF);
2003-01-07 04:02:57 +00:00
return longchar (((g+1) << 24) + c);
2003-01-06 02:50:25 +00:00
}
2003-01-28 00:45:01 +00:00
bool FontManager::ExtractGameGlyph(longchar ch, wchar_t &c, Game &g)
2003-01-06 02:50:25 +00:00
{
if((ch & 0xFF000000) == 0) return false; /* not a game glyph */
g = Game((ch >> 24) - 1);
2003-01-28 00:45:01 +00:00
c = wchar_t(ch & 0xFFFF);
2003-01-06 02:50:25 +00:00
return true;
2003-01-05 03:32:41 +00:00
}
void FontManager::ReloadFonts()
{
for(map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
i->second->Reload();
2003-01-05 03:32:41 +00:00
}
}
Font* FontManager::LoadFont( const CString &sFontOrTextureFilePath, CString sChars )
{
// Convert the path to lowercase so that we don't load duplicates.
// Really, this does not solve the duplicate problem. We could have two copies
2003-01-05 03:32:41 +00:00
// of the same bitmap if there are equivalent but different paths
// (e.g. "graphics\blah.png" and "..\stepmania\graphics\blah.png" ).
map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontOrTextureFilePath);
if(p != m_mapPathToFont.end()) {
Font *pFont=p->second;
// LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontOrTextureFilePath.GetString(), pFont->m_iRefCount) );
pFont->m_iRefCount++;
return pFont;
}
/* XXX: This will mismatch cached fonts if we load the same font with two
* different sChars. */
Font *f = new Font;
f->Load(sFontOrTextureFilePath, sChars);
2003-01-05 03:32:41 +00:00
m_mapPathToFont[sFontOrTextureFilePath] = f;
return f;
}
2003-01-04 05:20:40 +00:00
void FontManager::UnloadFont( Font *fp )
{
2003-01-05 03:32:41 +00:00
LOG->Trace( "FontManager::UnloadTexture(%s).", fp->path.GetString() );
2003-01-04 05:20:40 +00:00
for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
i != m_mapPathToFont.end(); ++i)
{
2003-01-04 05:20:40 +00:00
if(i->second == fp)
{
i->second->m_iRefCount--;
2003-01-04 05:20:40 +00:00
if( fp->m_iRefCount == 0 )
{
delete i->second; // free the texture
m_mapPathToFont.erase( i ); // and remove the key in the map
}
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
return;
}
}
2003-01-04 05:20:40 +00:00
RageException::Throw( "Unloaded an unknown font (%p)", fp );
}