QGearsBackgroundFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-11 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/QGearsBackgroundFile.h"
  23. #include <OgreResourceGroupManager.h>
  24. #include <OgreLogManager.h>
  25. #include "data/QGearsBackgroundFileSerializer.h"
  26. namespace QGears
  27. {
  28. //---------------------------------------------------------------------
  29. const String BackgroundFile::RESOURCE_TYPE( "QGearsBackgroundFile" );
  30. //---------------------------------------------------------------------
  31. BackgroundFile::BackgroundFile( Ogre::ResourceManager *creator
  32. ,const String &name, Ogre::ResourceHandle handle
  33. ,const String &group, bool isManual
  34. ,Ogre::ManualResourceLoader *loader ) :
  35. Ogre::Resource( creator, name, handle, group, isManual, loader )
  36. {
  37. createParamDictionary( RESOURCE_TYPE );
  38. }
  39. //---------------------------------------------------------------------
  40. BackgroundFile::~BackgroundFile()
  41. {
  42. unload();
  43. }
  44. //---------------------------------------------------------------------
  45. void
  46. BackgroundFile::loadImpl()
  47. {
  48. BackgroundFileSerializer serializer;
  49. Ogre::DataStreamPtr stream( Ogre::ResourceGroupManager::getSingleton().openResource( mName, mGroup, true, this ) );
  50. serializer.importBackgroundFile( stream, this );
  51. }
  52. //---------------------------------------------------------------------
  53. void
  54. BackgroundFile::unloadImpl()
  55. {
  56. for (auto& layer : m_layers)
  57. {
  58. memset(&layer, 0, sizeof(layer));
  59. }
  60. for (auto& pal : m_palette)
  61. {
  62. memset(&pal, 0, sizeof(pal));
  63. }
  64. for( size_t i(PAGE_COUNT); i--; )
  65. {
  66. m_pages[i].enabled = false;
  67. m_pages[i].unknown_02 = 0;
  68. m_pages[i].value_size = 0;
  69. m_pages[i].data.clear();
  70. }
  71. }
  72. //---------------------------------------------------------------------
  73. size_t
  74. BackgroundFile::calculateSize() const
  75. {
  76. size_t data_size( 0 );
  77. for( size_t i(LAYER_COUNT); i--; )
  78. {
  79. data_size += calculateSize( m_layers[i] );
  80. }
  81. for( size_t i(PAGE_COUNT); i--; )
  82. {
  83. data_size += calculateSize( m_pages[i] );
  84. }
  85. return data_size;
  86. }
  87. //---------------------------------------------------------------------
  88. size_t
  89. BackgroundFile::calculateSize( const Layer &layer ) const
  90. {
  91. return sizeof( layer.enabled )
  92. +sizeof( layer.width )
  93. +sizeof( layer.height )
  94. +sizeof( layer.unknown_06 )
  95. +sizeof( layer.unknown_08 )
  96. +sizeof( layer.unknown_0E )
  97. +layer.sprites.size() * sizeof( layer.sprites[0] );
  98. }
  99. //---------------------------------------------------------------------
  100. size_t
  101. BackgroundFile::calculateSize( const Page &page ) const
  102. {
  103. return sizeof( page.enabled )
  104. +sizeof( page.unknown_02 )
  105. +sizeof( page.value_size )
  106. +page.data.size();
  107. }
  108. //---------------------------------------------------------------------
  109. void
  110. BackgroundFile::addAllSprites( SpriteList& sprites ) const
  111. {
  112. for( size_t i(0); i < LAYER_COUNT; ++i )
  113. {
  114. if( m_layers[i].enabled )
  115. {
  116. sprites.insert( sprites.end(), m_layers[i].sprites.begin(), m_layers[i].sprites.end() );
  117. }
  118. }
  119. }
  120. //---------------------------------------------------------------------
  121. Ogre::Image*
  122. BackgroundFile::createImage( const PaletteFilePtr &palette ) const
  123. {
  124. assert( !palette.isNull() );
  125. SpriteList sprites;
  126. addAllSprites( sprites );
  127. size_t sprite_count( sprites.size() );
  128. size_t width( 1024 );
  129. size_t row_pitch( width );
  130. size_t height( width );
  131. size_t pixel_count( width * height );
  132. row_pitch = width;
  133. Ogre::PixelFormat format( Ogre::PF_A8R8G8B8 );
  134. size_t memory_size( Ogre::PixelUtil::getMemorySize( width, height, 1, format ) );
  135. Ogre::MemoryDataStream *buffer( new Ogre::MemoryDataStream( memory_size ) );
  136. uint32 *color( reinterpret_cast<uint32*>( buffer->getPtr() ) );
  137. Ogre::LogManager::getSingleton().stream()
  138. << "Image Size: " << width << " x " << height
  139. << " sprite_count " << sprite_count;
  140. size_t dst_x(0), dst_y(0),
  141. dst_x_16(0), dst_y_16(0),
  142. dst_x_32(0), dst_y_32(0);
  143. size_t dst_n_16(0);
  144. for( SpriteList::const_iterator it( sprites.begin() )
  145. ;it != sprites.end()
  146. ;++it)
  147. {
  148. const Page& data_page( m_pages[it->data_page] );
  149. const Pixel& src(it->src);
  150. if (!data_page.enabled)
  151. {
  152. Ogre::LogManager::getSingleton().stream()
  153. << "Error: referencing an disabled data page";
  154. }
  155. // Position sprites in 32x32 blocks
  156. if (it->width == 32)
  157. {
  158. dst_x = dst_x_32;
  159. dst_y = dst_y_32;
  160. dst_x_32 += 32;
  161. if (dst_x_32 == width)
  162. {
  163. dst_y_32 += 32;
  164. dst_x_32 = 0;
  165. }
  166. }
  167. else if (it->width == 16)
  168. {
  169. // if we start new 16x16*4 block
  170. if (dst_n_16 == 0)
  171. {
  172. dst_x_16 = dst_x_32;
  173. dst_y_16 = dst_y_32;
  174. dst_x_32 += 32;
  175. if (dst_x_32 == width)
  176. {
  177. dst_y_32 += 32;
  178. dst_x_32 = 0;
  179. }
  180. }
  181. dst_x = dst_x_16;
  182. dst_y = dst_y_16;
  183. ++dst_n_16;
  184. if (dst_n_16 == 1 || dst_n_16 == 3)
  185. {
  186. dst_x_16 += 16;
  187. }
  188. else if (dst_n_16 == 2)
  189. {
  190. dst_x_16 -= 16;
  191. dst_y_16 += 16;
  192. }
  193. else if (dst_n_16 == 4)
  194. {
  195. dst_n_16 = 0;
  196. }
  197. }
  198. else
  199. {
  200. Ogre::LogManager::getSingleton().stream()
  201. << "Error: sprite data with invalid size";
  202. }
  203. if (data_page.value_size == 2)
  204. {
  205. for (uint16 y(it->height); y--;)
  206. {
  207. for (uint16 x(it->width); x--;)
  208. {
  209. size_t data_index((src.y + y) * PAGE_DATA_WIDTH + src.x + x);
  210. if (data_index >= data_page.colors.size())
  211. {
  212. Ogre::LogManager::getSingleton().stream()
  213. << "Error: data page Index out of Bounds " << data_index;
  214. }
  215. Color colour(data_page.colors.at(data_index));
  216. data_index = (dst_y + y) * row_pitch + dst_x + x;
  217. if (data_index >= pixel_count)
  218. {
  219. Ogre::LogManager::getSingleton().stream()
  220. << "Error: writing Pixel out of Bounds " << data_index
  221. << " " << (dst_x + x) << " x " << dst_y + y;
  222. }
  223. color[data_index] = colour.getAsARGB();
  224. }
  225. }
  226. }
  227. else if (data_page.value_size == 1)
  228. {
  229. if (it->palette_page >= palette->getPages().size())
  230. {
  231. Ogre::LogManager::getSingleton().stream()
  232. << "Error: palette page Index out of Bounds " << it->palette_page;
  233. }
  234. const PaletteFile::Page& palette_page(palette->getPage(it->palette_page));
  235. bool firstColorHidden(false);
  236. if (it->palette_page < PALETTE_ENTRY_COUNT)
  237. {
  238. firstColorHidden = m_palette[it->palette_page] > 0;
  239. }
  240. for (uint16 y(it->height); y--;)
  241. {
  242. for (uint16 x(it->width); x--;)
  243. {
  244. size_t data_index((src.y + y) * PAGE_DATA_WIDTH + src.x + x);
  245. if (data_index >= data_page.data.size())
  246. {
  247. Ogre::LogManager::getSingleton().stream()
  248. << "Error: data page Index out of Bounds " << data_index;
  249. }
  250. uint8 index(data_page.data.at(data_index));
  251. if (index >= palette_page.size())
  252. {
  253. Ogre::LogManager::getSingleton().stream()
  254. << "Error: palette page Index out of Bounds " << index;
  255. }
  256. data_index = (dst_y + y) * row_pitch + dst_x + x;
  257. if (data_index >= pixel_count)
  258. {
  259. Ogre::LogManager::getSingleton().stream()
  260. << "Error: writing Pixel out of Bounds " << data_index
  261. << " " << (dst_x + x) << " x " << dst_y + y;
  262. }
  263. Color colour(palette_page[index]);
  264. // Strange PC version behavior: empty colors are replaced by the first color and the first color is hidden by a flag
  265. if (colour == Color::ZERO)
  266. {
  267. colour = palette_page[0];
  268. }
  269. if (index == 0 && firstColorHidden)
  270. {
  271. colour.a = 0;
  272. }
  273. else
  274. {
  275. colour.a = 1;
  276. }
  277. color[data_index] = colour.getAsARGB();
  278. }
  279. }
  280. }
  281. }
  282. Ogre::DataStreamPtr stream( buffer );
  283. Ogre::Image *image( new Ogre::Image() );
  284. image->loadRawData( stream, width, height, format );
  285. return image;
  286. }
  287. //---------------------------------------------------------------------
  288. }