QGearsBackgroundFileSerializer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include "common/TypeDefine.h"
  17. #include "data/QGearsBackgroundFile.h"
  18. #include "data/QGearsSerializer.h"
  19. namespace QGears{
  20. /**
  21. * Handles the serialization of background files.
  22. */
  23. class BackgroundFileSerializer : public Serializer{
  24. public:
  25. /**
  26. * Constructor.
  27. */
  28. BackgroundFileSerializer();
  29. /**
  30. * Destructor.
  31. */
  32. virtual ~BackgroundFileSerializer();
  33. /**
  34. * Imports a background file.
  35. *
  36. * @param stream[in] The contents of the background file.
  37. * @param dest[out] The background file.
  38. */
  39. void ImportBackgroundFile(
  40. Ogre::DataStreamPtr &stream, BackgroundFile *dest
  41. );
  42. enum {
  43. /**
  44. * Bitmask for red colour.
  45. */
  46. BIT_MASK_RED = 0xF800,
  47. /**
  48. * Bitmask for green colour.
  49. */
  50. BIT_MASK_GREEN = 0x07C0,
  51. /**
  52. * Bitmask for blue colour.
  53. */
  54. BIT_MASK_BLUE = 0x001F,
  55. /**
  56. * @todo Understand and document.
  57. */
  58. BIT_SIZE = 0x001F,
  59. /**
  60. * Bitmask for RGB colour.
  61. */
  62. BIT_MASK_RGB = BIT_MASK_BLUE | BIT_MASK_GREEN | BIT_MASK_RED,
  63. /**
  64. * @todo Understand and document.
  65. */
  66. SPRITE_DST_MAX = 1024
  67. };
  68. /**
  69. * A background file header.
  70. */
  71. struct Header{
  72. /**
  73. * Unused data.
  74. */
  75. uint16 unused;
  76. /**
  77. * @todo Understand and document.
  78. */
  79. uint16 sort_sprites_by_palette;
  80. };
  81. typedef BackgroundFile::Layer Layer;
  82. typedef BackgroundFile::SpriteData SpriteData;
  83. typedef BackgroundFile::SpriteList SpriteList;
  84. typedef BackgroundFile::Page Page;
  85. typedef BackgroundFile::Color Color;
  86. protected:
  87. /**
  88. * Reads a background file header.
  89. *
  90. * @param stream[in] The contents of the header.
  91. */
  92. virtual void ReadFileHeader(Ogre::DataStreamPtr &stream);
  93. /**
  94. * Reads a section of a background file header.
  95. *
  96. * @param stream[in] The contents of the header.
  97. * @param section_name[in] The name of the section to read.
  98. */
  99. virtual void ReadSectionHeader(
  100. Ogre::DataStreamPtr &stream, const String &section_name
  101. );
  102. /**
  103. * Reads pallete data from a background file.
  104. *
  105. * @param stream[in] Input stream.
  106. * @param dest[out] The data will be set on this file.
  107. */
  108. virtual void ReadPallete(
  109. Ogre::DataStreamPtr &stream, BackgroundFile *dest
  110. );
  111. /**
  112. * Reads background data from a background file.
  113. *
  114. * @param stream[in] Input stream.
  115. * @param dest[out] The data will be set on this file.
  116. */
  117. virtual void ReadBackground(
  118. Ogre::DataStreamPtr &stream, BackgroundFile *dest
  119. );
  120. /**
  121. * Reads texture data from a background file.
  122. *
  123. * @param stream[in] Input stream.
  124. * @param dest[out] The data will be set on this file.
  125. */
  126. virtual void ReadTexture(
  127. Ogre::DataStreamPtr &stream, BackgroundFile *dest
  128. );
  129. /**
  130. * @todo Understand and document.
  131. *
  132. * @param stream[in] Input stream.
  133. */
  134. virtual void ReadEnd(Ogre::DataStreamPtr &stream);
  135. /**
  136. * Reads a layer from a background file.
  137. *
  138. * @param stream[in] Input stream.
  139. * @param dest[out] The layer info will be loaded here.
  140. * @param layer_index[in] Index of the layer to read.
  141. */
  142. virtual void ReadLayer(
  143. Ogre::DataStreamPtr &stream, Layer *dest, size_t layer_index
  144. );
  145. /**
  146. * Reads an object as a sprite.
  147. *
  148. * @param stream[in] Input data.
  149. * @param dest[out] The formed sprite.
  150. */
  151. virtual void readObject(
  152. Ogre::DataStreamPtr &stream, SpriteData &dest
  153. );
  154. /**
  155. * Reads an object as colour data.
  156. *
  157. * @param stream[in] Input data.
  158. * @param dest[out] The formed colour data.
  159. */
  160. virtual void readObject(Ogre::DataStreamPtr &stream, Color &dest);
  161. /**
  162. * Reads an object as a background page.
  163. *
  164. * @param stream[in] Input data.
  165. * @param dest[out] The formed page.
  166. */
  167. virtual void readObject(Ogre::DataStreamPtr &stream, Page &dest);
  168. using Serializer::readObject;
  169. /**
  170. * Reads a stream as a vector.
  171. *
  172. * @param stream[in] The input stream.
  173. * @param dest[out] The vector data will be loaded here.
  174. * @param count[in] Data units to copy.
  175. */
  176. template<typename ValueType> void ReadVector(
  177. Ogre::DataStreamPtr &stream, std::vector<ValueType> &dest,
  178. size_t count
  179. ){
  180. dest.clear();
  181. dest.reserve(count);
  182. for(size_t i(count); i --;){
  183. ValueType in_tmp;
  184. readObject(stream, in_tmp);
  185. dest.push_back(in_tmp);
  186. }
  187. }
  188. /**
  189. * Name of the palette section in the file.
  190. */
  191. static const String SECTION_NAME_PALETTE;
  192. /**
  193. * Name of the back section in the file.
  194. */
  195. static const String SECTION_NAME_BACK;
  196. /**
  197. * Name of the texture section in the file.
  198. */
  199. static const String SECTION_NAME_TEXTURE;
  200. /**
  201. * End-of-file tag.
  202. */
  203. static const String TAG_FILE_END;
  204. /**
  205. * @todo Understand and document.
  206. */
  207. static const Ogre::Real SRC_BIG_SCALE;
  208. private:
  209. /**
  210. * Removes malformed sprites from the map.
  211. *
  212. * @param[in|out] The list from which to remove malformed sprites.
  213. * @todo Why are they malformed?
  214. */
  215. void RemoveBuggySprites(SpriteList &sprites);
  216. /**
  217. * The file header.
  218. */
  219. Header header_;
  220. /**
  221. * @todo Understand and document.
  222. */
  223. size_t layer_index_;
  224. };
  225. }