more bitmap font work
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
Desc: See header.
|
Desc: See header.
|
||||||
|
|
||||||
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
|
||||||
Chris Danford
|
Chris Danford
|
||||||
Glenn Maynard
|
Glenn Maynard
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|||||||
+43
-21
@@ -15,8 +15,10 @@
|
|||||||
#include "IniFile.h"
|
#include "IniFile.h"
|
||||||
|
|
||||||
struct glyph {
|
struct glyph {
|
||||||
|
RageTexture *Texture;
|
||||||
|
|
||||||
/* Number of pixels to advance horizontally after drawing this character. */
|
/* Number of pixels to advance horizontally after drawing this character. */
|
||||||
float advance;
|
float hadvance, vadvance;
|
||||||
|
|
||||||
/* Size of the actual rendered character. */
|
/* Size of the actual rendered character. */
|
||||||
float width, height;
|
float width, height;
|
||||||
@@ -28,35 +30,55 @@ struct glyph {
|
|||||||
RectF rect;
|
RectF rect;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Font
|
class FontPage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Font( const CString &sASCIITexturePath );
|
RageTexture* m_pTexture;
|
||||||
Font( const CString &sTexturePath, const CString& sChars );
|
|
||||||
~Font();
|
CString m_sTexturePath;
|
||||||
void Init();
|
|
||||||
|
/* All glyphs in this list will point to m_pTexture. */
|
||||||
|
vector<glyph> glyphs;
|
||||||
|
|
||||||
|
map<int,int> m_iCharToGlyphNo;
|
||||||
|
|
||||||
|
FontPage();
|
||||||
|
~FontPage();
|
||||||
|
|
||||||
int GetLineWidthInSourcePixels( const CString &szLine );
|
int GetLineWidthInSourcePixels( const CString &szLine );
|
||||||
int GetLineHeightInSourcePixels( const CString &szLine );
|
int GetLineHeightInSourcePixels( const CString &szLine );
|
||||||
|
|
||||||
int m_iRefCount;
|
void Load( const CString &sASCIITexturePath, IniFile &ini );
|
||||||
|
|
||||||
CString m_sTexturePath;
|
|
||||||
|
|
||||||
vector<glyph> glyphs;
|
|
||||||
|
|
||||||
RageTexture* m_pTexture;
|
|
||||||
bool m_bCapitalsOnly;
|
|
||||||
int m_iLineSpacing;
|
|
||||||
|
|
||||||
map<int,int> m_iCharToFrameNo;
|
|
||||||
|
|
||||||
const glyph &GetGlyph( int frameNo ) const { return glyphs[frameNo]; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight);
|
void SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight);
|
||||||
void SetTextureCoords(const vector<int> &widths);
|
void SetTextureCoords(const vector<int> &widths, int LineSpacing);
|
||||||
void Load( const CString &sASCIITexturePath, IniFile &ini );
|
};
|
||||||
|
|
||||||
|
class Font
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int m_iRefCount;
|
||||||
|
CString path;
|
||||||
|
map<int,glyph*> m_iCharToGlyph;
|
||||||
|
|
||||||
|
Font();
|
||||||
|
|
||||||
|
RageTexture *GetGlyphTexture( int c );
|
||||||
|
const glyph &GetGlyph( int c ) const;
|
||||||
|
|
||||||
|
int GetLineWidthInSourcePixels( const CString &szLine );
|
||||||
|
int GetLineHeightInSourcePixels( const CString &szLine );
|
||||||
|
|
||||||
|
/* Add a FontPage to this font. */
|
||||||
|
void AddPage(FontPage *fp);
|
||||||
|
|
||||||
|
/* Load font-wide settings. */
|
||||||
|
void LoadINI(IniFile &ini);
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<FontPage *> pages;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -57,67 +57,89 @@ Font* FontManager::LoadFont( CString sFontOrTextureFilePath, CString sChars )
|
|||||||
// of the same bitmap if there are equivalent but different paths
|
// of the same bitmap if there are equivalent but different paths
|
||||||
// (e.g. "graphics\blah.png" and "..\stepmania\graphics\blah.png" ).
|
// (e.g. "graphics\blah.png" and "..\stepmania\graphics\blah.png" ).
|
||||||
|
|
||||||
Font* pFont = NULL;
|
map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontOrTextureFilePath);
|
||||||
|
|
||||||
std::map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontOrTextureFilePath);
|
|
||||||
if(p != m_mapPathToFont.end()) {
|
if(p != m_mapPathToFont.end()) {
|
||||||
// LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount) );
|
// LOG->Trace( ssprintf("FontManager: The Font '%s' now has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount) );
|
||||||
pFont=p->second;
|
Font *pFont=p->second;
|
||||||
pFont->m_iRefCount++;
|
pFont->m_iRefCount++;
|
||||||
|
return pFont;
|
||||||
}
|
}
|
||||||
else // the texture is not already loaded
|
|
||||||
{
|
|
||||||
CString sDrive, sDir, sFName, sExt;
|
|
||||||
splitpath( false, sFontOrTextureFilePath, sDrive, sDir, sFName, sExt );
|
|
||||||
|
|
||||||
if( sChars == "" )
|
// the texture is not already loaded
|
||||||
pFont = (Font*) new Font( sFontOrTextureFilePath );
|
CString sDrive, sDir, sFName, sExt;
|
||||||
else
|
splitpath( false, sFontOrTextureFilePath, sDrive, sDir, sFName, sExt );
|
||||||
pFont = (Font*) new Font( sFontOrTextureFilePath, sChars );
|
|
||||||
|
Font* pFont = new Font;
|
||||||
|
|
||||||
|
|
||||||
|
FontPage *fp = new FontPage;
|
||||||
|
IniFile ini;
|
||||||
|
|
||||||
|
int expect;
|
||||||
|
if( sChars == "" )
|
||||||
|
{
|
||||||
|
/* Default to 0..255. */
|
||||||
|
for( int i=0; i<256; i++ )
|
||||||
|
fp->m_iCharToGlyphNo[i] = i;
|
||||||
|
|
||||||
|
// Find .ini widths path from texture path
|
||||||
|
CString sDir, sFileName, sExtension;
|
||||||
|
splitrelpath( sFontOrTextureFilePath, sDir, sFileName, sExtension );
|
||||||
|
const CString sIniPath = sDir + sFileName + ".ini";
|
||||||
|
|
||||||
|
ini.SetPath( sIniPath );
|
||||||
|
ini.ReadFile();
|
||||||
|
ini.RenameKey("Char Widths", "main");
|
||||||
|
expect = 256;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Map characters to frames; we don't actually have an INI. */
|
||||||
|
for( int i=0; i<sChars.GetLength(); i++ )
|
||||||
|
{
|
||||||
|
char c = sChars[i];
|
||||||
|
fp->m_iCharToGlyphNo[c] = i;
|
||||||
|
}
|
||||||
|
expect = sChars.GetLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
fp->Load(sFontOrTextureFilePath, ini);
|
||||||
|
if( fp->m_pTexture->GetNumFrames() != expect )
|
||||||
|
RageException::Throw( "The font '%s' has %d frames; expected %i frames.",
|
||||||
|
fp->m_pTexture->GetNumFrames(), expect );
|
||||||
|
|
||||||
// LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath.GetString());
|
// LOG->Trace( "FontManager: Loading '%s' from disk.", sFontFilePath.GetString());
|
||||||
|
|
||||||
m_mapPathToFont[sFontOrTextureFilePath] = pFont;
|
pFont->AddPage(fp);
|
||||||
}
|
m_mapPathToFont[sFontOrTextureFilePath] = pFont;
|
||||||
|
|
||||||
|
pFont->LoadINI(ini);
|
||||||
|
|
||||||
return pFont;
|
return pFont;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool FontManager::IsFontLoaded( CString sFontFilePath )
|
void FontManager::UnloadFont( Font *fp )
|
||||||
{
|
{
|
||||||
sFontFilePath.MakeLower();
|
|
||||||
|
|
||||||
return m_mapPathToFont.find(sFontFilePath) != m_mapPathToFont.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
void FontManager::UnloadFont( CString sFontFilePath )
|
|
||||||
{
|
|
||||||
sFontFilePath.MakeLower();
|
|
||||||
|
|
||||||
// LOG->Trace( "FontManager::UnloadTexture(%s).", sFontFilePath.GetString() );
|
// LOG->Trace( "FontManager::UnloadTexture(%s).", sFontFilePath.GetString() );
|
||||||
|
|
||||||
if( sFontFilePath == "" )
|
for( std::map<CString, Font*>::iterator i = m_mapPathToFont.begin();
|
||||||
|
i != m_mapPathToFont.end(); ++i)
|
||||||
{
|
{
|
||||||
// LOG->Trace( "FontManager::UnloadTexture(): tried to Unload a blank" );
|
if(i->second == fp)
|
||||||
return;
|
{
|
||||||
}
|
i->second->m_iRefCount--;
|
||||||
|
|
||||||
Font* pFont;
|
if( fp->m_iRefCount == 0 )
|
||||||
std::map<CString, Font*>::iterator p = m_mapPathToFont.find(sFontFilePath);
|
{
|
||||||
if(p == m_mapPathToFont.end())
|
delete i->second; // free the texture
|
||||||
RageException::Throw( "Tried to Unload a font that wasn't loaded. '%s'", sFontFilePath.GetString() );
|
m_mapPathToFont.erase( i ); // and remove the key in the map
|
||||||
|
}
|
||||||
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 );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// There are no more references to this texture.
|
|
||||||
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
|
// LOG->Trace( "FontManager: '%s' will be deleted. It has %d references.", sFontFilePath.GetString(), pFont->m_iRefCount );
|
||||||
SAFE_DELETE( pFont ); // free the texture
|
return;
|
||||||
m_mapPathToFont.erase( p ); // and remove the key in the map
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
RageException::Throw( "Unloaded an unknown font (%p)", fp );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,11 @@ public:
|
|||||||
~FontManager();
|
~FontManager();
|
||||||
|
|
||||||
Font* LoadFont( CString sFontOrTextureFilePath, CString sChars = "" );
|
Font* LoadFont( CString sFontOrTextureFilePath, CString sChars = "" );
|
||||||
bool IsFontLoaded( CString sFontPath );
|
void UnloadFont( Font *fp );
|
||||||
void UnloadFont( CString sFontPath );
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// map from file name to a texture holder
|
// map from file name to a texture holder
|
||||||
std::map<CString, Font*> m_mapPathToFont;
|
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
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ MenuTimer::MenuTimer()
|
|||||||
m_textDigit1.TurnShadowOff();
|
m_textDigit1.TurnShadowOff();
|
||||||
m_textDigit2.TurnShadowOff();
|
m_textDigit2.TurnShadowOff();
|
||||||
|
|
||||||
float fCharWidth = (float)m_textDigit1.m_pFont->m_pTexture->GetSourceFrameWidth();
|
const glyph &g = m_textDigit1.m_pFont->GetGlyph('0');
|
||||||
|
float fCharWidth = (float)g.Texture->GetSourceFrameWidth();
|
||||||
|
|
||||||
m_textDigit1.SetXY( -fCharWidth/2, 0 );
|
m_textDigit1.SetXY( -fCharWidth/2, 0 );
|
||||||
m_textDigit2.SetXY( +fCharWidth/2, 0 );
|
m_textDigit2.SetXY( +fCharWidth/2, 0 );
|
||||||
|
|||||||
Reference in New Issue
Block a user