QGearsHRCFile.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-11 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsHRCFile.h"
  23. #include <OgreLogManager.h>
  24. #include <OgreMeshManager.h>
  25. #include <OgreSkeletonManager.h>
  26. //#include "common/FF7NameLookup.h"
  27. #include "../../SupportedGames/FinalFantasy7/include/common/FF7NameLookup.h"
  28. #include "common/QGearsStringUtil.h"
  29. #include "data/QGearsHRCFileSerializer.h"
  30. #include "data/QGearsHRCMeshLoader.h"
  31. #include "data/QGearsHRCSkeletonLoader.h"
  32. namespace QGears
  33. {
  34. //---------------------------------------------------------------------
  35. const String HRCFile::RESOURCE_TYPE( "QGearsHRCFile" );
  36. //---------------------------------------------------------------------
  37. HRCFile::HRCFile( Ogre::ResourceManager *creator
  38. ,const String &name, Ogre::ResourceHandle handle
  39. ,const String &group, bool isManual
  40. ,Ogre::ManualResourceLoader *loader ) :
  41. Ogre::Resource( creator, name, handle, group, isManual, loader )
  42. ,m_mesh_loader( NULL )
  43. ,m_skeleton_loader( NULL )
  44. {
  45. createParamDictionary( RESOURCE_TYPE );
  46. }
  47. //---------------------------------------------------------------------
  48. HRCFile::~HRCFile()
  49. {
  50. if( m_skeleton_loader )
  51. {
  52. Ogre::SkeletonManager::getSingleton().remove( m_skeleton->getHandle() );
  53. delete m_skeleton_loader;
  54. m_skeleton_loader = NULL;
  55. }
  56. if( m_mesh_loader )
  57. {
  58. Ogre::MeshManager::getSingleton().remove( m_mesh->getHandle() );
  59. delete m_mesh_loader;
  60. m_mesh_loader = NULL;
  61. }
  62. m_skeleton.setNull();
  63. m_mesh.setNull();
  64. unload();
  65. }
  66. //---------------------------------------------------------------------
  67. void
  68. HRCFile::loadImpl()
  69. {
  70. HRCFileSerializer serializer;
  71. Ogre::DataStreamPtr stream( Ogre::ResourceGroupManager::getSingleton().openResource( mName, mGroup, true, this ) );
  72. serializer.importHRCFile( stream, this );
  73. const String skeleton_file_name( getSkeletonFileName() );
  74. Ogre::SkeletonManager &skeleton_manager( Ogre::SkeletonManager::getSingleton() );
  75. m_skeleton = skeleton_manager.getByName( skeleton_file_name, mGroup );
  76. if( m_skeleton.isNull() )
  77. {
  78. assert( m_skeleton_loader == NULL );
  79. m_skeleton_loader = new HRCSkeletonLoader( *this );
  80. m_skeleton = skeleton_manager.create( skeleton_file_name, mGroup, true, m_skeleton_loader );
  81. }
  82. const String mesh_file_name( getMeshFileName() );
  83. Ogre::LogManager::getSingleton().stream()
  84. << "HRC::loadImpl(): mesh_file_name: " << mesh_file_name;
  85. Ogre::MeshManager &mesh_manager( Ogre::MeshManager::getSingleton() );
  86. m_mesh = mesh_manager.getByName( mesh_file_name, mGroup );
  87. if( m_mesh.isNull() )
  88. {
  89. assert( m_mesh_loader == NULL );
  90. m_mesh_loader = new HRCMeshLoader( *this );
  91. m_mesh = mesh_manager.create( mesh_file_name, mGroup, true, m_mesh_loader );
  92. }
  93. }
  94. //---------------------------------------------------------------------
  95. void
  96. HRCFile::unloadImpl()
  97. {
  98. m_skeleton_name.clear();
  99. m_bones.clear();
  100. }
  101. //---------------------------------------------------------------------
  102. size_t
  103. HRCFile::calculateSize( const Bone &bone ) const
  104. {
  105. size_t size_rsd_names( 0 );
  106. for( RSDNameList::const_iterator it( bone.rsd_names.begin() )
  107. ;it != bone.rsd_names.end()
  108. ;++it )
  109. {
  110. size_rsd_names += it->size();
  111. }
  112. return bone.name.size()
  113. +bone.parent.size()
  114. +sizeof( bone.length )
  115. +size_rsd_names;
  116. }
  117. //---------------------------------------------------------------------
  118. size_t
  119. HRCFile::calculateSize() const
  120. {
  121. size_t size_bones( 0 );
  122. for( BoneList::const_iterator it( m_bones.begin() )
  123. ;it != m_bones.end()
  124. ;++it )
  125. {
  126. size_bones += calculateSize( *it );
  127. }
  128. return m_skeleton_name.size() + size_bones;
  129. }
  130. //---------------------------------------------------------------------
  131. void
  132. HRCFile::setSkeletonName( const String &name )
  133. {
  134. m_skeleton_name = name;
  135. }
  136. //---------------------------------------------------------------------
  137. String
  138. HRCFile::getSkeletonFileName( void ) const
  139. {
  140. String path;
  141. StringUtil::splitPath( getName(), path );
  142. return path + m_skeleton_name + EXT_SKELETON;
  143. }
  144. //---------------------------------------------------------------------
  145. String
  146. HRCFile::getMeshFileName( void ) const
  147. {
  148. String base_name;
  149. StringUtil::splitBase( getName(), base_name );
  150. return FF7::NameLookup::model( base_name ) + EXT_MESH;
  151. }
  152. //---------------------------------------------------------------------
  153. Ogre::SkeletonPtr
  154. HRCFile::getSkeleton( void ) const
  155. {
  156. m_skeleton->load();
  157. return m_skeleton;
  158. }
  159. //---------------------------------------------------------------------
  160. }