mainloop.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /* $Header: /Commando/Code/Tests/movietest/mainloop.cpp 4 5/06/98 3:10p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando *
  24. * *
  25. * $Archive:: /Commando/Code/Tests/movietest/mainloop.cpp $*
  26. * *
  27. * $Author:: Greg_h $*
  28. * *
  29. * $Modtime:: 3/24/98 10:44a $*
  30. * *
  31. * $Revision:: 4 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "mainloop.h"
  37. #include "mono.h"
  38. #include "msgloop.h"
  39. #include "wwfile.h"
  40. #include "rawfile.h"
  41. #include "_globals.h"
  42. #include "_viewpt.h"
  43. #include <sr.hpp>
  44. #include <assert.h>
  45. #include "assetmgr.h"
  46. #include "sr_util.h"
  47. #include "dynamesh.h"
  48. #include "material.h"
  49. #include "world.h"
  50. #include "_world.h"
  51. #include "rendobj.h"
  52. #include "r2dobj.h"
  53. #include "r3dobj.h"
  54. #include "mesh.h"
  55. #include "hmodel.h"
  56. #include "light.h"
  57. #include "wwdebug.h"
  58. #include "ww3d.h"
  59. /*
  60. ** Globals
  61. */
  62. WW3DAssetManager The3DAssetManager;
  63. CameraClass * Camera;
  64. MeshClass * Mesh;
  65. LightClass * Light;
  66. LightClass * Light2;
  67. HModelClass * TestModel = NULL;
  68. HAnimClass * TestAnim = NULL;
  69. HAnimClass * CameraAnim = NULL;
  70. Quaternion ViewOrientation(1);
  71. Quaternion ViewMotion(1);
  72. Vector3 ViewCenter(0,0,0);
  73. float ViewDist = 55.0f;
  74. /*
  75. ** Local functions
  76. */
  77. void Render(void);
  78. void Create_Scene(void);
  79. void Create_Objects(void);
  80. void Destroy_Scene(void);
  81. void Destroy_Objects(void);
  82. void Load_Data(void);
  83. void Render_Scene(void);
  84. void Time_Step(void);
  85. void Init_Debug(void);
  86. void Shutdown_Debug(void);
  87. void wwdebug_message_handler(const char * message);
  88. void wwdebug_assert_handler(const char * message);
  89. bool wwdebug_trigger_handler(int trigger_num);
  90. void Debug_Refs(void);
  91. /*
  92. ** delay for time milliseconds
  93. */
  94. void Wait( int time )
  95. {
  96. int start = SystemTimer();
  97. while ( 1000 * ( SystemTimer() - start ) < ( time * SYSTEM_TIMER_RATE ) ) ;
  98. }
  99. /*
  100. ** MAIN GAME LOOP
  101. */
  102. void Main_Loop(void)
  103. {
  104. Init_Debug();
  105. Create_Scene();
  106. Load_Data();
  107. Create_Objects();
  108. while (!Keyboard->Down(VK_ESCAPE)) {
  109. Time_Step();
  110. Render();
  111. Windows_Message_Handler();
  112. // if (Keyboard->Down(VK_F1)) {
  113. // while(Keyboard->Down(VK_F1));
  114. // TheWorld.Next_Render_Device();
  115. // }
  116. }
  117. Destroy_Objects();
  118. WW3DAssetManager::Get_Instance()->Free_Assets();
  119. Destroy_Scene();
  120. Shutdown_Debug();
  121. Debug_Refs();
  122. }
  123. void Render(void)
  124. {
  125. WW3D::Begin_Render(true,Vector3(0,0,0.5));
  126. WW3D::Render(TheScene,Camera);
  127. WW3D::End_Render();
  128. }
  129. void Create_Scene(void)
  130. {
  131. // TheWorld.Init_Scene(ScreenResolution,8,1);
  132. TheScene = new SimpleSceneClass;
  133. }
  134. /*
  135. ** Load initial game data
  136. */
  137. void Load_Data(void)
  138. {
  139. WW3DAssetManager::Get_Instance()->Load_3D_Assets(RawFileClass("CAMERA.W3D"));
  140. WW3DAssetManager::Get_Instance()->Load_3D_Assets(RawFileClass("MOVIE.W3D"));
  141. }
  142. /*
  143. **
  144. */
  145. void Create_Objects(void)
  146. {
  147. Camera = new CameraClass("CAMERA");
  148. // TheWorld.Set_Camera(Camera);
  149. // Camera->Set_Depth(5000.0f);
  150. // Camera->Set_Clip_Planes(1.0f,5000.0f);
  151. Camera->Set_Focal_Length(0.060f);
  152. Camera->Set_Viewport(Vector2(0.3,0.3),Vector2(0.5,0.5));
  153. CameraAnim = WW3DAssetManager::Get_Instance()->Get_HAnim("Camera.Camera");
  154. TestModel = WW3DAssetManager::Get_Instance()->Create_HModel("Movie");
  155. assert(TestModel);
  156. TestAnim = WW3DAssetManager::Get_Instance()->Get_HAnim("Movie.Movie");
  157. // TestModel->Add(&TheWorld);
  158. TestModel->Add(TheScene);
  159. Light = new LightClass();
  160. Light->Set_Transform(Matrix3D(1));
  161. // Light->Add(&TheWorld);
  162. Light->Add(TheScene);
  163. }
  164. /*
  165. **
  166. */
  167. void Destroy_Scene(void)
  168. {
  169. // TheWorld.Set_Camera(NULL);
  170. // TheWorld.Shutdown_Scene();
  171. delete TheScene;
  172. TheScene = NULL;
  173. }
  174. /*
  175. **
  176. */
  177. void Destroy_Objects(void)
  178. {
  179. if (Mesh) {
  180. Mesh->Release_Ref();
  181. Mesh = NULL;
  182. }
  183. if (TestModel) {
  184. TestModel->Remove();
  185. TestModel->Release_Ref();
  186. TestModel = NULL;
  187. }
  188. if (Camera) {
  189. Camera->Release_Ref();
  190. Camera = NULL;
  191. }
  192. if (Light) {
  193. Light->Remove();
  194. Light->Release_Ref();
  195. Light = NULL;
  196. }
  197. if (Light2) {
  198. Light2->Remove();
  199. Light2->Release_Ref();
  200. Light2 = NULL;
  201. }
  202. }
  203. void Time_Step(void)
  204. {
  205. static int _frame = 0;
  206. static int _camframe = 0;
  207. static float _t = 0.0f;
  208. static float _dt = 0.01f;
  209. _t += _dt;
  210. if (_t > 1.0f) {
  211. _t = 1.0f;
  212. _dt = -0.01f;
  213. }
  214. if (_t < 0.0f) {
  215. _t = 0.0f;
  216. _dt = 0.01f;
  217. }
  218. if (Mesh) {
  219. Mesh->Apply_Damage(0,_t);
  220. }
  221. if (TestAnim) {
  222. _frame++;
  223. if (_frame >= TestAnim->Get_Num_Frames()) {
  224. _frame = 0;
  225. }
  226. TestModel->Set_Transform(Matrix3D(1));
  227. TestModel->Set_Animation(TestAnim,_frame);
  228. } else {
  229. TestModel->Set_Transform(Matrix3D(1));
  230. TestModel->Set_Animation();
  231. }
  232. if (CameraAnim) {
  233. _camframe++;
  234. if (_camframe >= CameraAnim->Get_Num_Frames()) {
  235. _camframe = 0;
  236. }
  237. Camera->Set_Transform(Matrix3D(1));
  238. Camera->Set_Animation(CameraAnim,_camframe);
  239. } else {
  240. Matrix3D cam(1);
  241. cam.Translate(ViewCenter);
  242. cam = cam * Build_Matrix3D(ViewOrientation);
  243. cam.Translate(Vector3(0,0,ViewDist));
  244. Camera->Set_Transform(cam);
  245. }
  246. }
  247. void Init_Debug(void)
  248. {
  249. /*
  250. ** Install message handler functions for the WWDebug messages
  251. ** and assertion failures.
  252. */
  253. WWDebug_Install_Message_Handler(wwdebug_message_handler);
  254. WWDebug_Install_Assert_Handler(wwdebug_assert_handler);
  255. WWDebug_Install_Trigger_Handler(wwdebug_trigger_handler);
  256. }
  257. void Shutdown_Debug(void)
  258. {
  259. /*
  260. ** Remove message handler functions for the WWDebug messages
  261. ** and assertion failures.
  262. */
  263. WWDebug_Install_Message_Handler(NULL);
  264. WWDebug_Install_Assert_Handler(NULL);
  265. WWDebug_Install_Trigger_Handler(NULL);
  266. }
  267. void wwdebug_message_handler(const char * message)
  268. {
  269. /*
  270. ** Hand the message off to the scrolling debug screen
  271. */
  272. // Debug_Say((message));
  273. }
  274. void wwdebug_assert_handler(const char * message)
  275. {
  276. /*
  277. ** Hand the message off to the scrolling debug screen
  278. */
  279. // Debug_Say((message));
  280. /*
  281. ** break into the debugger
  282. */
  283. _asm int 0x03;
  284. }
  285. bool wwdebug_trigger_handler(int trigger_num)
  286. {
  287. return Keyboard->Down(trigger_num);
  288. }
  289. void Debug_Refs(void)
  290. {
  291. #ifndef NDEBUG
  292. char buf[1024];
  293. if (RefCountClass::Total_Refs() > 0) {
  294. sprintf(buf,"Main Looop End %d refs\n", RefCountClass::Total_Refs());
  295. MessageBox(NULL,buf,"Ref Debugging",MB_OK);
  296. }
  297. SList<ActiveRefStruct> * reflist = RefCountClass::Get_Active_Ref_List();
  298. SLNode<ActiveRefStruct> * objnode;
  299. for ( objnode = reflist->Head(); objnode; objnode = objnode->Next()) {
  300. ActiveRefStruct * ref = objnode->Data();
  301. sprintf(buf,"Active Ref: %s Line: %d Pointer: %p\n", ref->File,ref->Line,ref->Object);
  302. if (MessageBox(NULL,buf,"Ref Debugging",MB_OKCANCEL) == IDCANCEL) {
  303. break;
  304. }
  305. }
  306. #endif
  307. }