W3DDisplayStringManager.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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: W3DDisplayStringManager.cpp //////////////////////////////////////////////////////////////
  24. // Created: Colin Day, July 2001
  25. // Desc: Display string Manager for W3D
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include <stdlib.h>
  28. #include "Common/Debug.h"
  29. #include "GameClient/GameClient.h"
  30. #include "GameClient/GameText.h"
  31. #include "GameClient/DisplayString.h"
  32. #include "GameClient/DrawGroupInfo.h"
  33. #include "GameClient/GlobalLanguage.h"
  34. #include "W3DDevice/GameClient/W3DDisplayStringManager.h"
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////
  36. // PUBLIC FUNCTIONS
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38. //-------------------------------------------------------------------------------------------------
  39. W3DDisplayStringManager::W3DDisplayStringManager( void )
  40. {
  41. for (Int i = 0; i < MAX_GROUPS; ++i)
  42. {
  43. m_groupNumeralStrings[i] = NULL;
  44. }
  45. m_formationLetterDisplayString = NULL;
  46. }
  47. //-------------------------------------------------------------------------------------------------
  48. W3DDisplayStringManager::~W3DDisplayStringManager( void )
  49. {
  50. for (Int i = 0; i < MAX_GROUPS; ++i)
  51. {
  52. if (m_groupNumeralStrings[i])
  53. freeDisplayString(m_groupNumeralStrings[i]);
  54. m_groupNumeralStrings[i] = NULL;
  55. }
  56. if (m_formationLetterDisplayString)
  57. freeDisplayString( m_formationLetterDisplayString );
  58. m_formationLetterDisplayString = NULL;
  59. }
  60. //-------------------------------------------------------------------------------------------------
  61. void W3DDisplayStringManager::postProcessLoad( void )
  62. {
  63. // Get the font.
  64. GameFont *font = TheFontLibrary->getFont(
  65. TheDrawGroupInfo->m_fontName,
  66. TheDrawGroupInfo->m_fontSize,
  67. TheDrawGroupInfo->m_fontIsBold );
  68. for (Int i = 0; i < MAX_GROUPS; ++i)
  69. {
  70. m_groupNumeralStrings[i] = newDisplayString();
  71. m_groupNumeralStrings[i]->setFont(font);
  72. #ifdef KRIS_BRUTAL_HACK_FOR_AIRCRAFT_CARRIER_DEBUGGING
  73. UnicodeString displayNumber;
  74. displayNumber.format( L"%d", i);
  75. m_groupNumeralStrings[i]->setText( displayNumber );
  76. #else
  77. AsciiString displayNumber;
  78. displayNumber.format("NUMBER:%d", i);
  79. m_groupNumeralStrings[i]->setText(TheGameText->fetch(displayNumber));
  80. #endif
  81. }
  82. m_formationLetterDisplayString = newDisplayString();
  83. m_formationLetterDisplayString->setFont(font);
  84. AsciiString displayLetter;
  85. displayLetter.format("LABEL:FORMATION");
  86. m_formationLetterDisplayString->setText(TheGameText->fetch(displayLetter));
  87. }
  88. //-------------------------------------------------------------------------------------------------
  89. /** Allocate a new display string and tie it to the master list so we
  90. * can keep track of it */
  91. //-------------------------------------------------------------------------------------------------
  92. DisplayString *W3DDisplayStringManager::newDisplayString( void )
  93. {
  94. DisplayString *newString = newInstance(W3DDisplayString);
  95. // sanity
  96. if( newString == NULL )
  97. {
  98. DEBUG_LOG(( "newDisplayString: Could not allcoate new W3D display string\n" ));
  99. assert( 0 );
  100. return NULL;
  101. } // end if
  102. // assign a default font
  103. if (TheGlobalLanguageData && TheGlobalLanguageData->m_defaultDisplayStringFont.name.isNotEmpty())
  104. {
  105. newString->setFont(TheFontLibrary->getFont(
  106. TheGlobalLanguageData->m_defaultDisplayStringFont.name,
  107. TheGlobalLanguageData->m_defaultDisplayStringFont.size,
  108. TheGlobalLanguageData->m_defaultDisplayStringFont.bold) );
  109. }
  110. else
  111. newString->setFont( TheFontLibrary->getFont( AsciiString("Times New Roman"), 12, FALSE ) );
  112. // link string to list
  113. link( newString );
  114. // return our new string
  115. return newString;
  116. } // end newDisplayString
  117. //-------------------------------------------------------------------------------------------------
  118. /** Remove a display string from the master list and delete the data */
  119. //-------------------------------------------------------------------------------------------------
  120. void W3DDisplayStringManager::freeDisplayString( DisplayString *string )
  121. {
  122. // sanity
  123. if( string == NULL )
  124. return;
  125. // unlink
  126. unLink( string );
  127. // if the string happens to fall where our current checkpoint was, set the checkpoint to null
  128. if ( m_currentCheckpoint == string) {
  129. m_currentCheckpoint = NULL;
  130. }
  131. // free data
  132. string->deleteInstance();
  133. } // end freeDisplayString
  134. //-------------------------------------------------------------------------------------------------
  135. /** Update method for our display string Manager ... if it's been too
  136. * long since the last time a string has been rendered, we will free
  137. * the rendering resources of the string, if it needs to render again
  138. * the DisplayString will have to rebuild the rendering data before
  139. * the draw will work */
  140. //-------------------------------------------------------------------------------------------------
  141. void W3DDisplayStringManager::update( void )
  142. {
  143. // call base in case we add something later
  144. DisplayStringManager::update();
  145. W3DDisplayString *string = static_cast<W3DDisplayString *>(m_stringList);
  146. // if the m_currentCheckpoint is valid, use it for the starting point for the search
  147. if (m_currentCheckpoint) {
  148. string = static_cast<W3DDisplayString *>(m_currentCheckpoint);
  149. }
  150. UnsignedInt currFrame = TheGameClient->getFrame();
  151. const UnsignedInt w3dCleanupTime = 60; /** any string not rendered after
  152. this many frames will have its
  153. render resources freed */
  154. int numStrings = 10;
  155. // looping through all the strings eats up a lot of ambient time. Instead,
  156. // loop through 10 (arbitrarily chosen) or till the end is hit.
  157. while ( numStrings-- && string)
  158. {
  159. //
  160. // has this string "expired" in terms of using resources, a string
  161. // with a resource frame of zero isn't using any resources at all
  162. //
  163. if( string->m_lastResourceFrame != 0 &&
  164. currFrame - string->m_lastResourceFrame > w3dCleanupTime )
  165. {
  166. // free the resources
  167. string->m_textRenderer.Reset();
  168. string->m_textRendererHotKey.Reset();
  169. //
  170. // mark data in the string as changed so that if it needs to
  171. // be drawn again it will know to reconstruct the render data
  172. //
  173. string->m_textChanged = TRUE;
  174. //
  175. // set the last resource frame to zero, this allows us to ignore it
  176. // in future cleanup passes of this update routine
  177. //
  178. string->m_lastResourceFrame = 0;
  179. } // end if
  180. // move to next string
  181. string = static_cast<W3DDisplayString *>(string->next());
  182. } // end while
  183. // reset the starting point for our next search
  184. m_currentCheckpoint = string;
  185. } // end update
  186. //-------------------------------------------------------------------------------------------------
  187. DisplayString *W3DDisplayStringManager::getGroupNumeralString( Int numeral )
  188. {
  189. if (numeral < 0 || numeral > MAX_GROUPS - 1 )
  190. {
  191. DEBUG_CRASH(("Numeral '%d' out of range.\n", numeral));
  192. return m_groupNumeralStrings[0];
  193. }
  194. return m_groupNumeralStrings[numeral];
  195. }