QGearsBackgroundFile.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <array>
  17. #include <OgreResource.h>
  18. #include <OgreImage.h>
  19. #include <Ogre.h>
  20. #include "common/TypeDefine.h"
  21. #include "data/QGearsPaletteFile.h"
  22. namespace QGears{
  23. // TODO inherit from QGears Resource.
  24. /**
  25. * A background file representation.
  26. */
  27. class BackgroundFile : public Ogre::Resource{
  28. public:
  29. /**
  30. * Constructor.
  31. *
  32. * @param creator[in] Pointer to the ResourceManager that is
  33. * creating this resource.
  34. * @param name[in] The unique name of the resource.
  35. * @param handle[in] @todo Understand and document.
  36. * @param group[in] The name of the resource group to which this
  37. * resource belong.
  38. * @param is_manual[in] True if the resource is manually loaded,
  39. * false otherwise.
  40. * @param loader[in] Pointer to a ManualResourceLoader
  41. * implementation which will be called when the Resource wishes to
  42. * load (should be supplied if is_manual is set to true). It can be
  43. * null, but the Resource will never be able to reload if anything
  44. * ever causes it to unload. Therefore provision of a proper
  45. * ManualResourceLoader instance is strongly recommended.
  46. */
  47. BackgroundFile(
  48. Ogre::ResourceManager *creator, const String &name,
  49. Ogre::ResourceHandle handle, const String &group,
  50. bool is_manual = false, Ogre::ManualResourceLoader *loader = NULL
  51. );
  52. /**
  53. * Destructor.
  54. */
  55. virtual ~BackgroundFile();
  56. /**
  57. * The type of resource.
  58. */
  59. static const String RESOURCE_TYPE;
  60. enum {
  61. /**
  62. * Width of the background page, in pixels.
  63. *
  64. * Same as {@see PAGE_DATAH_EIGHT}.
  65. */
  66. PAGE_DATA_WIDTH = 256,
  67. /**
  68. * Height of the background page, in pixels.
  69. *
  70. * Same as {@see PAGE_DATA_WIDTH}.
  71. */
  72. PAGE_DATA_HEIGHT = PAGE_DATA_WIDTH,
  73. /**
  74. * Size of the background page, in pixels.
  75. *
  76. * Same as {@see PAGE_DATA_WIDTH} * {@see PAGE_DATA_HEIGHT}.
  77. */
  78. PAGE_DATA_SIZE = PAGE_DATA_WIDTH * PAGE_DATA_HEIGHT,
  79. /**
  80. * Width of sprites, in pixels.
  81. *
  82. * Same as {@see SPRITE_HEIGHT}.
  83. */
  84. SPRITE_WIDTH = 16,
  85. /**
  86. * Height of sprites, in pixels.
  87. *
  88. * Same as {@see SPRITE_WIDTH}.
  89. */
  90. SPRITE_HEIGHT = SPRITE_WIDTH,
  91. /**
  92. * Size of sprites, in pixels.
  93. *
  94. * Same as {@see SPRITE_WIDTH} * {@see SPRITE_HEIGHT}.
  95. */
  96. SPRITE_PIXEL_COUNT = SPRITE_WIDTH * SPRITE_HEIGHT,
  97. /**
  98. * Number of layers in a background.
  99. */
  100. LAYER_COUNT = 4,
  101. /**
  102. * Number of palettes in a background.
  103. */
  104. PALETTE_ENTRY_COUNT = 20,
  105. /**
  106. * Number of pages in a background.
  107. *
  108. * @todo What are pages?
  109. */
  110. PAGE_COUNT = 42
  111. };
  112. /**
  113. * Data for sprites.
  114. */
  115. struct SpriteData{
  116. /**
  117. * Pixel destination.
  118. */
  119. Pixel dst;
  120. /**
  121. * Unused data.
  122. */
  123. uint16 unknown_04[2];
  124. /**
  125. * Pixel source.
  126. */
  127. Pixel src;
  128. /**
  129. * Pixel alternate source.
  130. *
  131. * Used for special effects pages, when data_page2 != 0, it
  132. * must be used instead of {@see src}.
  133. */
  134. Pixel src2;
  135. /**
  136. * Sprite width.
  137. */
  138. uint16 width;
  139. /**
  140. * Sprite height.
  141. */
  142. uint16 height;
  143. /**
  144. * Palete page for the sprite
  145. */
  146. uint16 palette_page;
  147. /**
  148. * Sprite depth (Z-index).
  149. */
  150. uint16 depth;
  151. /**
  152. * Animation ID of the sprite.
  153. */
  154. uint8 animation_id;
  155. /**
  156. * Animation frame of the sprite.
  157. */
  158. uint8 animation_frame;
  159. /**
  160. * Indicates if the sprite uses color blending.
  161. */
  162. bool has_blending[2];
  163. /**
  164. * The type of blending used.
  165. */
  166. uint16 blending;
  167. /**
  168. * The page with the sprite data.
  169. */
  170. uint16 data_page;
  171. /**
  172. * Page with alternate sprite data.
  173. *
  174. * Used for special effects pages. When data_page2 != 0, it
  175. * must be used instead of {@see data_page}.
  176. */
  177. uint16 data_page2;
  178. /**
  179. * Color depth.
  180. *
  181. * Do not use. Use texture page depth instead.
  182. */
  183. uint16 colour_depth;
  184. /**
  185. * Sprite source.
  186. *
  187. * For PC use only.
  188. * z = unknown, x = srcX / 16 * 625000, y = srcY / 16 * 625000
  189. */
  190. Ogre::Vector3 src_big;
  191. };
  192. typedef std::vector<SpriteData> SpriteList;
  193. typedef std::vector<SpriteData*> SpritePtrList;
  194. /**
  195. * A layer of a background.
  196. */
  197. struct Layer{
  198. /**
  199. * Indicates if the layer is used.
  200. */
  201. bool enabled;
  202. /**
  203. * Layer width.
  204. */
  205. uint16 width;
  206. /**
  207. * Layer height.
  208. */
  209. uint16 height;
  210. /**
  211. * Unknown.
  212. *
  213. * uint16 sprite_count only read in serializer. Unused in layer
  214. * 0.
  215. */
  216. uint16 unknown_06;
  217. /**
  218. * Unknown.
  219. *
  220. * Used in layers 1, 2 and 3
  221. */
  222. uint16 unknown_08[3];
  223. /**
  224. * Unknown.
  225. *
  226. * Used in layers 2 and 3
  227. */
  228. uint16 unknown_0E[4];
  229. /**
  230. * List of sprites in the layer.
  231. */
  232. SpriteList sprites;
  233. };
  234. typedef PaletteFile::Color Color;
  235. typedef std::vector<uint8> Buffer;
  236. typedef std::vector<Color> Colors;
  237. /**
  238. * A background page/
  239. */
  240. struct Page{
  241. /**
  242. * Indicates if the page is used.
  243. */
  244. bool enabled;
  245. /**
  246. * Unknown data.
  247. */
  248. uint16 unknown_02;
  249. /**
  250. * @todo Understand and document.
  251. */
  252. uint16 value_size;
  253. // //uint8 if value_size == 1, uint16 if value_size == 2
  254. // uint8 data[PAGE_DATA_WIDTH][PAGE_DATA_HEIGHT];
  255. /**
  256. * Data buffer.
  257. */
  258. Buffer data;
  259. /**
  260. * Background color list.
  261. */
  262. Colors colors;
  263. };
  264. /**
  265. * Retrieves the background layers.
  266. *
  267. * @return The list of layers.
  268. */
  269. std::array<Layer, LAYER_COUNT>& GetLayers(void) {return layers_;}
  270. /**
  271. * Retrieves the background palette.
  272. *
  273. * @return The palette.
  274. */
  275. std::array<uint8, PALETTE_ENTRY_COUNT>& GetPalette(void) {
  276. return palette_;
  277. }
  278. /**
  279. * Retrieves the background pages.
  280. *
  281. * @return The background pages.
  282. */
  283. std::array<Page, PAGE_COUNT>& GetPages(void){return pages_;}
  284. /**
  285. * Creates an image from a palette.
  286. *
  287. * @param palette[in] The color palette for the image.
  288. * @return The image.
  289. */
  290. Ogre::Image* CreateImage(const PaletteFilePtr &palette);
  291. /**
  292. * Adds all sprites to the background.
  293. *
  294. * @param sprites[in] The list of sprites to add.
  295. */
  296. void AddAllSprites(SpritePtrList& sprites) ;
  297. protected:
  298. /**
  299. * Loads the file.
  300. */
  301. virtual void loadImpl() override final;
  302. /**
  303. * Unloads the file.
  304. */
  305. virtual void unloadImpl() override final;
  306. /**
  307. * Calculates the size of the background.
  308. *
  309. * It includes all layers and pages.
  310. *
  311. * @return The total size of the background, in bytes.
  312. */
  313. virtual size_t CalculateSize() const;
  314. /**
  315. * Calculates the size of a background layer.
  316. *
  317. * @param layer[in] The layer to calculate size from.
  318. * @return The total size of the background layer, in bytes.
  319. */
  320. virtual size_t CalculateSize(const Layer &layer) const;
  321. /**
  322. * Calculates the size of a background page.
  323. *
  324. * @param page[in] The layer to calculate size from.
  325. * @return The total size of the background page, in bytes.
  326. */
  327. virtual size_t CalculateSize(const Page &page) const;
  328. private:
  329. /**
  330. * The list of layers.
  331. */
  332. std::array<Layer, LAYER_COUNT> layers_;
  333. /**
  334. * The color palette.
  335. */
  336. std::array<uint8, PALETTE_ENTRY_COUNT> palette_;
  337. /**
  338. * The background pages.
  339. */
  340. std::array<Page, PAGE_COUNT> pages_;
  341. };
  342. typedef Ogre::SharedPtr<BackgroundFile> BackgroundFilePtr;
  343. }