gamemode.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. ** Command & Conquer Renegade(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/gamemode.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/02/02 2:27a $*
  29. * *
  30. * $Revision:: 61 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "gamemode.h"
  36. #include "wwprofile.h"
  37. #include "ww3d.h"
  38. #include "wwdebug.h"
  39. #include "miscutil.h"
  40. #include "slist.h"
  41. //#include "menu.h"
  42. #include "textdisplay.h"
  43. #include "thread.h"
  44. #include <stdio.h>
  45. #include "backgroundmgr.h"
  46. #include "render2d.h"
  47. #include "dazzle.h"
  48. #include "combat.h"
  49. #include "meshmdl.h"
  50. #include "rinfo.h"
  51. #include "objectives.h"
  52. #include "messagewindow.h"
  53. #include "dialogmgr.h"
  54. #include "pscene.h"
  55. #include "phys.h"
  56. #include "ccamera.h"
  57. #include "diagnostics.h"
  58. #include "dx8wrapper.h"
  59. #include "sortingrenderer.h"
  60. #include "textureloader.h"
  61. #include "binkmovie.h"
  62. //#include "helptext.h"
  63. #include "cnetwork.h"
  64. #include "dx8rendererdebugger.h"
  65. #include "consolemode.h"
  66. /*
  67. ** The gamemode list
  68. */
  69. SList<GameModeClass> GameModeList;
  70. Vector3 GameModeManager::BackgroundColor(0,0,0);
  71. int GameMajorModeClass::NumActiveMajorModes = 0;
  72. static unsigned _HiddenFrameCount=0;
  73. /*
  74. ** make sure we only have 1 active majormode
  75. */
  76. void GameMajorModeClass::Activate()
  77. {
  78. if ( State == GAME_MODE_INACTIVE ) {
  79. NumActiveMajorModes++;
  80. assert ( NumActiveMajorModes == 1 );
  81. }
  82. GameModeClass::Activate();
  83. }
  84. void GameMajorModeClass::Deactivate()
  85. {
  86. if ( !Is_Inactive() ) {
  87. NumActiveMajorModes--;
  88. assert ( NumActiveMajorModes == 0 );
  89. }
  90. GameModeClass::Deactivate();
  91. }
  92. /*
  93. **
  94. */
  95. void GameModeManager::Destroy( GameModeClass *mode )
  96. {
  97. assert( mode != NULL );
  98. mode->Deactivate();
  99. mode->Safely_Deactivate();
  100. Remove( mode );
  101. delete mode;
  102. }
  103. /*
  104. **
  105. */
  106. GameModeClass *GameModeManager::Add( GameModeClass *mode )
  107. {
  108. GameModeList.Add_Tail( mode );
  109. return mode;
  110. }
  111. void GameModeManager::Remove( GameModeClass *mode )
  112. {
  113. GameModeList.Remove( mode );
  114. }
  115. int GameModeManager::Count()
  116. {
  117. return GameModeList.Get_Count();
  118. }
  119. /*
  120. **
  121. */
  122. void GameModeManager::Destroy_All( void )
  123. {
  124. while ( Count() ) {
  125. Destroy( GameModeList.Remove_Tail() );
  126. }
  127. }
  128. void GameModeManager::List_Active_Game_Modes(void)
  129. {
  130. if (!Get_Text_Display()) {
  131. return;
  132. }
  133. WWASSERT(Get_Text_Display() != NULL);
  134. Get_Text_Display()->Print_System("Active game modes:");
  135. for (SLNode<GameModeClass> * game_mode_node = GameModeList.Head();
  136. game_mode_node != NULL; game_mode_node = game_mode_node->Next()) {
  137. GameModeClass * p_mode = game_mode_node->Data();
  138. WWASSERT(p_mode != NULL);
  139. if (p_mode->Is_Active()) {
  140. Get_Text_Display()->Print_System( " %s", p_mode->Name() );
  141. //Debug_Say((mode_str));
  142. }
  143. }
  144. }
  145. /*
  146. ** let all non inactive game modes think
  147. */
  148. void GameModeManager::Think( void )
  149. {
  150. for ( SLNode<GameModeClass> *game_mode_node = GameModeList.Head();
  151. game_mode_node != NULL;
  152. game_mode_node = game_mode_node->Next()) {
  153. GameModeClass *mode = game_mode_node->Data();
  154. //if ( mode->Get_State() != GAME_MODE_INACTIVE &&
  155. // mode->Get_State() != GAME_MODE_INACTIVE_PENDING ) {
  156. //if ( mode->Get_State() != GAME_MODE_INACTIVE &&
  157. if ( !mode->Is_Inactive() ) {
  158. // char name[80];
  159. // sprintf( name, "Think - %s", mode->Name() );
  160. mode->Think();
  161. }
  162. mode->Safely_Deactivate(); // if required
  163. }
  164. BINKMovie::Update();
  165. }
  166. void GameModeManager::Safely_Deactivate(void)
  167. {
  168. WWPROFILE( "Deactivate" );
  169. //
  170. // This method safely deactivates any inactive pending mode without
  171. // attempting a think
  172. //
  173. for (SLNode<GameModeClass> *game_mode_node = GameModeList.Head();
  174. game_mode_node != NULL; game_mode_node = game_mode_node->Next()) {
  175. GameModeClass * p_mode = game_mode_node->Data();
  176. WWASSERT(p_mode != NULL);
  177. p_mode->Safely_Deactivate(); // if required
  178. }
  179. }
  180. /*
  181. ** let all non inactive game modes draw
  182. */
  183. void GameModeManager::Render( void )
  184. {
  185. WWPROFILE( "Render" );
  186. if (!ConsoleBox.Is_Exclusive()) {
  187. bool clear=true;
  188. bool old_enable_draw=DX8Wrapper::_Is_Triangle_Draw_Enabled();
  189. bool old_enable_sorting_draw=SortingRendererClass::_Is_Triangle_Draw_Enabled();
  190. if (_HiddenFrameCount) {
  191. DX8Wrapper::_Enable_Triangle_Draw(false);
  192. SortingRendererClass::_Enable_Triangle_Draw(false);
  193. clear=false;
  194. }
  195. // Update the mesh debugger. This doesn't do anything at all unless the debugger is
  196. // enabled, so it is safe to always call it
  197. DX8RendererDebugger::Update();
  198. bool do_pscene = (COMBAT_SCENE != NULL) && !cNetwork::I_Am_Only_Server();
  199. if (!GameInFocus) do_pscene=false; // Don't render the game scene if the applicationisn't active
  200. if (do_pscene) {
  201. //
  202. // Don't pre-process the combat scene (does VIS and stuff) if
  203. // the game isn't active. (This gives us a menu performance boost).
  204. //
  205. if (Find( "Combat" )->Is_Active()) {
  206. COMBAT_SCENE->Pre_Render_Processing(*COMBAT_CAMERA);
  207. }
  208. }
  209. {
  210. WWPROFILE( "Begin_Render" );
  211. WW3D::Begin_Render (clear, clear, BackgroundMgrClass::Get_Clear_Color());
  212. }
  213. if (GameInFocus) {
  214. WWPROFILE( "Render Game Modes" );
  215. for ( SLNode<GameModeClass> *game_mode_node = GameModeList.Head();
  216. game_mode_node != NULL;
  217. game_mode_node = game_mode_node->Next()) {
  218. GameModeClass *mode = game_mode_node->Data();
  219. if ( mode->Get_State() != GAME_MODE_INACTIVE ) {
  220. mode->Render();
  221. }
  222. }
  223. }
  224. {
  225. WWPROFILE( "Message Window" );
  226. if (CombatManager::Get_Message_Window () != NULL) {
  227. CombatManager::Get_Message_Window ()->Render();
  228. }
  229. }
  230. {
  231. WWPROFILE( "ObjectiveViewer" );
  232. ObjectiveManager::Render_Viewer();
  233. }
  234. {
  235. WWPROFILE( "DialogMgr" );
  236. DialogMgrClass::Render();
  237. }
  238. {
  239. WWPROFILE( "cDiagnostics" );
  240. cDiagnostics::Render();
  241. }
  242. /*
  243. {
  244. WWPROFILE( "cHelpText" );
  245. cHelpText::Render();
  246. }
  247. */
  248. // Only update the movie when the application is active
  249. if (GameInFocus) {
  250. WWPROFILE( "BINK" );
  251. BINKMovie::Render();
  252. }
  253. {
  254. WWPROFILE( "End_Render" );
  255. WW3D::End_Render();
  256. }
  257. if (do_pscene) {
  258. COMBAT_SCENE->Post_Render_Processing();
  259. }
  260. {
  261. WWPROFILE( "Switch_Thread" );
  262. ThreadClass::Switch_Thread(); // This is important when working with multiple threads!
  263. }
  264. if (_HiddenFrameCount) {
  265. _HiddenFrameCount--;
  266. DX8Wrapper::_Enable_Triangle_Draw(old_enable_draw);
  267. SortingRendererClass::_Enable_Triangle_Draw(old_enable_sorting_draw);
  268. TextureLoader::Flush_Pending_Load_Tasks();
  269. }
  270. }
  271. }
  272. void GameModeManager::Hide_Render_Frames(unsigned frame_count)
  273. {
  274. _HiddenFrameCount=frame_count;
  275. }
  276. /*
  277. ** find a registered game mode by name
  278. */
  279. GameModeClass *GameModeManager::Find( const char * name )
  280. {
  281. for ( SLNode<GameModeClass> *game_mode_node = GameModeList.Head();
  282. game_mode_node != NULL;
  283. game_mode_node = game_mode_node->Next()) {
  284. GameModeClass *mode = game_mode_node->Data();
  285. if ( !stricmp( name, mode->Name() ) ) {
  286. return mode;
  287. }
  288. }
  289. return NULL;
  290. }
  291. //-----------------------------------------------------------------------------
  292. void GameModeClass::Activate()
  293. {
  294. if (State == GAME_MODE_INACTIVE) {
  295. Init();
  296. State = GAME_MODE_ACTIVE;
  297. }
  298. if (State == GAME_MODE_INACTIVE_PENDING) {
  299. State = GAME_MODE_ACTIVE;
  300. }
  301. }
  302. //-----------------------------------------------------------------------------
  303. void GameModeClass::Deactivate()
  304. {
  305. if (!Is_Inactive()) {
  306. State = GAME_MODE_INACTIVE_PENDING;
  307. }
  308. }
  309. //-----------------------------------------------------------------------------
  310. void GameModeClass::Safely_Deactivate()
  311. {
  312. if (State == GAME_MODE_INACTIVE_PENDING) {
  313. Shutdown();
  314. State = GAME_MODE_INACTIVE;
  315. }
  316. }
  317. //-----------------------------------------------------------------------------
  318. void GameModeClass::Suspend()
  319. {
  320. if (State == GAME_MODE_ACTIVE) {
  321. State = GAME_MODE_SUSPENDED;
  322. }
  323. }
  324. //-----------------------------------------------------------------------------
  325. void GameModeClass::Resume()
  326. {
  327. if (State == GAME_MODE_SUSPENDED) {
  328. State = GAME_MODE_ACTIVE;
  329. }
  330. }