QGearsBackgroundFileSerializer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "data/QGearsPaletteFileSerializer.h"
  24. #include <OgreException.h>
  25. #include <OgreLogManager.h>
  26. #include <OgreStringConverter.h>
  27. namespace QGears
  28. {
  29. //---------------------------------------------------------------------
  30. const String BackgroundFileSerializer::SECTION_NAME_PALETTE("PALETTE");
  31. const String BackgroundFileSerializer::SECTION_NAME_BACK ("BACK");
  32. const String BackgroundFileSerializer::SECTION_NAME_TEXTURE("TEXTURE");
  33. const String BackgroundFileSerializer::TAG_FILE_END ("END");
  34. const Ogre::Real BackgroundFileSerializer::unknown_24_SCALE( 10000000.0 );
  35. //---------------------------------------------------------------------
  36. BackgroundFileSerializer::BackgroundFileSerializer() :
  37. Serializer()
  38. {
  39. }
  40. //---------------------------------------------------------------------
  41. BackgroundFileSerializer::~BackgroundFileSerializer()
  42. {
  43. }
  44. //---------------------------------------------------------------------
  45. void
  46. BackgroundFileSerializer::importBackgroundFile( Ogre::DataStreamPtr &stream
  47. ,BackgroundFile *pDest )
  48. {
  49. readFileHeader( stream );
  50. read1ByteBool( stream, pDest->getLayers()[0].enabled );
  51. readPallete( stream, pDest );
  52. stream->skip( 2 * 2 ); // 2 * uint16 unused;
  53. readBackground( stream, pDest );
  54. readTexture( stream, pDest );
  55. readEnd( stream );
  56. }
  57. //---------------------------------------------------------------------
  58. void
  59. BackgroundFileSerializer::readFileHeader( Ogre::DataStreamPtr &stream )
  60. {
  61. uint16 data[2];
  62. readShorts( stream, data, 2 );
  63. m_header.unused = data[0];
  64. m_header.sort_sprites_by_palette = data[1];
  65. }
  66. //---------------------------------------------------------------------
  67. void
  68. BackgroundFileSerializer::readSectionHeader( Ogre::DataStreamPtr &stream
  69. ,const String &section_name )
  70. {
  71. String actual( readString( stream, section_name.size() ) );
  72. if( actual != section_name )
  73. {
  74. Ogre::LogManager::getSingleton().stream()
  75. << "Warming: Section didn't start with"
  76. << " expected String '" << section_name << "'"
  77. << " actual was '" << actual << "'";
  78. }
  79. }
  80. //---------------------------------------------------------------------
  81. void
  82. BackgroundFileSerializer::readEnd( Ogre::DataStreamPtr &stream )
  83. {
  84. readEndString( stream, TAG_FILE_END );
  85. }
  86. //---------------------------------------------------------------------
  87. void
  88. BackgroundFileSerializer::readPallete( Ogre::DataStreamPtr &stream
  89. ,BackgroundFile *pDest )
  90. {
  91. readSectionHeader( stream, SECTION_NAME_PALETTE );
  92. stream->read( pDest->getPalette(), BackgroundFile::PALETTE_ENTRY_COUNT );
  93. }
  94. //---------------------------------------------------------------------
  95. void
  96. BackgroundFileSerializer::readBackground( Ogre::DataStreamPtr &stream
  97. ,BackgroundFile *pDest )
  98. {
  99. readSectionHeader( stream, SECTION_NAME_BACK );
  100. Layer *layer( pDest->getLayers() );
  101. for( size_t i( 0 ); i < BackgroundFile::LAYER_COUNT; ++i, ++layer )
  102. {
  103. if( i != 0 )
  104. {
  105. read1ByteBool( stream, layer->enabled );
  106. }
  107. readLayer( stream, layer, i );
  108. }
  109. }
  110. //---------------------------------------------------------------------
  111. void
  112. BackgroundFileSerializer::readLayer( Ogre::DataStreamPtr &stream
  113. ,Layer *pDest, size_t layer_index )
  114. {
  115. if( !pDest->enabled ) return;
  116. uint16 tmp[4], sprite_count;
  117. readShorts( stream, tmp, 4 );
  118. pDest->width = tmp[0];
  119. pDest->height = tmp[1];
  120. sprite_count = tmp[2];
  121. pDest->unknown_06 = tmp[3];
  122. switch( layer_index )
  123. {
  124. case 1:
  125. readShorts( stream, pDest->unknown_08, 3 );
  126. case 2:
  127. case 3:
  128. readShorts( stream, pDest->unknown_0E, 4 );
  129. }
  130. stream->skip( 2 * 2 ); // 2 * uint16 unused;
  131. readVector( stream, pDest->sprites, sprite_count );
  132. }
  133. //---------------------------------------------------------------------
  134. void
  135. BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, SpriteData &pDest )
  136. {
  137. readObject( stream, pDest.dst );
  138. readShorts( stream, pDest.unknown_04, 2 );
  139. readObject( stream, pDest.src );
  140. readShorts( stream, pDest.unknown_0C, 4 );
  141. readShort( stream, pDest.palette_page );
  142. readShort( stream, pDest.depth );
  143. stream->read( pDest.flags_18, sizeof( pDest.flags_18 ) );
  144. uint8 flags[2];
  145. stream->read( flags, sizeof( flags ) );
  146. pDest.flags_20[0] = flags[0] > 0;
  147. pDest.flags_20[1] = flags[1] > 0;
  148. readShort( stream, pDest.unknown_1C );
  149. readShort( stream, pDest.data_page );
  150. readShort( stream, pDest.data_page2 );
  151. // when data_page2 != 0, it must be used instead of data_page
  152. if ( pDest.data_page2 )
  153. {
  154. pDest.data_page = pDest.data_page2;
  155. }
  156. readShort( stream, pDest.colourDepth );
  157. readObject( stream, pDest.unknown_24 );
  158. pDest.unknown_24 /= unknown_24_SCALE;
  159. stream->skip( 2 * 2 ); // 2 * uint16 unused
  160. }
  161. //---------------------------------------------------------------------
  162. void
  163. BackgroundFileSerializer::readTexture( Ogre::DataStreamPtr &stream
  164. ,BackgroundFile *pDest )
  165. {
  166. readSectionHeader( stream, SECTION_NAME_TEXTURE );
  167. Page *pages( pDest->getPages() );
  168. for( size_t i(0); i < BackgroundFile::PAGE_COUNT; ++i )
  169. {
  170. readObject( stream, pages[i] );
  171. }
  172. }
  173. //---------------------------------------------------------------------
  174. void
  175. BackgroundFileSerializer::readObject( Ogre::DataStreamPtr& stream, Color& pDest )
  176. {
  177. uint16 colour;
  178. readShort( stream, colour );
  179. pDest.r = static_cast<float>(( colour & BIT_MASK_RED ) >> 11);
  180. pDest.g = static_cast<float>(( colour & BIT_MASK_GREEN ) >> 6);
  181. pDest.b = static_cast<float>(colour & BIT_MASK_BLUE);
  182. pDest /= BIT_SIZE;
  183. pDest.a = 1.0f;
  184. if ( colour == 0 )
  185. {
  186. pDest.a = 0.0f;
  187. }
  188. }
  189. //---------------------------------------------------------------------
  190. void
  191. BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, Page &pDest )
  192. {
  193. read2ByteBool( stream, pDest.enabled );
  194. if( !pDest.enabled ) return;
  195. readShort( stream, pDest.unknown_02 );
  196. readShort( stream, pDest.value_size );
  197. if( pDest.value_size != 1 && pDest.value_size != 2 )
  198. {
  199. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  200. ,"Page value_size other then 1 and 2 is not supported"
  201. ,"BackgroundFileSerializer::readObject" );
  202. }
  203. size_t color_count( BackgroundFile::PAGE_DATA_SIZE );
  204. pDest.colors.clear();
  205. pDest.data.clear();
  206. if( pDest.value_size == 2 )
  207. {
  208. pDest.colors.reserve( color_count );
  209. for( size_t i( color_count ); i--; )
  210. {
  211. Color colourDest;
  212. readObject( stream, colourDest );
  213. pDest.colors.push_back( colourDest );
  214. }
  215. }
  216. else
  217. {
  218. pDest.data.resize( color_count );
  219. stream->read( &pDest.data[0], color_count );
  220. }
  221. }
  222. //---------------------------------------------------------------------
  223. }