00001
#include "StdAfx.h"
00002
#include "FontManager.h"
00003
00004 CFontManager::CFontManager()
00005 {
00006
SetEntityType(Entity_FontManager);
00007
CGlobalLogger::GetSingleton().
Write(
"Font Manager - Instantiated\n");
00008 }
00009
00010 CFontManager::~CFontManager()
00011 {
00012
Destroy();
00013
CGlobalLogger::GetSingleton().
Write(
"Font Manager - Destroyed\n");
00014 }
00015
00016 CCustomFont *
CFontManager::AddResource(CString strName, UINT uiID, eEntityType ResourceType)
00017 {
00018
switch(ResourceType)
00019 {
00020
case Entity_Default:
00021
case Entity_CustomFont:
00022
case Entity_BitmapFont:
00023
CBitmapFont *pNewBitmapFont =
new CBitmapFont;
00024
00025 pNewBitmapFont->
SetID(uiID);
00026 pNewBitmapFont->
SetName(strName);
00027
00028
push_back(pNewBitmapFont);
00029
00030
return pNewBitmapFont;
00031
break;
00032 }
00033
00034
return NULL;
00035 }
00036
00037 int CFontManager::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00038 {
00039
TiXmlElement* pXMLElement = NULL;
00040
const char *pcValue = NULL;
00041
00042
int iRetValue =
CResourceManager::LoadXML(pDataNode, strFilename);
00043
if(iRetValue !=1)
00044
return iRetValue;
00045
00046 pXMLElement = m_pXMLElement;
00047
00048
if(stricmp(pXMLElement->
pcValue(),
"Font") == 0)
00049 {
00050
CXMLResource *AddedResource =
LoadResource(pDataNode, strFilename);
00051
if(AddedResource !=NULL)
00052 {
00053
CGlobalLogger::GetSingleton().
Write(
"Font (Name: %s, ID: %d) has been added successfully.\n", AddedResource->
GetName().GetBuffer(), AddedResource->
GetID());
00054
return 1;
00055 }
00056
else
00057
return 0;
00058 }
00059
00060 UINT FontLoadedCount = 0;
00061
TiXmlNode *pIterXMLNode = pXMLElement->
FirstChild();
00062
while(pIterXMLNode !=NULL)
00063 {
00064
if(stricmp(pIterXMLNode->
pcValue(),
"font") == 0)
00065 {
00066
CXMLResource *AddedResource =
LoadResource(pIterXMLNode,
"");
00067
if(AddedResource !=NULL)
00068 {
00069
CGlobalLogger::GetSingleton().
Write(
"Font (Name: %s, ID: %d) has been added successfully.\n", AddedResource->
GetName().GetBuffer(), AddedResource->
GetID());
00070 FontLoadedCount++;
00071 }
00072 }
00073
00074 pIterXMLNode = pXMLElement->
IterateChildren(pIterXMLNode);
00075 }
00076
00077
return FontLoadedCount;
00078 }
00079
00080 int CFontManager::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00081 {
00082
00083
if(pDataNode == NULL && strFilename ==
"")
00084
return -1;
00085
00086
char Buf[128] =
"";
00087
TiXmlElement *pSaveXMLElement = NULL;
00088
00089
if(pDataNode != NULL)
00090 pSaveXMLElement = pDataNode->
ToElement();
00091
else
00092 pSaveXMLElement =
new TiXmlElement(
"FontDatabase");
00093
00094
CCustomFont *pCustomFont = (
CCustomFont *)
begin();
00095
set_ptr(pCustomFont);
00096
00097
while(pCustomFont !=NULL)
00098 {
00099 TiXmlElement CustomFontXMLElement(
"Font");
00100
if(pCustomFont->
SaveXML(&CustomFontXMLElement,
""))
00101 pSaveXMLElement->
InsertEndChild(CustomFontXMLElement);
00102
00103 pCustomFont = (
CCustomFont *)
next();
00104 }
00105
00106
if(pDataNode == NULL && strFilename !=
"")
00107 {
00108
TiXmlDocument SaveDoc;
00109 SaveDoc.
InsertEndChild(*pSaveXMLElement);
00110
int iRetValue = SaveDoc.
SaveFile(strFilename);
00111 SAFEDEL(pSaveXMLElement)
00112
00113
return iRetValue;
00114 }
00115
00116
return 1;
00117 }
00118
00119 CCustomFont *
CFontManager::LoadResource(
TiXmlNode *pDataNode, CString strFilename, UINT uiID, CString strName)
00120 {
00121
if(pDataNode != NULL)
00122 {
00123
const char *pcValue = NULL;
00124
TiXmlElement *pXMLElement = pDataNode->
ToElement();
00125
00126 pcValue = pXMLElement->
Attribute(
"FontByID");
00127
if(pcValue !=NULL)
00128 {
00129
CCustomFont *ExistingCustomFont =
GetResource(-1, atoi(pcValue),
"");
00130
if(ExistingCustomFont !=NULL)
00131
return ExistingCustomFont;
00132
else
00133
return NULL;
00134 }
00135
00136 pcValue = pXMLElement->
Attribute(
"FontByName");
00137
if(pcValue !=NULL)
00138 {
00139
CCustomFont *ExistingCustomFont =
GetResource(-1, -1, pcValue);
00140
if(ExistingCustomFont !=NULL)
00141
return ExistingCustomFont;
00142
else
00143
return NULL;
00144 }
00145 }
00146
00147
CCustomFont *NewFont =
new CBitmapFont;
00148
00149
if(NewFont->
LoadXML(pDataNode, strFilename))
00150 {
00151
if(NewFont->
GetID() == 0)
00152 NewFont->
SetID(uiID);
00153
00154
if(NewFont->
GetName() ==
"")
00155 NewFont->
SetName(strName);
00156
00157
if(NewFont->
GetName() !=
"")
00158 {
00159
if(
GetResource(-1, -1, NewFont->
GetName()) == NULL)
00160 {
00161
push_back(NewFont);
00162
return NewFont;
00163 }
00164
else
00165 {
00166 SAFEDEL(NewFont)
00167
return NULL;
00168 }
00169 }
00170
else
00171 {
00172
push_back(NewFont);
00173
return NewFont;
00174 }
00175 }
00176
else
00177 {
00178 SAFEDEL(NewFont)
00179
return NULL;
00180 }
00181
00182
00183
return NULL;
00184 }
00185
00186 CCustomFont *
CFontManager::GetResource(
int uiIndex,
int uiID, CString strName, CString strFilename)
00187 {
00188
if(uiID !=-1 && uiIndex == -1 && strName ==
"" && strFilename ==
"")
00189 {
00190
CXMLResource *ResourcePtr =
begin();
00191
set_ptr(ResourcePtr);
00192
00193
while(ResourcePtr !=NULL)
00194 {
00195
if(ResourcePtr->
GetID() == uiID)
00196
return (
CBitmapFont *)ResourcePtr;
00197
00198 ResourcePtr =
next();
00199 }
00200 }
00201
else if(strName !=
"" && uiIndex == -1 && uiID == -1 && strFilename ==
"")
00202 {
00203
CXMLResource *ResourcePtr =
begin();
00204
set_ptr(ResourcePtr);
00205
00206
while(ResourcePtr !=NULL)
00207 {
00208
if(ResourcePtr->
GetName() == strName)
00209
return (
CBitmapFont *)ResourcePtr;
00210
00211 ResourcePtr =
next();
00212 }
00213 }
00214
else if(uiIndex !=-1 && uiID == -1 && strName ==
"" && strFilename ==
"")
00215 {
00216
if(uiIndex >=0 && uiIndex <
size())
00217
return (
CBitmapFont *)
at(uiIndex);
00218 }
00219
else if(strFilename !=
"" && uiIndex ==-1 && uiID == -1 && strName ==
"")
00220 {
00221
CXMLResource *ResourcePtr =
begin();
00222
set_ptr(ResourcePtr);
00223
00224
while(ResourcePtr !=NULL)
00225 {
00226
if(stricmp(ResourcePtr->
GetFilename().GetBuffer(), strFilename.GetBuffer()) == 0)
00227
return (
CCustomFont *)ResourcePtr;
00228
00229 ResourcePtr =
next();
00230 }
00231 }
00232
00233
return NULL;
00234 }
00235
00236 int CFontManager::RemoveResource(
CCustomFont *Font)
00237 {
00238
00239
return 1;
00240 }
00241
00242 void CFontManager::Destroy()
00243 {
00244
if(
CGlobalLogger::GetSingleton().
GetLogLevel() == 0)
00245
CGlobalLogger::GetSingleton().
Write(
"Font Manager - Cleaning Up\n");
00246
else if(
CGlobalLogger::GetSingleton().
GetLogLevel() == 1)
00247
CGlobalLogger::GetSingleton().
Write(
"Font Manager - Cleaning Up - Freed %d fonts.\n",
size());
00248
else if(
CGlobalLogger::GetSingleton().
GetLogLevel() == 2)
00249 {
00250
CCustomFont *IterFont = (
CCustomFont *)
begin();
00251
set_ptr(IterFont);
00252
00253
while(IterFont !=NULL)
00254 {
00255
if(IterFont->
GetFontType() == Bitmap_Font)
00256
CGlobalLogger::GetSingleton().
Write(
"Bitmap Font released(Name: %s, ID: %d)\n", IterFont->
GetName().GetBuffer(), IterFont->
GetID());
00257
else if(IterFont->
GetFontType() == Logical_Font)
00258
CGlobalLogger::GetSingleton().
Write(
"Logical Font released(Name: %s, ID: %d)\n", IterFont->
GetName().GetBuffer(), IterFont->
GetID());
00259
00260 IterFont = (
CCustomFont *)
next();
00261 }
00262 }
00263
CResourceManager::Destroy();
00264 }
00265
00266 bool CFontManager::IsOfType(eEntityType eType)
00267 {
00268
if(eType == Entity_FontManager)
00269
return true;
00270
00271
return CResourceManager::IsOfType(eType);
00272 }