GameFont.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: GameFont.cpp /////////////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, June 2001
  25. // Desc: Access to our representation for fonts
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "GameClient/GameFont.h"
  29. // PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
  30. FontLibrary *TheFontLibrary = NULL;
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////
  32. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////
  34. //-------------------------------------------------------------------------------------------------
  35. /** Link a font to the font list */
  36. //-------------------------------------------------------------------------------------------------
  37. void FontLibrary::linkFont( GameFont *font )
  38. {
  39. // sanity
  40. if( font == NULL )
  41. return;
  42. // link it
  43. font->next = m_fontList;
  44. m_fontList = font;
  45. // increment linked count
  46. m_count++;
  47. } // end linkFont
  48. //-------------------------------------------------------------------------------------------------
  49. /** Unlink a font from the font list */
  50. //-------------------------------------------------------------------------------------------------
  51. void FontLibrary::unlinkFont( GameFont *font )
  52. {
  53. GameFont *other = NULL;
  54. // sanity
  55. if( font == NULL )
  56. return;
  57. // sanity check and make sure this font is actually in this library
  58. for( other = m_fontList; other; other = other->next )
  59. if( other == font )
  60. break;
  61. if( other == NULL )
  62. {
  63. DEBUG_CRASH(( "Font '%s' not found in library\n", font->nameString.str() ));
  64. return;
  65. } // end if
  66. // scan for the font pointing to the one we're going to unlink
  67. for( other = m_fontList; other; other = other->next )
  68. if( other->next == font )
  69. break;
  70. //
  71. // if nothing was fount this was at the head of the list, otherwise
  72. // remove from chain
  73. //
  74. if( other == NULL )
  75. m_fontList = font->next;
  76. else
  77. other->next = font->next;
  78. // clean up this font we just unlinked just to be cool!
  79. font->next = NULL;
  80. // we now have one less font on the list
  81. m_count--;
  82. } // end unlinkFont
  83. //-------------------------------------------------------------------------------------------------
  84. /** Delete all font data, DO NOT throw an exception ... the destructor uses this */
  85. //-------------------------------------------------------------------------------------------------
  86. void FontLibrary::deleteAllFonts( void )
  87. {
  88. GameFont *font;
  89. // release all the fonts
  90. while( m_fontList )
  91. {
  92. // get temp pointer to this font
  93. font = m_fontList;
  94. // remove font fron the list, this will change m_fontList
  95. unlinkFont( font );
  96. // release font data
  97. releaseFontData( font );
  98. // delete the font list element
  99. font->deleteInstance();
  100. } // end while
  101. } // deleteAllFonts
  102. ///////////////////////////////////////////////////////////////////////////////////////////////////
  103. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////
  105. //-------------------------------------------------------------------------------------------------
  106. //-------------------------------------------------------------------------------------------------
  107. FontLibrary::FontLibrary( void )
  108. {
  109. m_fontList = NULL;
  110. m_count = 0;
  111. } // end FontLibrary
  112. //-------------------------------------------------------------------------------------------------
  113. //-------------------------------------------------------------------------------------------------
  114. FontLibrary::~FontLibrary( void )
  115. {
  116. // delete all font data
  117. deleteAllFonts();
  118. } // end ~FontLibrary
  119. //-------------------------------------------------------------------------------------------------
  120. /** Initialize what we need to in the font library */
  121. //-------------------------------------------------------------------------------------------------
  122. void FontLibrary::init( void )
  123. {
  124. } // end init
  125. //-------------------------------------------------------------------------------------------------
  126. /** Reset the fonts for this font library */
  127. //-------------------------------------------------------------------------------------------------
  128. void FontLibrary::reset( void )
  129. {
  130. // delete all font data
  131. deleteAllFonts();
  132. } // end reset
  133. //-------------------------------------------------------------------------------------------------
  134. /** Get a font from our list, if we don't have that font loaded we will
  135. * attempt to load it */
  136. //-------------------------------------------------------------------------------------------------
  137. GameFont *FontLibrary::getFont( AsciiString name, Int pointSize, Bool bold )
  138. {
  139. GameFont *font;
  140. // search for font in list
  141. for( font = m_fontList; font; font = font->next )
  142. {
  143. if( font->pointSize == pointSize &&
  144. font->bold == bold &&
  145. font->nameString == name
  146. )
  147. return font; // found
  148. } // end for font
  149. // font not found, allocate a new font element
  150. font = newInstance(GameFont);
  151. if( font == NULL )
  152. {
  153. DEBUG_CRASH(( "getFont: Unable to allocate new font list element\n" ));
  154. return NULL;
  155. } // end if
  156. // copy font data over to new element
  157. font->nameString = name;
  158. font->pointSize = pointSize;
  159. font->bold = bold;
  160. font->fontData = NULL;
  161. //DEBUG_LOG(("Font: Loading font '%s' %d point\n", font->nameString.str(), font->pointSize));
  162. // load the device specific data pointer
  163. if( loadFontData( font ) == FALSE )
  164. {
  165. DEBUG_CRASH(( "getFont: Unable to load font data pointer '%s'\n", name ));
  166. font->deleteInstance();
  167. return NULL;
  168. } // end if
  169. // tie font into list
  170. linkFont( font );
  171. // all is done and loaded
  172. return font;
  173. } // end getFont