PhysTestDoc.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. // PhysTestDoc.cpp : implementation of the CPhysTestDoc class
  19. //
  20. #include "stdafx.h"
  21. #include "PhysTest.h"
  22. #include "PhysTestDoc.h"
  23. #include "MainFrm.h"
  24. #include "DataView.h"
  25. #include "ww3d.h"
  26. #include "ffactory.h"
  27. #include "pscene.h"
  28. #include "light.h"
  29. #include "rcfile.h"
  30. #include "assetmgr.h"
  31. #include "rendobj.h"
  32. #include "chunkio.h"
  33. #include "lev_file.h"
  34. #include "physlist.h"
  35. #include "saveload.h"
  36. #include "chunkio.h"
  37. #include "rawfile.h"
  38. #include "physstaticsavesystem.h"
  39. #include "physdynamicsavesystem.h"
  40. #include "PhysTestSaveSystem.h"
  41. #ifdef _DEBUG
  42. #define new DEBUG_NEW
  43. #undef THIS_FILE
  44. static char THIS_FILE[] = __FILE__;
  45. #endif
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Chunk ID's used by PhysTestDoc
  48. enum
  49. {
  50. PHYSTESTDOC_CHUNK_MAINFRAME = 0x03321990,
  51. PHYSTESTDOC_CHUNK_SYSTEMS,
  52. };
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CPhysTestDoc
  55. IMPLEMENT_DYNCREATE(CPhysTestDoc, CDocument)
  56. BEGIN_MESSAGE_MAP(CPhysTestDoc, CDocument)
  57. //{{AFX_MSG_MAP(CPhysTestDoc)
  58. // NOTE - the ClassWizard will add and remove mapping macros here.
  59. // DO NOT EDIT what you see in these blocks of generated code!
  60. //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CPhysTestDoc construction/destruction
  64. CPhysTestDoc::CPhysTestDoc() :
  65. Scene(NULL),
  66. Light(NULL),
  67. Origin(NULL)
  68. {
  69. }
  70. CPhysTestDoc::~CPhysTestDoc()
  71. {
  72. if (Scene) {
  73. Scene->Remove_All();
  74. }
  75. REF_PTR_RELEASE(Scene);
  76. REF_PTR_RELEASE(Light);
  77. REF_PTR_RELEASE(Origin);
  78. }
  79. void CPhysTestDoc::Init_Scene(void)
  80. {
  81. if (Scene == NULL)
  82. {
  83. // Instantiate a new scene
  84. Scene = NEW_REF(PhysicsSceneClass,());
  85. Scene->Enable_Dynamic_Projectors(true);
  86. // Were we successful in instantiating the scene object?
  87. ASSERT(Scene);
  88. if (Scene != NULL) {
  89. // Set up some collision groups.
  90. Scene->Enable_All_Collision_Detections(0);
  91. // Set some default ambient lighting
  92. Scene->Set_Ambient_Light(Vector3 (0.5F, 0.5F, 0.5F));
  93. // Create a new scene light
  94. if (Light == NULL) {
  95. Light = NEW_REF(LightClass,(LightClass::DIRECTIONAL));
  96. ASSERT(Light);
  97. if (Light) {
  98. // Create some default light settings
  99. Matrix3D transform(1);
  100. transform.Look_At(Vector3(0,0,0),Vector3(10,10,10),0.0f);
  101. Light->Set_Transform(transform);
  102. Light->Set_Attenuation_Model(LightClass::ATTENUATION_NONE);
  103. Light->Set_Flag (LightClass::NEAR_ATTENUATION, false);
  104. Light->Set_Flag (LightClass::FAR_ATTENUATION, false);
  105. Light->Set_Far_Attenuation_Range (1000000, 1000000);
  106. Light->Set_Intensity(1.0F);
  107. Light->Set_Force_Visible(true);
  108. Light->Set_Ambient(Vector3(0,0,0));
  109. Light->Set_Diffuse(Vector3(1, 1, 1));
  110. Light->Set_Specular(Vector3(1, 1, 1));
  111. // Add this light to the scene
  112. Light->Add(Scene);
  113. }
  114. }
  115. // Create an object at the Origin
  116. if (Origin == NULL) {
  117. ResourceFileClass mesh_file(NULL, "Axes.w3d");
  118. WW3DAssetManager::Get_Instance()->Load_3D_Assets(mesh_file);
  119. Origin = WW3DAssetManager::Get_Instance()->Create_Render_Obj("Axes");
  120. Origin->Set_Transform(Matrix3D(1));
  121. Scene->Add_Render_Object(Origin);
  122. }
  123. }
  124. }
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CPhysTestDoc serialization
  128. void CPhysTestDoc::Serialize(CArchive& ar)
  129. {
  130. assert(0);
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CPhysTestDoc diagnostics
  134. #ifdef _DEBUG
  135. void CPhysTestDoc::AssertValid() const
  136. {
  137. CDocument::AssertValid();
  138. }
  139. void CPhysTestDoc::Dump(CDumpContext& dc) const
  140. {
  141. CDocument::Dump(dc);
  142. }
  143. #endif //_DEBUG
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CPhysTestDoc commands
  146. BOOL CPhysTestDoc::OnNewDocument()
  147. {
  148. if (!CDocument::OnNewDocument())
  149. return FALSE;
  150. if (Scene) {
  151. Scene->Remove_All();
  152. WWASSERT(Light);
  153. if (Light) {
  154. Scene->Add_Render_Object(Light);
  155. }
  156. WWASSERT(Origin);
  157. if (Origin) {
  158. Scene->Add_Render_Object(Origin);
  159. }
  160. // rebuild the list view
  161. Get_Data_View()->Rebuild_Tree();
  162. }
  163. return TRUE;
  164. }
  165. BOOL CPhysTestDoc::OnOpenDocument(LPCTSTR lpszPathName)
  166. {
  167. Load_PHY_File(lpszPathName);
  168. Get_Data_View()->Rebuild_Tree();
  169. return TRUE;
  170. }
  171. BOOL CPhysTestDoc::OnSaveDocument(LPCTSTR lpszPathName)
  172. {
  173. Save_PHY_File(lpszPathName);
  174. return TRUE;
  175. }
  176. ///////////////////////////////////////////////////////////////
  177. //
  178. // Load_LEV_File
  179. //
  180. void CPhysTestDoc::Load_LEV_File(LPCTSTR lpszPathName)
  181. {
  182. #if 0
  183. WWASSERT(Scene);
  184. //
  185. // HACK HACK -- Force the current directory to be the directory
  186. // the file is located in.
  187. //
  188. if (::strrchr (lpszPathName, '\\')) {
  189. CString stringTemp = lpszPathName;
  190. stringTemp = stringTemp.Left ((long)::strrchr (lpszPathName, '\\') - (long)lpszPathName);
  191. ::SetCurrentDirectory (stringTemp);
  192. WW3D::Add_Search_Path (stringTemp);
  193. }
  194. LPCTSTR extension = ::strrchr (lpszPathName, '.');
  195. ASSERT(::lstrcmpi(extension,".lev") == 0);
  196. // load the LEV
  197. FileClass *file = DefaultFileFactory.Get_File (lpszPathName);
  198. file->Open();
  199. ChunkLoadClass cload(file);
  200. // should see a LEV_CHUNK_LEVEL
  201. cload.Open_Chunk();
  202. WWASSERT(cload.Cur_Chunk_ID() == LEV_CHUNK_LEVEL);
  203. // for now only look for a LEV_CHUNK_MAP
  204. cload.Open_Chunk();
  205. WWASSERT(cload.Cur_Chunk_ID() == LEV_CHUNK_MAP);
  206. // Physics Scene handles the tile map loading
  207. Scene->LEV_Load(cload);
  208. // close the LEV_CHUNK_MAP
  209. cload.Close_Chunk();
  210. // close the LEV_CHUNK_LEVEL
  211. cload.Close_Chunk();
  212. // done with the file!
  213. file->Close();
  214. DefaultFileFactory.Return_File(file);
  215. // Replace our default objects
  216. WWASSERT(Light);
  217. if (Light) {
  218. Scene->Add_Render_Object(Light);
  219. }
  220. WWASSERT(Origin);
  221. if (Origin) {
  222. Scene->Add_Render_Object(Origin);
  223. }
  224. #endif
  225. }
  226. ///////////////////////////////////////////////////////////////
  227. //
  228. // Load_W3D_File
  229. //
  230. void CPhysTestDoc::Load_W3D_File(LPCTSTR lpszPathName)
  231. {
  232. WW3DAssetManager::Get_Instance()->Load_3D_Assets(lpszPathName);
  233. // rebuild the list view
  234. Get_Data_View()->Rebuild_Tree();
  235. }
  236. ///////////////////////////////////////////////////////////////
  237. //
  238. // Load_PHY_File
  239. //
  240. void CPhysTestDoc::Load_PHY_File(LPCTSTR lpszPathName)
  241. {
  242. RawFileClass file(lpszPathName);
  243. WWASSERT(file);
  244. file.Open(FileClass::READ);
  245. ChunkLoadClass cload(&file);
  246. while (cload.Open_Chunk()) {
  247. switch(cload.Cur_Chunk_ID()) {
  248. case PHYSTESTDOC_CHUNK_SYSTEMS:
  249. SaveLoadSystemClass::Load( cload );
  250. break;
  251. };
  252. cload.Close_Chunk();
  253. }
  254. file.Close();
  255. }
  256. ///////////////////////////////////////////////////////////////
  257. //
  258. // Save_PHY_File
  259. //
  260. void CPhysTestDoc::Save_PHY_File(LPCTSTR lpszPathName)
  261. {
  262. RawFileClass file(lpszPathName);
  263. WWASSERT(file);
  264. file.Open(FileClass::WRITE);
  265. ChunkSaveClass csave(&file);
  266. csave.Begin_Chunk(PHYSTESTDOC_CHUNK_SYSTEMS);
  267. SaveLoadSystemClass::Save( csave, _PhysTestSaveSystem );
  268. SaveLoadSystemClass::Save( csave, _PhysStaticDataSaveSystem );
  269. SaveLoadSystemClass::Save( csave, _PhysStaticObjectsSaveSystem );
  270. SaveLoadSystemClass::Save( csave, _PhysDynamicSaveSystem );
  271. csave.End_Chunk();
  272. file.Close();
  273. }
  274. ///////////////////////////////////////////////////////////////
  275. //
  276. // Get_Data_View
  277. //
  278. CDataView * CPhysTestDoc::Get_Data_View(void)
  279. {
  280. CDataView * view = NULL;
  281. // Get a pointer to the main window
  282. CMainFrame * mainwnd = (CMainFrame *)::AfxGetMainWnd();
  283. if (mainwnd) {
  284. // Get the pane from the main window
  285. view = (CDataView *)mainwnd->Get_Pane(0,0);
  286. }
  287. // Return a pointer to the tree view
  288. return view;
  289. }
  290. ///////////////////////////////////////////////////////////////
  291. //
  292. // Get_Graphic_View
  293. //
  294. CGraphicView * CPhysTestDoc::Get_Graphic_View(void)
  295. {
  296. CGraphicView * view = NULL;
  297. // Get a pointer to the main window
  298. CMainFrame * mainwnd = (CMainFrame *)::AfxGetMainWnd();
  299. if (mainwnd) {
  300. // Get the pane from the main window
  301. view = (CGraphicView *)mainwnd->Get_Pane(0,1);
  302. }
  303. // Return a pointer to the graphic view
  304. return view;
  305. }
  306. ///////////////////////////////////////////////////////////////
  307. //
  308. // Add_Physics_Object - add a new object to be simulated
  309. //
  310. void CPhysTestDoc::Add_Physics_Object(PhysClass * obj)
  311. {
  312. if (obj == NULL) return;
  313. if (Scene == NULL) return;
  314. Scene->Add_Dynamic_Object(obj);
  315. // rebuild the list view
  316. Get_Data_View()->Rebuild_Tree();
  317. }
  318. int CPhysTestDoc::Get_Physics_Object_Count(void)
  319. {
  320. PhysListIterator it = Scene->Get_Dynamic_Object_Iterator();
  321. int count = 0;
  322. for (it.First(); !it.Is_Done(); it.Next()) {
  323. count++;
  324. }
  325. return count;
  326. }