mainloop.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/mainloop.cpp $*
  25. * *
  26. * $Author:: Tom_s $*
  27. * *
  28. * $Modtime:: 2/21/02 3:13p $*
  29. * *
  30. * $Revision:: 77 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "mainloop.h"
  36. #include "init.h"
  37. #include "shutdown.h"
  38. #include "timemgr.h"
  39. #include "input.h"
  40. #include "gamemode.h"
  41. #include "debug.h"
  42. #include "msgloop.h"
  43. #include "wwprofile.h"
  44. #include "cnetwork.h"
  45. #include "miscutil.h"
  46. //#include "gamesettings.h"
  47. #include "WWAudio.H"
  48. #include "devoptions.h"
  49. #include "multihud.h"
  50. #include "gamedata.h"
  51. #include "diagnostics.h"
  52. #include "wwprofile.h"
  53. #include "crandom.h"
  54. #include "dialogmgr.h"
  55. #include "ccamera.h"
  56. #include "pathmgr.h"
  57. #include "networkobjectmgr.h"
  58. #include "WebBrowser.h"
  59. #include "autostart.h"
  60. #include "gameinitmgr.h"
  61. #include "servercontrol.h"
  62. #include "consolemode.h"
  63. #include "gamespyadmin.h"
  64. #include "demosupport.h"
  65. #include "GameSpy_QnR.h"
  66. /*
  67. **
  68. */
  69. bool RunMainLoop = true;
  70. int ExitCode = EXIT_SUCCESS;
  71. void Stop_Main_Loop(int exitCode)
  72. {
  73. RunMainLoop = false;
  74. ExitCode = exitCode;
  75. }
  76. void _Game_Main_Loop_Loop(void)
  77. {
  78. WWPROFILE( "Main Loop" );
  79. unsigned long time1 = TIMEGETTIME();
  80. TimeManager::Update();
  81. Input::Update();
  82. { WWPROFILE( "Pathfind Evaluate" );
  83. if (COMBAT_CAMERA != NULL) {
  84. Vector3 camera_pos = COMBAT_CAMERA->Get_Position();
  85. PathMgrClass::Resolve_Paths( camera_pos );
  86. }
  87. }
  88. { WWPROFILE( "Think" );
  89. GameModeManager::Think();
  90. GameInitMgrClass::Think();
  91. }
  92. { WWPROFILE( "Dialog Mgr Update" );
  93. DialogMgrClass::On_Frame_Update ();
  94. }
  95. { WWPROFILE( "Network Object Mgr Think" );
  96. NetworkObjectMgrClass::Think ();
  97. ServerControl.Service();
  98. }
  99. { WWPROFILE("GameSpy_QnR");
  100. GameSpyQnR.Think();
  101. }
  102. if (cGameSpyAdmin::Is_Gamespy_Game()) {
  103. WWPROFILE( "cGameSpyAdmin Think" );
  104. cGameSpyAdmin::Think();
  105. }
  106. //
  107. // If the following assert hits it may indicate that your
  108. // working directory pathname got cleared in the project settings.
  109. //
  110. WWASSERT(GameModeManager::Find("Combat") != NULL);
  111. if (!GameModeManager::Find("Combat")->Is_Active()) {
  112. cNetwork::Update();
  113. }
  114. // Denzil - Embedded browser
  115. if (WebBrowser::IsWebPageDisplayed() == false) {
  116. GameModeManager::Render();
  117. }
  118. if (AutoRestart.Is_Active()) {
  119. AutoRestart.Think();
  120. }
  121. { WWPROFILE("ConsoleBox");
  122. ConsoleBox.Think();
  123. }
  124. DEMO_SECURITY_CHECK;
  125. { WWPROFILE( "Audio" );
  126. if (!ConsoleBox.Is_Exclusive()) {
  127. WWAudioClass::Get_Instance ()->On_Frame_Update (0);
  128. }
  129. }
  130. // Give the sound manager a chance to think
  131. // PROFILE( "Audio", WWAudioClass::Get_Instance ()->On_Frame_Update (0) );
  132. Windows_Message_Handler();
  133. #ifdef WWDEBUG
  134. // Sometimes it is useful to be able to artificially lower the frame rate
  135. Sleep(cDevOptions::DesiredFrameSleepMs.Get());
  136. #endif
  137. #if 0
  138. { WWPROFILE( "Random" );
  139. // spin the Random Generator, a little
  140. int count = FreeRandom.Get_Int( 5 );
  141. while ( count-- > 0 ) {
  142. FreeRandom.Get_Int();
  143. }
  144. }
  145. #endif
  146. DebugManager::Update();
  147. /*
  148. ** Sleep for a while if we are hogging the CPU.
  149. */
  150. if (cNetwork::I_Am_Only_Server()) {
  151. unsigned long time2 = TIMEGETTIME();
  152. if (time2 >= time1) {
  153. /*
  154. ** 16 (approx) for 60 fps. (1000/60)
  155. */
  156. unsigned long diff = time2 - time1;
  157. if (diff < 16) {
  158. unsigned long sleep_time = 16 - (time2 - time1);
  159. Sleep(sleep_time);
  160. }
  161. }
  162. }
  163. }
  164. /*
  165. ** MAIN GAME LOOP
  166. */
  167. int Game_Main_Loop(void)
  168. {
  169. const unsigned long servicetime = 1000; // Time in milliseconds.
  170. unsigned long time;
  171. // Only run main loop if the init is succesful!
  172. if (Game_Init()) {
  173. while ( RunMainLoop ) {
  174. _Game_Main_Loop_Loop();
  175. }
  176. // IML: Allow a short period to process any outstanding sound effects before shutdown.
  177. time = TIMEGETTIME();
  178. while (TIMEGETTIME() - time < servicetime) {
  179. WWAudioClass::Get_Instance ()->On_Frame_Update (0);
  180. }
  181. Game_Shutdown();
  182. }
  183. return ExitCode;
  184. }