QGearsBackgroundFileSerializer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-10 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/QGearsBackgroundFileSerializer.h"
  23. #include <OgreException.h>
  24. #include <OgreLogManager.h>
  25. #include <OgreStringConverter.h>
  26. namespace QGears
  27. {
  28. //---------------------------------------------------------------------
  29. const String BackgroundFileSerializer::SECTION_NAME_PALETTE("PALETTE");
  30. const String BackgroundFileSerializer::SECTION_NAME_BACK ("BACK");
  31. const String BackgroundFileSerializer::SECTION_NAME_TEXTURE("TEXTURE");
  32. const String BackgroundFileSerializer::TAG_FILE_END ("END");
  33. const Ogre::Real BackgroundFileSerializer::unknown_24_SCALE( 10000000.0 );
  34. //---------------------------------------------------------------------
  35. BackgroundFileSerializer::BackgroundFileSerializer() :
  36. Serializer()
  37. {
  38. }
  39. //---------------------------------------------------------------------
  40. BackgroundFileSerializer::~BackgroundFileSerializer()
  41. {
  42. }
  43. //---------------------------------------------------------------------
  44. void
  45. BackgroundFileSerializer::importBackgroundFile( Ogre::DataStreamPtr &stream
  46. ,BackgroundFile *pDest )
  47. {
  48. readFileHeader( stream );
  49. read1ByteBool( stream, pDest->getLayers()[0].enabled );
  50. readPallete( stream, pDest );
  51. stream->skip( 2 * 2 ); // 2 * uint16 unused;
  52. readBackground( stream, pDest );
  53. readTexture( stream, pDest );
  54. readEnd( stream );
  55. }
  56. //---------------------------------------------------------------------
  57. void
  58. BackgroundFileSerializer::readFileHeader( Ogre::DataStreamPtr &stream )
  59. {
  60. uint16 data[2];
  61. readShorts( stream, data, 2 );
  62. m_header.unused = data[0];
  63. m_header.sort_sprites_by_palette = data[1];
  64. }
  65. //---------------------------------------------------------------------
  66. void
  67. BackgroundFileSerializer::readSectionHeader( Ogre::DataStreamPtr &stream
  68. ,const String &section_name )
  69. {
  70. String actual( readString( stream, section_name.size() ) );
  71. if( actual != section_name )
  72. {
  73. Ogre::LogManager::getSingleton().stream()
  74. << "Warming: Section didn't start with"
  75. << " expected String '" << section_name << "'"
  76. << " actual was '" << actual << "'";
  77. }
  78. }
  79. //---------------------------------------------------------------------
  80. void
  81. BackgroundFileSerializer::readEnd( Ogre::DataStreamPtr &stream )
  82. {
  83. readEndString( stream, TAG_FILE_END );
  84. }
  85. //---------------------------------------------------------------------
  86. void
  87. BackgroundFileSerializer::readPallete( Ogre::DataStreamPtr &stream
  88. ,BackgroundFile *pDest )
  89. {
  90. readSectionHeader( stream, SECTION_NAME_PALETTE );
  91. stream->read( pDest->getPalette(), BackgroundFile::PALETTE_ENTRY_COUNT );
  92. }
  93. //---------------------------------------------------------------------
  94. void
  95. BackgroundFileSerializer::readBackground( Ogre::DataStreamPtr &stream
  96. ,BackgroundFile *pDest )
  97. {
  98. readSectionHeader( stream, SECTION_NAME_BACK );
  99. Layer *layer( pDest->getLayers() );
  100. for( size_t i( 0 ); i < BackgroundFile::LAYER_COUNT; ++i, ++layer )
  101. {
  102. if( i != 0 )
  103. {
  104. read1ByteBool( stream, layer->enabled );
  105. }
  106. readLayer( stream, layer, i );
  107. }
  108. }
  109. //---------------------------------------------------------------------
  110. void
  111. BackgroundFileSerializer::readLayer( Ogre::DataStreamPtr &stream
  112. ,Layer *pDest, size_t layer_index )
  113. {
  114. if( !pDest->enabled ) return;
  115. uint16 tmp[4], sprite_count;
  116. readShorts( stream, tmp, 4 );
  117. pDest->width = tmp[0];
  118. pDest->height = tmp[1];
  119. sprite_count = tmp[2];
  120. pDest->unknown_06 = tmp[3];
  121. switch( layer_index )
  122. {
  123. case 1:
  124. readShorts( stream, pDest->unknown_08, 3 );
  125. case 2:
  126. case 3:
  127. readShorts( stream, pDest->unknown_0E, 4 );
  128. }
  129. stream->skip( 2 * 2 ); // 2 * uint16 unused;
  130. readVector( stream, pDest->sprites, sprite_count );
  131. }
  132. //---------------------------------------------------------------------
  133. void
  134. BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, SpriteData &pDest )
  135. {
  136. readObject( stream, pDest.dst );
  137. readShorts( stream, pDest.unknown_04, 2 );
  138. readObject( stream, pDest.src );
  139. readShorts( stream, pDest.unknown_0C, 4 );
  140. readShort( stream, pDest.palette_page );
  141. readShort( stream, pDest.depth );
  142. stream->read( pDest.flags_18, sizeof( pDest.flags_18 ) );
  143. uint8 flags[2];
  144. stream->read( flags, sizeof( flags ) );
  145. pDest.flags_20[0] = flags[0] > 0;
  146. pDest.flags_20[1] = flags[1] > 0;
  147. readShort( stream, pDest.unknown_1C );
  148. readShort( stream, pDest.data_page );
  149. readShort( stream, pDest.unknown_20 );
  150. readShort( stream, pDest.unknown_22 );
  151. readObject( stream, pDest.unknown_24 );
  152. pDest.unknown_24 /= unknown_24_SCALE;
  153. stream->skip( 2 * 2 ); // 2 * uint16 unused
  154. }
  155. //---------------------------------------------------------------------
  156. void
  157. BackgroundFileSerializer::readTexture( Ogre::DataStreamPtr &stream
  158. ,BackgroundFile *pDest )
  159. {
  160. readSectionHeader( stream, SECTION_NAME_TEXTURE );
  161. Page *pages( pDest->getPages() );
  162. for( size_t i(0); i < BackgroundFile::PAGE_COUNT; ++i )
  163. {
  164. readObject( stream, pages[i] );
  165. }
  166. }
  167. //---------------------------------------------------------------------
  168. void
  169. BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, Page &pDest )
  170. {
  171. read2ByteBool( stream, pDest.enabled );
  172. if( !pDest.enabled ) return;
  173. readShort( stream, pDest.unknown_02 );
  174. readShort( stream, pDest.value_size );
  175. if( pDest.value_size != 1 && pDest.value_size != 2 )
  176. {
  177. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  178. ,"Page value_size other then 1 and 2 is not supported"
  179. ,"BackgroundFileSerializer::readObject" );
  180. }
  181. if( pDest.value_size == 2 )
  182. {
  183. Ogre::LogManager::getSingleton().stream()
  184. << "Warning: Page value_size == 2 @" << stream->tell();
  185. }
  186. size_t data_size( pDest.value_size * BackgroundFile::PAGE_DATA_SIZE );
  187. pDest.data.clear();
  188. pDest.data.resize( data_size );
  189. stream->read( &pDest.data[0], data_size );
  190. }
  191. //---------------------------------------------------------------------
  192. }