VGearsBackgroundFile.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include <OgreResourceGroupManager.h>
  16. #include <OgreLogManager.h>
  17. #include "data/VGearsBackgroundFile.h"
  18. #include "data/VGearsBackgroundFileSerializer.h"
  19. namespace VGears{
  20. const String BackgroundFile::RESOURCE_TYPE("VGearsBackgroundFile");
  21. BackgroundFile::BackgroundFile(
  22. Ogre::ResourceManager *creator, const String &name,
  23. Ogre::ResourceHandle handle, const String &group, bool is_manual,
  24. Ogre::ManualResourceLoader *loader
  25. ) : Ogre::Resource(creator, name, handle, group, is_manual, loader)
  26. {createParamDictionary(RESOURCE_TYPE);}
  27. BackgroundFile::~BackgroundFile(){unload();}
  28. void BackgroundFile::loadImpl(){
  29. BackgroundFileSerializer serializer;
  30. Ogre::DataStreamPtr stream(
  31. Ogre::ResourceGroupManager::getSingleton().openResource(
  32. mName, mGroup, true, this
  33. )
  34. );
  35. serializer.ImportBackgroundFile(stream, this);
  36. }
  37. void BackgroundFile::unloadImpl(){
  38. for (auto& layer : layers_) memset(&layer, 0, sizeof(layer));
  39. for (auto& pal : palette_) memset(&pal, 0, sizeof(pal));
  40. for (size_t i(PAGE_COUNT); i --;){
  41. pages_[i].enabled = false;
  42. pages_[i].unknown_02 = 0;
  43. pages_[i].value_size = 0;
  44. pages_[i].data.clear();
  45. }
  46. }
  47. size_t BackgroundFile::CalculateSize() const{
  48. size_t data_size(0);
  49. for (size_t i(LAYER_COUNT); i-- ;)
  50. data_size += CalculateSize(layers_[i]);
  51. for (size_t i(PAGE_COUNT); i-- ;) data_size += CalculateSize(pages_[i]);
  52. return data_size;
  53. }
  54. size_t BackgroundFile::CalculateSize(const Layer &layer) const{
  55. return
  56. sizeof(layer.enabled)
  57. + sizeof(layer.width)
  58. + sizeof(layer.height)
  59. + sizeof(layer.unknown_06)
  60. + sizeof(layer.unknown_08)
  61. + sizeof(layer.unknown_0E)
  62. + (layer.sprites.size() * sizeof(layer.sprites[0]));
  63. }
  64. size_t BackgroundFile::CalculateSize(const Page &page) const{
  65. return
  66. sizeof(page.enabled)
  67. + sizeof(page.unknown_02)
  68. + sizeof(page.value_size)
  69. + page.data.size();
  70. }
  71. void BackgroundFile::AddAllSprites(SpritePtrList& sprites){
  72. for (size_t i(0); i < LAYER_COUNT; ++ i){
  73. if (layers_[i].enabled){
  74. for (auto& it : layers_[i].sprites){
  75. SpriteData* ptr = &it;
  76. sprites.push_back(ptr);
  77. }
  78. std::sort(layers_[i].sprites.begin(), layers_[i].sprites.end(), SpriteSorter);
  79. }
  80. }
  81. }
  82. Ogre::Image* BackgroundFile::CreateImage(const PaletteFilePtr &palette){
  83. assert(palette != nullptr);
  84. SpritePtrList sprites;
  85. palette_tiles_.clear();
  86. AddAllSprites(sprites);
  87. size_t width(1024);
  88. size_t row_pitch(width);
  89. size_t height(width);
  90. size_t pixel_count(width * height);
  91. Ogre::PixelFormat format(Ogre::PF_A8R8G8B8);
  92. size_t memory_size(
  93. Ogre::PixelUtil::getMemorySize(width, height, 1, format)
  94. );
  95. Ogre::MemoryDataStream *buffer(new Ogre::MemoryDataStream(memory_size));
  96. uint32 *color(reinterpret_cast<uint32*>(buffer->getPtr()));
  97. // The tiles don't fill the image: what they don't cover must be transparent, not whatever was in memory.
  98. memset(color, 0, memory_size);
  99. int dst_x(0), dst_y(0);
  100. int dst_x_16(0), dst_y_16(0);
  101. int dst_x_32(0), dst_y_32(0);
  102. int dst_n_16(0);
  103. for (auto it(sprites.begin()); it != sprites.end(); ++ it){
  104. SpriteData& sprite = **it;
  105. const Page& data_page(pages_[sprite.data_page]);
  106. const Pixel& src(sprite.src);
  107. if (!data_page.enabled){
  108. Ogre::LogManager::getSingleton().stream()
  109. << "Error: referencing an disabled data page";
  110. }
  111. // Position sprites in 32x32 blocks.
  112. if (sprite.width == 32){
  113. dst_x = dst_x_32;
  114. dst_y = dst_y_32;
  115. dst_x_32 += 32;
  116. if (dst_x_32 == width){
  117. dst_y_32 += 32;
  118. dst_x_32 = 0;
  119. }
  120. }
  121. else if (sprite.width == 16){
  122. // Start new 16x16*4 block
  123. if (dst_n_16 == 0){
  124. dst_x_16 = dst_x_32;
  125. dst_y_16 = dst_y_32;
  126. dst_x_32 += 32;
  127. if (dst_x_32 == width){
  128. dst_y_32 += 32;
  129. dst_x_32 = 0;
  130. }
  131. }
  132. dst_x = dst_x_16;
  133. dst_y = dst_y_16;
  134. ++ dst_n_16;
  135. if (dst_n_16 == 1 || dst_n_16 == 3) dst_x_16 += 16;
  136. else if (dst_n_16 == 2){
  137. dst_x_16 -= 16;
  138. dst_y_16 += 16;
  139. }
  140. else if (dst_n_16 == 4) dst_n_16 = 0;
  141. }
  142. else{
  143. Ogre::LogManager::getSingleton().stream()
  144. << "Error: sprite data with invalid size";
  145. }
  146. if (data_page.value_size == 2){
  147. for (uint16 y((*it)->height); y --;){
  148. for (uint16 x((*it)->width); x--;){
  149. size_t data_index(
  150. (src.y + y) * PAGE_DATA_WIDTH + src.x + x
  151. );
  152. if (data_index >= data_page.colors.size()){
  153. Ogre::LogManager::getSingleton().stream()
  154. << "Error: data page Index out of Bounds "
  155. << data_index;
  156. }
  157. Color colour(data_page.colors.at(data_index));
  158. data_index = (dst_y + y) * row_pitch + dst_x + x;
  159. if (data_index >= pixel_count){
  160. Ogre::LogManager::getSingleton().stream()
  161. << "Error: writing Pixel out of Bounds "
  162. << data_index << " " << (dst_x + x) << " x "
  163. << dst_y + y;
  164. }
  165. color[data_index] = colour.getAsARGB();
  166. }
  167. }
  168. }
  169. else if (data_page.value_size == 1){
  170. if (sprite.palette_page >= palette->GetPages().size()){
  171. // fr_e substrative blending hack.
  172. if (
  173. mName.compare("fr_e.background") == 0
  174. && sprite.has_blending
  175. && palette->GetPages().size() == 2
  176. ){
  177. // Repair palette id.
  178. sprite.palette_page = sprite.data_page > 16;
  179. sprite.blending = 2; // Substrative blending.
  180. }
  181. // lastmap hack
  182. else if (
  183. mName.compare("lastmap.background") == 0
  184. && sprite.has_blending
  185. && palette->GetPages().size() >= 7
  186. ){
  187. sprite.palette_page = 6; // Repair palette id
  188. sprite.blending = 2; // Substrative blending
  189. }
  190. else{
  191. Ogre::LogManager::getSingleton().stream()
  192. << "Error: palette page Index out of Bounds "
  193. << sprite.palette_page;
  194. }
  195. }
  196. const PaletteFile::Page& palette_page(
  197. palette->GetPage(sprite.palette_page)
  198. );
  199. bool first_color_hidden(false);
  200. if (sprite.palette_page < PALETTE_ENTRY_COUNT){
  201. first_color_hidden = palette_[sprite.palette_page] > 0;
  202. }
  203. // Remember the tile, for the palette animations.
  204. PaletteTile palette_tile;
  205. palette_tile.x = dst_x;
  206. palette_tile.y = dst_y;
  207. palette_tile.width = sprite.width;
  208. palette_tile.height = sprite.height;
  209. palette_tile.palette_page = sprite.palette_page;
  210. palette_tile.first_color_hidden = first_color_hidden;
  211. palette_tile.indexes.assign(
  212. static_cast<size_t>(sprite.width) * sprite.height, 0
  213. );
  214. for (uint16 y(sprite.height); y --;){
  215. for (uint16 x(sprite.width); x --;){
  216. size_t data_index(
  217. (src.y + y) * PAGE_DATA_WIDTH + src.x + x
  218. );
  219. if (data_index >= data_page.data.size()){
  220. Ogre::LogManager::getSingleton().stream()
  221. << "Error: data page Index out of Bounds "
  222. << data_index;
  223. }
  224. uint8 index(data_page.data.at(data_index));
  225. palette_tile.indexes[
  226. static_cast<size_t>(y) * sprite.width + x
  227. ] = index;
  228. if (index >= palette_page.size()){
  229. Ogre::LogManager::getSingleton().stream()
  230. << "Error: palette page Index out of Bounds "
  231. << index;
  232. }
  233. data_index = (dst_y + y) * row_pitch + dst_x + x;
  234. if (data_index >= pixel_count){
  235. Ogre::LogManager::getSingleton().stream()
  236. << "Error: writing Pixel out of Bounds "
  237. << data_index << " " << (dst_x + x) << " x "
  238. << dst_y + y;
  239. }
  240. Color colour(palette_page[index]);
  241. // Strange PC version behavior: empty colors are
  242. // replaced by the first color and the first color is
  243. // hidden by a flag.
  244. const bool empty_color = (colour == Color::ZERO);
  245. if (empty_color) colour = palette_page[0];
  246. // A page whose first color is a bright marker (the yellow of mds6_2, the green
  247. // of mds5_1) uses it as the key of the hidden color: the empty entries that
  248. // took it are hidden too, or they would be painted with that color. When
  249. // the first color is a dark one, the empty entries are painted with it,
  250. // as the dithering of the ground of mds6_2 needs.
  251. const float marker_level = 24.0f / 31.0f;
  252. const bool bright_first_color = (
  253. palette_page[0].r >= marker_level || palette_page[0].g >= marker_level
  254. || palette_page[0].b >= marker_level
  255. );
  256. if (
  257. (index == 0 && first_color_hidden)
  258. || (empty_color && first_color_hidden && bright_first_color)
  259. ) colour.a = 0;
  260. else if (
  261. colour.a
  262. && (colour.r != 0 || colour.g != 0 || colour.b != 0)
  263. ){
  264. colour.a = 0.5;
  265. }
  266. else colour.a = 1;
  267. color[data_index] = colour.getAsARGB();
  268. }
  269. }
  270. if (sprite.palette_page < palette->GetPages().size()){
  271. palette_tiles_.push_back(std::move(palette_tile));
  272. }
  273. }
  274. // Source in the texture atlas is where it's just copied to.
  275. sprite.src.x = dst_x;
  276. sprite.src.y = dst_y;
  277. }
  278. Ogre::DataStreamPtr stream(buffer);
  279. Ogre::Image *image(new Ogre::Image());
  280. image->loadRawData(stream, width, height, format);
  281. return image;
  282. }
  283. bool BackgroundFile::SpriteSorter(SpriteData sprite1, SpriteData sprite2){
  284. if (sprite1.dst.y == sprite2.dst.y) return sprite1.dst.x < sprite2.dst.x;
  285. else return sprite1.dst.y < sprite2.dst.y;
  286. }
  287. }