CDefFile.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * CDefFile.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CDefFile.h"
  12. #include "IImageLoader.h"
  13. #include "../../lib/filesystem/Filesystem.h"
  14. #include "../../lib/Point.h"
  15. #include <SDL_pixels.h>
  16. enum class DefType : uint32_t
  17. {
  18. SPELL = 0x40,
  19. SPRITE = 0x41,
  20. CREATURE = 0x42,
  21. MAP = 0x43,
  22. MAP_HERO = 0x44,
  23. TERRAIN = 0x45,
  24. CURSOR = 0x46,
  25. INTERFACE = 0x47,
  26. SPRITE_FRAME = 0x48,
  27. BATTLE_HERO = 0x49
  28. };
  29. /*************************************************************************
  30. * DefFile, class used for def loading *
  31. *************************************************************************/
  32. static bool colorsSimilar (const SDL_Color & lhs, const SDL_Color & rhs)
  33. {
  34. // it seems that H3 does not requires exact match to replace colors -> (255, 103, 255) gets interpreted as shadow
  35. // exact logic is not clear and requires extensive testing with image editing
  36. // potential reason is that H3 uses 16-bit color format (565 RGB bits), meaning that 3 least significant bits are lost in red and blue component
  37. static const int threshold = 8;
  38. int diffR = static_cast<int>(lhs.r) - rhs.r;
  39. int diffG = static_cast<int>(lhs.g) - rhs.g;
  40. int diffB = static_cast<int>(lhs.b) - rhs.b;
  41. int diffA = static_cast<int>(lhs.a) - rhs.a;
  42. return std::abs(diffR) < threshold && std::abs(diffG) < threshold && std::abs(diffB) < threshold && std::abs(diffA) < threshold;
  43. }
  44. CDefFile::CDefFile(const AnimationPath & Name):
  45. data(nullptr),
  46. palette(nullptr)
  47. {
  48. //First 8 colors in def palette used for transparency
  49. static const SDL_Color sourcePalette[8] = {
  50. {0, 255, 255, SDL_ALPHA_OPAQUE},
  51. {255, 150, 255, SDL_ALPHA_OPAQUE},
  52. {255, 100, 255, SDL_ALPHA_OPAQUE},
  53. {255, 50, 255, SDL_ALPHA_OPAQUE},
  54. {255, 0, 255, SDL_ALPHA_OPAQUE},
  55. {255, 255, 0, SDL_ALPHA_OPAQUE},
  56. {180, 0, 255, SDL_ALPHA_OPAQUE},
  57. {0, 255, 0, SDL_ALPHA_OPAQUE}
  58. };
  59. static const SDL_Color targetPalette[8] = {
  60. {0, 0, 0, 0 }, // transparency ( used in most images )
  61. {0, 0, 0, 64 }, // shadow border ( used in battle, adventure map def's )
  62. {0, 0, 0, 64 }, // shadow border ( used in fog-of-war def's )
  63. {0, 0, 0, 128}, // shadow body ( used in fog-of-war def's )
  64. {0, 0, 0, 128}, // shadow body ( used in battle, adventure map def's )
  65. {0, 0, 0, 0 }, // selection / owner flag ( used in battle, adventure map def's )
  66. {0, 0, 0, 128}, // shadow body below selection ( used in battle def's )
  67. {0, 0, 0, 64 } // shadow border below selection ( used in battle def's )
  68. };
  69. data = CResourceHandler::get()->load(Name)->readAll().first;
  70. palette = std::unique_ptr<SDL_Color[]>(new SDL_Color[256]);
  71. int it = 0;
  72. //ui32 type = read_le_u32(data.get() + it);
  73. it+=4;
  74. //int width = read_le_u32(data + it); it+=4;//not used
  75. //int height = read_le_u32(data + it); it+=4;
  76. it+=8;
  77. ui32 totalBlocks = read_le_u32(data.get() + it);
  78. it+=4;
  79. for (ui32 i= 0; i<256; i++)
  80. {
  81. palette[i].r = data[it++];
  82. palette[i].g = data[it++];
  83. palette[i].b = data[it++];
  84. palette[i].a = SDL_ALPHA_OPAQUE;
  85. }
  86. // these colors seems to be used unconditionally
  87. palette[0] = targetPalette[0];
  88. palette[1] = targetPalette[1];
  89. palette[4] = targetPalette[4];
  90. // rest of special colors are used only if their RGB values are close to H3
  91. for (uint32_t i = 0; i < 8; ++i)
  92. {
  93. if (colorsSimilar(sourcePalette[i], palette[i]))
  94. palette[i] = targetPalette[i];
  95. }
  96. for (ui32 i=0; i<totalBlocks; i++)
  97. {
  98. size_t blockID = read_le_u32(data.get() + it);
  99. it+=4;
  100. size_t totalEntries = read_le_u32(data.get() + it);
  101. it+=12;
  102. //8 unknown bytes - skipping
  103. //13 bytes for name of every frame in this block - not used, skipping
  104. it+= 13 * (int)totalEntries;
  105. for (ui32 j=0; j<totalEntries; j++)
  106. {
  107. size_t currOffset = read_le_u32(data.get() + it);
  108. offset[blockID].push_back(currOffset);
  109. it += 4;
  110. }
  111. }
  112. }
  113. void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
  114. {
  115. std::map<size_t, std::vector <size_t> >::const_iterator it;
  116. it = offset.find(group);
  117. assert (it != offset.end());
  118. const ui8 * FDef = data.get()+it->second[frame];
  119. const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
  120. SSpriteDef sprite;
  121. sprite.format = read_le_u32(&sd.format);
  122. sprite.fullWidth = read_le_u32(&sd.fullWidth);
  123. sprite.fullHeight = read_le_u32(&sd.fullHeight);
  124. sprite.width = read_le_u32(&sd.width);
  125. sprite.height = read_le_u32(&sd.height);
  126. sprite.leftMargin = read_le_u32(&sd.leftMargin);
  127. sprite.topMargin = read_le_u32(&sd.topMargin);
  128. ui32 currentOffset = sizeof(SSpriteDef);
  129. //special case for some "old" format defs (SGTWMTA.DEF and SGTWMTB.DEF)
  130. if(sprite.format == 1 && sprite.width > sprite.fullWidth && sprite.height > sprite.fullHeight)
  131. {
  132. sprite.leftMargin = 0;
  133. sprite.topMargin = 0;
  134. sprite.width = sprite.fullWidth;
  135. sprite.height = sprite.fullHeight;
  136. currentOffset -= 16;
  137. }
  138. const ui32 BaseOffset = currentOffset;
  139. loader.init(Point(sprite.width, sprite.height),
  140. Point(sprite.leftMargin, sprite.topMargin),
  141. Point(sprite.fullWidth, sprite.fullHeight), palette.get());
  142. switch(sprite.format)
  143. {
  144. case 0:
  145. {
  146. //pixel data is not compressed, copy data to surface
  147. for(ui32 i=0; i<sprite.height; i++)
  148. {
  149. loader.load(sprite.width, FDef + currentOffset);
  150. currentOffset += sprite.width;
  151. loader.endLine();
  152. }
  153. break;
  154. }
  155. case 1:
  156. {
  157. //for each line we have offset of pixel data
  158. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset);
  159. currentOffset += sizeof(ui32) * sprite.height;
  160. for(ui32 i=0; i<sprite.height; i++)
  161. {
  162. //get position of the line
  163. currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i);
  164. ui32 TotalRowLength = 0;
  165. while(TotalRowLength<sprite.width)
  166. {
  167. ui8 segmentType = FDef[currentOffset++];
  168. ui32 length = FDef[currentOffset++] + 1;
  169. if(segmentType==0xFF)//Raw data
  170. {
  171. loader.load(length, FDef + currentOffset);
  172. currentOffset+=length;
  173. }
  174. else// RLE
  175. {
  176. loader.load(length, segmentType);
  177. }
  178. TotalRowLength += length;
  179. }
  180. loader.endLine();
  181. }
  182. break;
  183. }
  184. case 2:
  185. {
  186. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset);
  187. for(ui32 i=0; i<sprite.height; i++)
  188. {
  189. ui32 TotalRowLength=0;
  190. while(TotalRowLength<sprite.width)
  191. {
  192. ui8 segment=FDef[currentOffset++];
  193. ui8 code = segment / 32;
  194. ui8 length = (segment & 31) + 1;
  195. if(code==7)//Raw data
  196. {
  197. loader.load(length, FDef + currentOffset);
  198. currentOffset += length;
  199. }
  200. else//RLE
  201. {
  202. loader.load(length, code);
  203. }
  204. TotalRowLength+=length;
  205. }
  206. loader.endLine();
  207. }
  208. break;
  209. }
  210. case 3:
  211. {
  212. for(ui32 i=0; i<sprite.height; i++)
  213. {
  214. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32));
  215. ui32 TotalRowLength=0;
  216. while(TotalRowLength<sprite.width)
  217. {
  218. ui8 segment = FDef[currentOffset++];
  219. ui8 code = segment / 32;
  220. ui8 length = (segment & 31) + 1;
  221. if(code==7)//Raw data
  222. {
  223. loader.load(length, FDef + currentOffset);
  224. currentOffset += length;
  225. }
  226. else//RLE
  227. {
  228. loader.load(length, code);
  229. }
  230. TotalRowLength += length;
  231. }
  232. loader.endLine();
  233. }
  234. break;
  235. }
  236. default:
  237. logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
  238. break;
  239. }
  240. }
  241. CDefFile::~CDefFile() = default;
  242. const std::map<size_t, size_t > CDefFile::getEntries() const
  243. {
  244. std::map<size_t, size_t > ret;
  245. for (auto & elem : offset)
  246. ret[elem.first] = elem.second.size();
  247. return ret;
  248. }