QGearsFLevelFile.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-24 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/QGearsFLevelFile.h"
  23. #include <OgreLogManager.h>
  24. #include <OgreTextureManager.h>
  25. #include "common/FF7NameLookup.h"
  26. #include "common/QGearsStringUtil.h"
  27. #include "data/QGearsAFileManager.h"
  28. #include "data/QGearsFLevelBackground2DLoader.h"
  29. #include "data/QGearsFLevelFileSerializer.h"
  30. #include "data/QGearsFLevelTextureLoader.h"
  31. #include "data/QGearsHRCFileManager.h"
  32. #include "map/QGearsBackground2DFileManager.h"
  33. #include "FF7Common.h"
  34. #include "core/Logger.h"
  35. namespace QGears
  36. {
  37. //---------------------------------------------------------------------
  38. const String FLevelFile::RESOURCE_TYPE( "QGearsFLevelFile" );
  39. const String FLevelFile::SUFFIX_BACKGROUND_TEXTURE( "/texture" );
  40. const String FLevelFile::SUFFIX_BACKGROUND_2D ( "/background_2d" );
  41. //--------------------------------------------------------------------------
  42. FLevelFile::FLevelFile( Ogre::ResourceManager *creator
  43. ,const String &name, Ogre::ResourceHandle handle
  44. ,const String &group, bool isManual
  45. ,Ogre::ManualResourceLoader *loader ) :
  46. Resource( creator, name, handle, group, isManual, loader )
  47. ,m_background_texture_loader( nullptr )
  48. ,m_background_2d_loader( nullptr )
  49. {
  50. createParamDictionary( getResourceType() );
  51. }
  52. //--------------------------------------------------------------------------
  53. FLevelFile::~FLevelFile()
  54. {
  55. if( m_background_texture_loader )
  56. {
  57. assert( !m_background_texture.isNull() );
  58. Ogre::TextureManager::getSingleton().remove( m_background_texture->getHandle() );
  59. delete m_background_texture_loader;
  60. m_background_texture_loader = nullptr;
  61. }
  62. m_background_texture.setNull();
  63. if( m_background_2d_loader )
  64. {
  65. assert( !m_background_2d.isNull() );
  66. Background2DFileManager::getSingleton().remove( m_background_2d->getHandle() );
  67. delete m_background_2d_loader;
  68. m_background_2d_loader = nullptr;
  69. }
  70. m_background_2d.setNull();
  71. unload();
  72. }
  73. //--------------------------------------------------------------------------
  74. void
  75. FLevelFile::loadImpl()
  76. {
  77. FLevelFileSerializer serializer;
  78. Ogre::DataStreamPtr stream( openResource() );
  79. if (stream.isNull())
  80. {
  81. LOG_ERROR("Failed to open resource");
  82. }
  83. serializer.importFLevelFile( stream, this );
  84. String background_texture_name( getBackgroundTextureName() );
  85. if( m_background_texture_loader == nullptr )
  86. {
  87. m_background_texture_loader = new FLevelTextureLoader( *this );
  88. m_background_texture = Ogre::TextureManager::getSingleton().create( background_texture_name, mGroup, true, m_background_texture_loader );
  89. }
  90. String background_2d_name( getBackground2DName() );
  91. if( m_background_2d_loader == nullptr )
  92. {
  93. m_background_2d_loader = new FLevelBackground2DLoader( *this );
  94. m_background_2d = Background2DFileManager::getSingleton().createResource( background_2d_name, mGroup, true, m_background_2d_loader ).staticCast<Background2DFile>();
  95. }
  96. }
  97. //--------------------------------------------------------------------------
  98. void FLevelFile::loadModels()
  99. {
  100. HRCFileManager &hrc_mgr( HRCFileManager::getSingleton() );
  101. ModelList &models( m_model_list->getModels() );
  102. ModelList::const_iterator it ( models.begin() )
  103. , it_end( models.end() );
  104. while( it != it_end )
  105. {
  106. String hrc_name( it->hrc_name );
  107. Ogre::LogManager::getSingleton().stream()
  108. << "Loading Model: " << hrc_name;
  109. StringUtil::toLowerCase( hrc_name );
  110. HRCFilePtr hrc = hrc_mgr.load( hrc_name, mGroup ).staticCast<HRCFile>();
  111. loadAnimations( hrc, it->animations );
  112. m_hrc_files.push_back( hrc );
  113. ++it;
  114. }
  115. }
  116. //--------------------------------------------------------------------------
  117. void
  118. FLevelFile::loadAnimations( const HRCFilePtr &model, const AnimationList &animations )
  119. {
  120. AFileManager &a_mgr( AFileManager::getSingleton() );
  121. AnimationList::const_iterator it ( animations.begin() )
  122. , it_end( animations.end() );
  123. while( it != it_end )
  124. {
  125. String animation_name;
  126. StringUtil::splitBase( it->name, animation_name );
  127. StringUtil::toLowerCase( animation_name );
  128. String animation_filename( animation_name + FF7::EXT_A );
  129. AFilePtr animation = a_mgr.load( animation_filename, model->getGroup() ).staticCast<AFile>();
  130. animation_name = FF7::NameLookup::animation( animation_name );
  131. Ogre::LogManager::getSingleton().stream()
  132. << " Adding Animation: " << animation_name;
  133. animation->addTo( model->getSkeleton(), animation_name );
  134. ++it;
  135. }
  136. }
  137. //--------------------------------------------------------------------------
  138. void
  139. FLevelFile::unloadImpl()
  140. {
  141. m_background.setNull();
  142. m_camera_matrix.setNull();
  143. m_palette.setNull();
  144. m_model_list.setNull();
  145. m_walkmesh.setNull();
  146. m_hrc_files.clear();
  147. }
  148. //--------------------------------------------------------------------------
  149. const String&
  150. FLevelFile::getResourceType( void ) const
  151. {
  152. return RESOURCE_TYPE;
  153. }
  154. //--------------------------------------------------------------------------
  155. size_t
  156. FLevelFile::calculateSize() const
  157. {
  158. // data is only stored in section resources
  159. return 0;
  160. }
  161. //--------------------------------------------------------------------------
  162. const BackgroundFilePtr&
  163. FLevelFile::getBackground( void ) const
  164. {
  165. return m_background;
  166. }
  167. //--------------------------------------------------------------------------
  168. void
  169. FLevelFile::setBackground( const BackgroundFilePtr &background )
  170. {
  171. m_background = background;
  172. }
  173. //--------------------------------------------------------------------------
  174. const CameraMatrixFilePtr&
  175. FLevelFile::getCameraMatrix( void ) const
  176. {
  177. return m_camera_matrix;
  178. }
  179. //--------------------------------------------------------------------------
  180. void
  181. FLevelFile::setCameraMatrix( const CameraMatrixFilePtr &camera_matrix )
  182. {
  183. m_camera_matrix = camera_matrix;
  184. }
  185. //--------------------------------------------------------------------------
  186. const PaletteFilePtr&
  187. FLevelFile::getPalette( void ) const
  188. {
  189. return m_palette;
  190. }
  191. //--------------------------------------------------------------------------
  192. void
  193. FLevelFile::setPalette( const PaletteFilePtr &palette )
  194. {
  195. m_palette = palette;
  196. }
  197. //--------------------------------------------------------------------------
  198. const ModelListFilePtr&
  199. FLevelFile::getModelList() const
  200. {
  201. return m_model_list;
  202. }
  203. //--------------------------------------------------------------------------
  204. void
  205. FLevelFile::setModelList(const ModelListFilePtr &model_list)
  206. {
  207. m_model_list = model_list;
  208. }
  209. //--------------------------------------------------------------------------
  210. const WalkmeshFilePtr&
  211. FLevelFile::getWalkmesh() const
  212. {
  213. return m_walkmesh;
  214. }
  215. //--------------------------------------------------------------------------
  216. void
  217. FLevelFile::setWalkmesh( const WalkmeshFilePtr &walkmesh )
  218. {
  219. m_walkmesh = walkmesh;
  220. }
  221. //--------------------------------------------------------------------------
  222. String
  223. FLevelFile::getBackgroundTextureName( void ) const
  224. {
  225. String base_name;
  226. StringUtil::splitFull( getName(), base_name );
  227. base_name += SUFFIX_BACKGROUND_TEXTURE;
  228. return base_name;
  229. }
  230. //--------------------------------------------------------------------------
  231. String
  232. FLevelFile::getBackground2DName( void ) const
  233. {
  234. String base_name;
  235. StringUtil::splitFull( getName(), base_name );
  236. base_name += SUFFIX_BACKGROUND_2D;
  237. return base_name;
  238. }
  239. //--------------------------------------------------------------------------
  240. }