TxzFileSerializer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 warrantexture_y 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 "common/TypeDefine.h"
  18. #include "data/QGearsSerializer.h"
  19. namespace QGears {
  20. class TxzFile;
  21. class TxzFileSerializer : public Serializer{
  22. public:
  23. #ifdef _MSC_VER
  24. #pragma pack(push)
  25. #pragma pack(1)
  26. #endif
  27. struct WorldMapTexture{
  28. unsigned int clut_x:6;
  29. unsigned int clut_y:10;
  30. unsigned int texture_x:4;
  31. unsigned int texture_y:1;
  32. unsigned int abr:2;
  33. unsigned int tp:2;
  34. unsigned int reserved:7;
  35. #ifdef _MSC_VER
  36. };
  37. #pragma pack(pop)
  38. #else
  39. } __attribute__((packed)); // Assume GCC
  40. #endif
  41. #ifdef _MSC_VER
  42. #pragma pack(push)
  43. #pragma pack(1)
  44. #endif
  45. /**
  46. * A play station pixel representation.
  47. */
  48. struct PsxPixel{
  49. /**
  50. * Red component.
  51. */
  52. uint16 r:5;
  53. /**
  54. * Green component.
  55. */
  56. uint16 g:5;
  57. /**
  58. * Blue component.
  59. */
  60. uint16 b:5;
  61. /**
  62. * Alpha component.
  63. */
  64. uint16 a:1;
  65. #ifdef _MSC_VER
  66. };
  67. #pragma pack(pop)
  68. #else
  69. } __attribute__((packed)); // Assume GCC
  70. #endif
  71. #ifdef _MSC_VER
  72. #pragma pack(push)
  73. #pragma pack(1)
  74. #endif
  75. /**
  76. * Play station pixel representation (4 bits).
  77. */
  78. struct PsxPixel4Bit{
  79. /**
  80. * First pixel index.
  81. */
  82. uint8 index1:4;
  83. /**
  84. * Second pixel index.
  85. */
  86. uint8 index2:4;
  87. #ifdef _MSC_VER
  88. };
  89. #pragma pack(pop)
  90. #else
  91. } __attribute__((packed)); // Assume GCC
  92. #endif
  93. #ifdef _MSC_VER
  94. #pragma pack(push)
  95. #pragma pack(1)
  96. #endif
  97. /**
  98. * RGBA colour representation.
  99. */
  100. struct Rgba{
  101. /**
  102. * Red component.
  103. */
  104. unsigned char r = 0;
  105. /**
  106. * Green component.
  107. */
  108. unsigned char g = 0;
  109. /**
  110. * Blue component.
  111. */
  112. unsigned char b = 0;
  113. /**
  114. * Alpha component.
  115. */
  116. unsigned char a = 0;
  117. #ifdef _MSC_VER
  118. };
  119. #pragma pack(pop)
  120. #else
  121. } __attribute__((packed)); // Assume GCC
  122. #endif
  123. /**
  124. * A video RAM block representation
  125. */
  126. struct VRamBlock{
  127. /**
  128. * Block size, in bytes.
  129. */
  130. uint32 block_size = 0;
  131. /**
  132. * X component of the top left cornet of the block, in pixels.
  133. */
  134. uint16 x = 0;
  135. /**
  136. * Y component of the top left cornet of the block, in pixels.
  137. */
  138. uint16 y = 0;
  139. /**
  140. * Width of the block, in pixels.
  141. */
  142. uint16 width = 0;
  143. /**
  144. * Heigh of the block, in pixels.
  145. */
  146. uint16 height = 0;
  147. /**
  148. * The list of pixels.
  149. *
  150. * It's size shoud be width * height * 2
  151. */
  152. std::vector<uint8> pixels;
  153. };
  154. /**
  155. * Constructor.
  156. */
  157. TxzFileSerializer();
  158. /**
  159. * Destructor.
  160. */
  161. virtual ~TxzFileSerializer();
  162. /**
  163. * Imports a TXZ file.
  164. */
  165. void Import(Ogre::DataStreamPtr& stream, TxzFile& dest);
  166. /**
  167. * Retrieves a texture color pixels from a texture ID.
  168. *
  169. * @param id[in] ID of the texture.
  170. * @return List of color pixels conforming the texture, or an empty
  171. * vector if the texture doesn't exist.
  172. */
  173. std::vector<std::vector<Rgba>> GetWorldMapTexture(uint32 id);
  174. private:
  175. /**
  176. * The list of video RAM blocks.
  177. */
  178. std::vector<VRamBlock> v_ram_blocks_;
  179. /**
  180. * Imports a texture from decompressed file contents.
  181. *
  182. * @param stream[in] Contents of the decompressed file.
  183. * @dest[out] The texture data will be loaded here.
  184. */
  185. void ImportDecompressed(Ogre::DataStream& stream, TxzFile& dest);
  186. /**
  187. * Partially imports a texture from decompressed file contents.
  188. *
  189. * @param stream[in] Contents of the decompressed file.
  190. * @dest[out] The texture data will be loaded here.
  191. * @param section_end[in] Bytes to import.
  192. * @todo Sure it is decompressed?
  193. */
  194. void ImportTextureSection(
  195. Ogre::DataStream& stream, TxzFile& dest, size_t section_end
  196. );
  197. /**
  198. * @todo Understand and document. Where is this implemented?
  199. *
  200. * @param pallete_table[in] @todo.
  201. *
  202. */
  203. void ExtractTexture(
  204. const std::array<WorldMapTexture,512>& pallete_table
  205. );
  206. /**
  207. * The list of textures.
  208. */
  209. std::map<uint32, std::vector<std::vector<Rgba>>> textures_;
  210. };
  211. static_assert(
  212. sizeof(TxzFileSerializer::WorldMapTexture) == 4,
  213. "WorldMapTexture is not packed"
  214. );
  215. static_assert(
  216. sizeof(TxzFileSerializer::PsxPixel) == 2, "PsxPixel is not packed"
  217. );
  218. }