Guest User

Thing.cpp

a guest
Nov 9th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "../eterbase/Debug.h"
  3. #include "Thing.h"
  4. #include "ThingInstance.h"
  5.  
  6. CGraphicThing::CGraphicThing(const char* c_szFileName) : CResource(c_szFileName)
  7. {
  8. Initialize();
  9. }
  10.  
  11. CGraphicThing::~CGraphicThing()
  12. {
  13. //OnClear();
  14. Clear();
  15. }
  16.  
  17. void CGraphicThing::Initialize()
  18. {
  19. m_pgrnFile = NULL;
  20. m_pgrnFileInfo = NULL;
  21. m_pgrnAni = NULL;
  22.  
  23. m_models = NULL;
  24. m_motions = NULL;
  25. }
  26.  
  27. void CGraphicThing::OnClear()
  28. {
  29. if (m_motions)
  30. delete [] m_motions;
  31.  
  32. if (m_models)
  33. delete [] m_models;
  34.  
  35. if (m_pgrnFile)
  36. GrannyFreeFile(m_pgrnFile);
  37.  
  38. Initialize();
  39. }
  40.  
  41. CGraphicThing::TType CGraphicThing::Type()
  42. {
  43. static TType s_type = StringToType("CGraphicThing");
  44. return s_type;
  45. }
  46.  
  47. bool CGraphicThing::OnIsEmpty() const
  48. {
  49. return m_pgrnFile ? false : true;
  50. }
  51.  
  52. bool CGraphicThing::OnIsType(TType type)
  53. {
  54. if (CGraphicThing::Type() == type)
  55. return true;
  56.  
  57. return CResource::OnIsType(type);
  58. }
  59.  
  60. bool CGraphicThing::CreateDeviceObjects()
  61. {
  62. if (!m_pgrnFileInfo)
  63. return true;
  64.  
  65. for (int m = 0; m < m_pgrnFileInfo->ModelCount; ++m)
  66. {
  67. CGrannyModel & rModel = m_models[m];
  68. rModel.CreateDeviceObjects();
  69. }
  70.  
  71. return true;
  72. }
  73.  
  74. void CGraphicThing::DestroyDeviceObjects()
  75. {
  76. if (!m_pgrnFileInfo)
  77. return;
  78.  
  79. for (int m = 0; m < m_pgrnFileInfo->ModelCount; ++m)
  80. {
  81. CGrannyModel & rModel = m_models[m];
  82. rModel.DestroyDeviceObjects();
  83. }
  84. }
  85.  
  86. bool CGraphicThing::CheckModelIndex(int iModel) const
  87. {
  88. if (!m_pgrnFileInfo)
  89. {
  90. Tracef("m_pgrnFileInfo == NULL: %s\n", GetFileName());
  91. return false;
  92. }
  93.  
  94. assert(m_pgrnFileInfo != NULL);
  95.  
  96. if (iModel < 0)
  97. return false;
  98.  
  99. if (iModel >= m_pgrnFileInfo->ModelCount)
  100. return false;
  101.  
  102. return true;
  103. }
  104.  
  105. bool CGraphicThing::CheckMotionIndex(int iMotion) const
  106. {
  107. // Temporary
  108. if (!m_pgrnFileInfo)
  109. return false;
  110. // Temporary
  111.  
  112. assert(m_pgrnFileInfo != NULL);
  113.  
  114. if (iMotion < 0)
  115. return false;
  116.  
  117. if (iMotion >= m_pgrnFileInfo->AnimationCount)
  118. return false;
  119.  
  120. return true;
  121. }
  122.  
  123. CGrannyModel * CGraphicThing::GetModelPointer(int iModel)
  124. {
  125. assert(CheckModelIndex(iModel));
  126. assert(m_models != NULL);
  127. return m_models + iModel;
  128. }
  129.  
  130. CGrannyMotion * CGraphicThing::GetMotionPointer(int iMotion)
  131. {
  132. assert(CheckMotionIndex(iMotion));
  133.  
  134. if (iMotion >= m_pgrnFileInfo->AnimationCount)
  135. return NULL;
  136.  
  137. assert(m_motions != NULL);
  138. return (m_motions + iMotion);
  139. }
  140.  
  141. int CGraphicThing::GetModelCount() const
  142. {
  143. if (!m_pgrnFileInfo)
  144. return 0;
  145.  
  146. return (m_pgrnFileInfo->ModelCount);
  147. }
  148.  
  149. int CGraphicThing::GetMotionCount() const
  150. {
  151. if (!m_pgrnFileInfo)
  152. return 0;
  153.  
  154. return (m_pgrnFileInfo->AnimationCount);
  155. }
  156.  
  157. bool CGraphicThing::OnLoad(int iSize, const void * c_pvBuf)
  158. {
  159. if (!c_pvBuf)
  160. return false;
  161.  
  162. m_pgrnFile = GrannyReadEntireFileFromMemory(iSize, (void *) c_pvBuf);
  163.  
  164. if (!m_pgrnFile)
  165. return false;
  166.  
  167. m_pgrnFileInfo = GrannyGetFileInfo(m_pgrnFile);
  168.  
  169. if (!m_pgrnFileInfo)
  170. return false;
  171.  
  172. LoadModels();
  173. LoadMotions();
  174. return true;
  175. }
  176.  
  177. // SUPPORT_LOCAL_TEXTURE
  178. static std::string gs_modelLocalPath;
  179.  
  180. const std::string& GetModelLocalPath()
  181. {
  182. return gs_modelLocalPath;
  183. }
  184. // END_OF_SUPPORT_LOCAL_TEXTURE
  185.  
  186. bool CGraphicThing::LoadModels()
  187. {
  188. assert(m_pgrnFile != NULL);
  189. assert(m_models == NULL);
  190.  
  191. if (m_pgrnFileInfo->ModelCount <= 0)
  192. return false;
  193.  
  194. // SUPPORT_LOCAL_TEXTURE
  195. const std::string& fileName = GetFileNameString();
  196.  
  197. //char localPath[256] = "";
  198. if (fileName.length() > 2 && fileName[1] != ':')
  199. {
  200. int sepPos = fileName.rfind('\\');
  201. gs_modelLocalPath.assign(fileName, 0, sepPos+1);
  202. }
  203. // END_OF_SUPPORT_LOCAL_TEXTURE
  204.  
  205. int modelCount = m_pgrnFileInfo->ModelCount;
  206.  
  207. m_models = new CGrannyModel[modelCount];
  208.  
  209. for (int m = 0; m < modelCount; ++m)
  210. {
  211. CGrannyModel & rModel = m_models[m];
  212. granny_model * pgrnModel = m_pgrnFileInfo->Models[m];
  213.  
  214. if (!rModel.CreateFromGrannyModelPointer(pgrnModel))
  215. return false;
  216. }
  217.  
  218. GrannyFreeFileSection(m_pgrnFile, GrannyStandardRigidVertexSection);
  219. GrannyFreeFileSection(m_pgrnFile, GrannyStandardRigidIndexSection);
  220. GrannyFreeFileSection(m_pgrnFile, GrannyStandardDeformableIndexSection);
  221. GrannyFreeFileSection(m_pgrnFile, GrannyStandardTextureSection);
  222. return true;
  223. }
  224.  
  225. bool CGraphicThing::LoadMotions()
  226. {
  227. assert(m_pgrnFile != NULL);
  228. assert(m_motions == NULL);
  229.  
  230. if (m_pgrnFileInfo->AnimationCount <= 0)
  231. return false;
  232.  
  233. int motionCount = m_pgrnFileInfo->AnimationCount;
  234.  
  235. m_motions = new CGrannyMotion[motionCount];
  236.  
  237. for (int m = 0; m < motionCount; ++m)
  238. if (!m_motions[m].BindGrannyAnimation(m_pgrnFileInfo->Animations[m]))
  239. return false;
  240.  
  241. return true;
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment