OpenBarnyard
 
Loading...
Searching...
No Matches
AGUI2FontManager.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "AGUI2FontManager.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12TBOOL AGUI2FontManager::Open( const TCHAR* a_szFileName )
13{
14 auto pResource = FindFontResource( a_szFileName );
15
16 if ( pResource )
17 {
18 pResource->uiRefCount++;
19 return TTRUE;
20 }
21
22 pResource = FindUnusedFontResource();
23 TASSERT( pResource != TNULL );
24
25 if ( !pResource )
26 {
27 return TFALSE;
28 }
29
30 if ( pResource->oTRB.Load( a_szFileName ) != TTRB::ERROR_OK )
31 {
32 return TFALSE;
33 }
34
35 pResource->szFileName = new TCHAR[ TStringManager::String8Length( a_szFileName ) + 1 ];
36 TStringManager::String8Copy( pResource->szFileName, a_szFileName );
37
38 pResource->uiRefCount++;
39 CreateFontFromResource( pResource );
40
41 return TTRUE;
42}
43
45{
46 auto pRef = FindFontRef( a_szFontName );
47
48 if ( pRef )
49 {
50 pRef->uiNumRefs += 1;
51 return pRef->pFont;
52 }
53
54 return TNULL;
55}
56
58{
59 for ( TUINT i = 0; i < MAX_NUM_FONT_REFS; i++ )
60 {
61 if ( s_fontrefs[ i ].pFontResource != TNULL )
62 {
63 if ( TStringManager::String8CompareNoCase( s_fontrefs[ i ].szName, a_szFontName ) == 0 )
64 {
65 return &s_fontrefs[ i ];
66 }
67 }
68 }
69
70 return TNULL;
71}
72
74{
75 for ( TUINT i = 0; i < MAX_NUM_LOADED_FONTS; i++ )
76 {
77 if ( s_fontresources[ i ].uiRefCount > 0 )
78 {
79 if ( TStringManager::String8CompareNoCase( s_fontresources[ i ].szFileName, a_szResourceName ) == 0 )
80 {
81 return &s_fontresources[ i ];
82 }
83 }
84 }
85
86 return TNULL;
87}
88
89AGUI2FontRef* AGUI2FontManager::FindUnusedFontRef()
90{
91 for ( TUINT i = 0; i < MAX_NUM_FONT_REFS; i++ )
92 {
93 if ( s_fontrefs[ i ].pFontResource == TNULL )
94 {
95 return &s_fontrefs[ i ];
96 }
97 }
98
99 return TNULL;
100}
101
102AGUI2FontResource* AGUI2FontManager::FindUnusedFontResource()
103{
104 for ( TUINT i = 0; i < MAX_NUM_LOADED_FONTS; i++ )
105 {
106 if ( s_fontresources[ i ].uiRefCount == 0 )
107 {
108 return &s_fontresources[ i ];
109 }
110 }
111
112 return TNULL;
113}
114
115void AGUI2FontManager::CreateFontFromResource( AGUI2FontResource* a_pFontResource )
116{
117 auto& trb = a_pFontResource->oTRB;
118
119 for ( TINT i = 0; i < trb.GetNumSymbols(); i++ )
120 {
121 auto pSymbol = trb.GetSymbol( i );
122 auto pSymbolName = trb.GetSymbolName( pSymbol );
123
124 auto pFontRef = FindFontRef( pSymbolName );
125
126 if ( pFontRef == TNULL )
127 {
128 pFontRef = FindUnusedFontRef();
129 TASSERT( pFontRef != TNULL );
130
131 if ( pFontRef == TNULL ) return;
132
133 pFontRef->szName = new TCHAR[ TStringManager::String8Length( pSymbolName ) + 1 ];
134 TStringManager::String8Copy( pFontRef->szName, pSymbolName );
135
136 auto pFontDef = trb.CastSymbol<AGUI2FontDef>( pSymbolName );
137 pFontRef->pFont = new AGUI2Font();
138 pFontRef->pFont->Create( pFontDef );
139 pFontRef->pFontResource = a_pFontResource;
140 pFontRef->Unk1 = TNULL;
141 }
142 }
143}
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
@ ERROR_OK
Definition TTRB.h:258
static TSIZE String8Length(const TCHAR *str)
static TINT String8CompareNoCase(const TCHAR *str1, const TCHAR *str2, TSIZE size=-1)
static TCHAR * String8Copy(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
static AGUI2FontRef * FindFontRef(const TCHAR *a_szFontName)
static constexpr TUINT MAX_NUM_FONT_REFS
static TBOOL Open(const TCHAR *a_szFileName)
static AGUI2FontResource * FindFontResource(const TCHAR *a_szResourceName)
static AGUI2FontResource s_fontresources[MAX_NUM_LOADED_FONTS]
static constexpr TUINT MAX_NUM_LOADED_FONTS
static AGUI2FontRef s_fontrefs[MAX_NUM_FONT_REFS]
static AGUI2Font * FindFont(const TCHAR *a_szFontName)