CDefFile.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // Extremely simple file cache. TODO: smarter, more general solution
  17. class CFileCache
  18. {
  19. static const int cacheSize = 50; //Max number of cached files
  20. struct FileData
  21. {
  22. ResourceID name;
  23. size_t size;
  24. std::unique_ptr<ui8[]> data;
  25. std::unique_ptr<ui8[]> getCopy()
  26. {
  27. auto ret = std::unique_ptr<ui8[]>(new ui8[size]);
  28. std::copy(data.get(), data.get() + size, ret.get());
  29. return ret;
  30. }
  31. FileData(ResourceID name_, size_t size_, std::unique_ptr<ui8[]> data_):
  32. name{std::move(name_)},
  33. size{size_},
  34. data{std::move(data_)}
  35. {}
  36. };
  37. std::deque<FileData> cache;
  38. public:
  39. std::unique_ptr<ui8[]> getCachedFile(ResourceID rid)
  40. {
  41. for(auto & file : cache)
  42. {
  43. if (file.name == rid)
  44. return file.getCopy();
  45. }
  46. // Still here? Cache miss
  47. if (cache.size() > cacheSize)
  48. cache.pop_front();
  49. auto data = CResourceHandler::get()->load(rid)->readAll();
  50. cache.emplace_back(std::move(rid), data.second, std::move(data.first));
  51. return cache.back().getCopy();
  52. }
  53. };
  54. enum class DefType : uint32_t
  55. {
  56. SPELL = 0x40,
  57. SPRITE = 0x41,
  58. CREATURE = 0x42,
  59. MAP = 0x43,
  60. MAP_HERO = 0x44,
  61. TERRAIN = 0x45,
  62. CURSOR = 0x46,
  63. INTERFACE = 0x47,
  64. SPRITE_FRAME = 0x48,
  65. BATTLE_HERO = 0x49
  66. };
  67. static CFileCache animationCache;
  68. /*************************************************************************
  69. * DefFile, class used for def loading *
  70. *************************************************************************/
  71. static bool colorsSimilar (const SDL_Color & lhs, const SDL_Color & rhs)
  72. {
  73. // it seems that H3 does not requires exact match to replace colors -> (255, 103, 255) gets interpreted as shadow
  74. // exact logic is not clear and requires extensive testing with image editing
  75. // 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
  76. static const int threshold = 8;
  77. int diffR = static_cast<int>(lhs.r) - rhs.r;
  78. int diffG = static_cast<int>(lhs.g) - rhs.g;
  79. int diffB = static_cast<int>(lhs.b) - rhs.b;
  80. int diffA = static_cast<int>(lhs.a) - rhs.a;
  81. return std::abs(diffR) < threshold && std::abs(diffG) < threshold && std::abs(diffB) < threshold && std::abs(diffA) < threshold;
  82. }
  83. CDefFile::CDefFile(std::string Name):
  84. data(nullptr),
  85. palette(nullptr)
  86. {
  87. //First 8 colors in def palette used for transparency
  88. static const SDL_Color sourcePalette[8] = {
  89. {0, 255, 255, SDL_ALPHA_OPAQUE},
  90. {255, 150, 255, SDL_ALPHA_OPAQUE},
  91. {255, 100, 255, SDL_ALPHA_OPAQUE},
  92. {255, 50, 255, SDL_ALPHA_OPAQUE},
  93. {255, 0, 255, SDL_ALPHA_OPAQUE},
  94. {255, 255, 0, SDL_ALPHA_OPAQUE},
  95. {180, 0, 255, SDL_ALPHA_OPAQUE},
  96. {0, 255, 0, SDL_ALPHA_OPAQUE}
  97. };
  98. static const SDL_Color targetPalette[8] = {
  99. {0, 0, 0, 0 }, // transparency ( used in most images )
  100. {0, 0, 0, 64 }, // shadow border ( used in battle, adventure map def's )
  101. {0, 0, 0, 64 }, // shadow border ( used in fog-of-war def's )
  102. {0, 0, 0, 128}, // shadow body ( used in fog-of-war def's )
  103. {0, 0, 0, 128}, // shadow body ( used in battle, adventure map def's )
  104. {0, 0, 0, 0 }, // selection / owner flag ( used in battle, adventure map def's )
  105. {0, 0, 0, 128}, // shadow body below selection ( used in battle def's )
  106. {0, 0, 0, 64 } // shadow border below selection ( used in battle def's )
  107. };
  108. data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
  109. palette = std::unique_ptr<SDL_Color[]>(new SDL_Color[256]);
  110. int it = 0;
  111. //ui32 type = read_le_u32(data.get() + it);
  112. it+=4;
  113. //int width = read_le_u32(data + it); it+=4;//not used
  114. //int height = read_le_u32(data + it); it+=4;
  115. it+=8;
  116. ui32 totalBlocks = read_le_u32(data.get() + it);
  117. it+=4;
  118. for (ui32 i= 0; i<256; i++)
  119. {
  120. palette[i].r = data[it++];
  121. palette[i].g = data[it++];
  122. palette[i].b = data[it++];
  123. palette[i].a = SDL_ALPHA_OPAQUE;
  124. }
  125. // these colors seems to be used unconditionally
  126. palette[0] = targetPalette[0];
  127. palette[1] = targetPalette[1];
  128. palette[4] = targetPalette[4];
  129. // rest of special colors are used only if their RGB values are close to H3
  130. for (uint32_t i = 0; i < 8; ++i)
  131. {
  132. if (colorsSimilar(sourcePalette[i], palette[i]))
  133. palette[i] = targetPalette[i];
  134. }
  135. for (ui32 i=0; i<totalBlocks; i++)
  136. {
  137. size_t blockID = read_le_u32(data.get() + it);
  138. it+=4;
  139. size_t totalEntries = read_le_u32(data.get() + it);
  140. it+=12;
  141. //8 unknown bytes - skipping
  142. //13 bytes for name of every frame in this block - not used, skipping
  143. it+= 13 * (int)totalEntries;
  144. for (ui32 j=0; j<totalEntries; j++)
  145. {
  146. size_t currOffset = read_le_u32(data.get() + it);
  147. offset[blockID].push_back(currOffset);
  148. it += 4;
  149. }
  150. }
  151. }
  152. void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
  153. {
  154. std::map<size_t, std::vector <size_t> >::const_iterator it;
  155. it = offset.find(group);
  156. assert (it != offset.end());
  157. const ui8 * FDef = data.get()+it->second[frame];
  158. const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
  159. SSpriteDef sprite;
  160. sprite.format = read_le_u32(&sd.format);
  161. sprite.fullWidth = read_le_u32(&sd.fullWidth);
  162. sprite.fullHeight = read_le_u32(&sd.fullHeight);
  163. sprite.width = read_le_u32(&sd.width);
  164. sprite.height = read_le_u32(&sd.height);
  165. sprite.leftMargin = read_le_u32(&sd.leftMargin);
  166. sprite.topMargin = read_le_u32(&sd.topMargin);
  167. ui32 currentOffset = sizeof(SSpriteDef);
  168. //special case for some "old" format defs (SGTWMTA.DEF and SGTWMTB.DEF)
  169. if(sprite.format == 1 && sprite.width > sprite.fullWidth && sprite.height > sprite.fullHeight)
  170. {
  171. sprite.leftMargin = 0;
  172. sprite.topMargin = 0;
  173. sprite.width = sprite.fullWidth;
  174. sprite.height = sprite.fullHeight;
  175. currentOffset -= 16;
  176. }
  177. const ui32 BaseOffset = currentOffset;
  178. loader.init(Point(sprite.width, sprite.height),
  179. Point(sprite.leftMargin, sprite.topMargin),
  180. Point(sprite.fullWidth, sprite.fullHeight), palette.get());
  181. switch(sprite.format)
  182. {
  183. case 0:
  184. {
  185. //pixel data is not compressed, copy data to surface
  186. for(ui32 i=0; i<sprite.height; i++)
  187. {
  188. loader.load(sprite.width, FDef + currentOffset);
  189. currentOffset += sprite.width;
  190. loader.endLine();
  191. }
  192. break;
  193. }
  194. case 1:
  195. {
  196. //for each line we have offset of pixel data
  197. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset);
  198. currentOffset += sizeof(ui32) * sprite.height;
  199. for(ui32 i=0; i<sprite.height; i++)
  200. {
  201. //get position of the line
  202. currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i);
  203. ui32 TotalRowLength = 0;
  204. while(TotalRowLength<sprite.width)
  205. {
  206. ui8 segmentType = FDef[currentOffset++];
  207. ui32 length = FDef[currentOffset++] + 1;
  208. if(segmentType==0xFF)//Raw data
  209. {
  210. loader.load(length, FDef + currentOffset);
  211. currentOffset+=length;
  212. }
  213. else// RLE
  214. {
  215. loader.load(length, segmentType);
  216. }
  217. TotalRowLength += length;
  218. }
  219. loader.endLine();
  220. }
  221. break;
  222. }
  223. case 2:
  224. {
  225. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset);
  226. for(ui32 i=0; i<sprite.height; i++)
  227. {
  228. ui32 TotalRowLength=0;
  229. while(TotalRowLength<sprite.width)
  230. {
  231. ui8 segment=FDef[currentOffset++];
  232. ui8 code = segment / 32;
  233. ui8 length = (segment & 31) + 1;
  234. if(code==7)//Raw data
  235. {
  236. loader.load(length, FDef + currentOffset);
  237. currentOffset += length;
  238. }
  239. else//RLE
  240. {
  241. loader.load(length, code);
  242. }
  243. TotalRowLength+=length;
  244. }
  245. loader.endLine();
  246. }
  247. break;
  248. }
  249. case 3:
  250. {
  251. for(ui32 i=0; i<sprite.height; i++)
  252. {
  253. currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32));
  254. ui32 TotalRowLength=0;
  255. while(TotalRowLength<sprite.width)
  256. {
  257. ui8 segment = FDef[currentOffset++];
  258. ui8 code = segment / 32;
  259. ui8 length = (segment & 31) + 1;
  260. if(code==7)//Raw data
  261. {
  262. loader.load(length, FDef + currentOffset);
  263. currentOffset += length;
  264. }
  265. else//RLE
  266. {
  267. loader.load(length, code);
  268. }
  269. TotalRowLength += length;
  270. }
  271. loader.endLine();
  272. }
  273. break;
  274. }
  275. default:
  276. logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
  277. break;
  278. }
  279. }
  280. CDefFile::~CDefFile() = default;
  281. const std::map<size_t, size_t > CDefFile::getEntries() const
  282. {
  283. std::map<size_t, size_t > ret;
  284. for (auto & elem : offset)
  285. ret[elem.first] = elem.second.size();
  286. return ret;
  287. }