2
0

CDefHandler.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include "StdInc.h"
  2. #include "SDL.h"
  3. #include "CDefHandler.h"
  4. #include "../lib/filesystem/Filesystem.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include "CBitmapHandler.h"
  7. /*
  8. * CDefHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. #ifdef unused
  17. static long long pow(long long a, int b)
  18. {
  19. if (!b) return 1;
  20. long c = a;
  21. while (--b)
  22. a*=c;
  23. return a;
  24. }
  25. #endif
  26. CDefHandler::CDefHandler()
  27. {
  28. notFreeImgs = false;
  29. }
  30. CDefHandler::~CDefHandler()
  31. {
  32. if (notFreeImgs)
  33. return;
  34. for (auto & elem : ourImages)
  35. {
  36. if (elem.bitmap)
  37. {
  38. SDL_FreeSurface(elem.bitmap);
  39. elem.bitmap=nullptr;
  40. }
  41. }
  42. }
  43. CDefEssential::~CDefEssential()
  44. {
  45. for(auto & elem : ourImages)
  46. SDL_FreeSurface(elem.bitmap);
  47. }
  48. void CDefHandler::openFromMemory(ui8 *table, const std::string & name)
  49. {
  50. SDL_Color palette[256];
  51. SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
  52. ui8 *p;
  53. defName = name;
  54. DEFType = read_le_u32(&de.DEFType);
  55. width = read_le_u32(&de.width);
  56. height = read_le_u32(&de.height);
  57. ui32 totalBlocks = read_le_u32(&de.totalBlocks);
  58. for (ui32 it=0;it<256;it++)
  59. {
  60. palette[it].r = de.palette[it].R;
  61. palette[it].g = de.palette[it].G;
  62. palette[it].b = de.palette[it].B;
  63. #if 0
  64. palette[it].unused = 255;
  65. #else
  66. palette[it].a = 255;
  67. #endif // 0
  68. }
  69. // The SDefEntryBlock starts just after the SDefEntry
  70. p = reinterpret_cast<ui8 *>(&de);
  71. p += sizeof(de);
  72. int totalEntries=0;
  73. for (ui32 z=0; z<totalBlocks; z++)
  74. {
  75. SDefEntryBlock &block = * reinterpret_cast<SDefEntryBlock *>(p);
  76. ui32 totalInBlock;
  77. totalInBlock = read_le_u32(&block.totalInBlock);
  78. for (ui32 j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  79. SEntries.push_back(SEntry());
  80. p = block.data;
  81. for (ui32 j=0; j<totalInBlock; j++)
  82. {
  83. char Buffer[13];
  84. memcpy(Buffer, p, 12);
  85. Buffer[12] = 0;
  86. SEntries[totalEntries+j].name=Buffer;
  87. p += 13;
  88. }
  89. for (ui32 j=0; j<totalInBlock; j++)
  90. {
  91. SEntries[totalEntries+j].offset = read_le_u32(p);
  92. p += 4;
  93. }
  94. //totalEntries+=totalInBlock;
  95. for(ui32 hh=0; hh<totalInBlock; ++hh)
  96. {
  97. SEntries[totalEntries].group = z;
  98. ++totalEntries;
  99. }
  100. }
  101. for(auto & elem : SEntries)
  102. {
  103. elem.name = elem.name.substr(0, elem.name.find('.')+4);
  104. }
  105. //RWEntries = new ui32[height];
  106. for(ui32 i=0; i < SEntries.size(); ++i)
  107. {
  108. Cimage nimg;
  109. nimg.bitmap = getSprite(i, table, palette);
  110. nimg.imName = SEntries[i].name;
  111. nimg.groupNumber = SEntries[i].group;
  112. ourImages.push_back(nimg);
  113. }
  114. }
  115. void CDefHandler::expand(ui8 N,ui8 & BL, ui8 & BR)
  116. {
  117. BL = (N & 0xE0) >> 5;
  118. BR = N & 0x1F;
  119. }
  120. SDL_Surface * CDefHandler::getSprite (int SIndex, const ui8 * FDef, const SDL_Color * palette) const
  121. {
  122. SDL_Surface * ret=nullptr;
  123. ui32 BaseOffset,
  124. SpriteWidth, SpriteHeight, //format of sprite
  125. TotalRowLength, // length of read segment
  126. add, FullHeight,FullWidth,
  127. RowAdd,
  128. defType2;
  129. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  130. ui8 SegmentType;
  131. BaseOffset = SEntries[SIndex].offset;
  132. SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);
  133. defType2 = read_le_u32(&sd.defType2);
  134. FullWidth = read_le_u32(&sd.FullWidth);
  135. FullHeight = read_le_u32(&sd.FullHeight);
  136. SpriteWidth = read_le_u32(&sd.SpriteWidth);
  137. SpriteHeight = read_le_u32(&sd.SpriteHeight);
  138. LeftMargin = read_le_u32(&sd.LeftMargin);
  139. TopMargin = read_le_u32(&sd.TopMargin);
  140. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  141. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  142. //if(LeftMargin + RightMargin < 0)
  143. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  144. if(LeftMargin<0)
  145. SpriteWidth+=LeftMargin;
  146. if(RightMargin<0)
  147. SpriteWidth+=RightMargin;
  148. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  149. add = 4 - FullWidth%4;
  150. if (add==4)
  151. add=0;
  152. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  153. BaseOffset += sizeof(SSpriteDef);
  154. int BaseOffsetor = BaseOffset;
  155. for(int i=0; i<256; ++i)
  156. {
  157. SDL_Color pr;
  158. pr.r = palette[i].r;
  159. pr.g = palette[i].g;
  160. pr.b = palette[i].b;
  161. #if 0
  162. pr.unused = palette[i].unused;
  163. #else
  164. pr.a = palette[i].a;
  165. #endif // 0
  166. (*(ret->format->palette->colors+i))=pr;
  167. }
  168. int ftcp=0;
  169. // If there's a margin anywhere, just blank out the whole surface.
  170. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  171. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  172. }
  173. // Skip top margin
  174. if (TopMargin > 0)
  175. ftcp += TopMargin*(FullWidth+add);
  176. switch(defType2)
  177. {
  178. case 0:
  179. {
  180. for (ui32 i=0;i<SpriteHeight;i++)
  181. {
  182. if (LeftMargin>0)
  183. ftcp += LeftMargin;
  184. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  185. ftcp += SpriteWidth;
  186. BaseOffset += SpriteWidth;
  187. if (RightMargin>0)
  188. ftcp += RightMargin;
  189. }
  190. }
  191. break;
  192. case 1:
  193. {
  194. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+BaseOffset);
  195. BaseOffset += sizeof(int) * SpriteHeight;
  196. for (ui32 i=0;i<SpriteHeight;i++)
  197. {
  198. BaseOffset=BaseOffsetor + read_le_u32(RWEntriesLoc + i);
  199. if (LeftMargin>0)
  200. ftcp += LeftMargin;
  201. TotalRowLength=0;
  202. do
  203. {
  204. ui32 SegmentLength;
  205. SegmentType=FDef[BaseOffset++];
  206. SegmentLength=FDef[BaseOffset++] + 1;
  207. if (SegmentType==0xFF)
  208. {
  209. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, FDef + BaseOffset, SegmentLength);
  210. BaseOffset+=SegmentLength;
  211. }
  212. else
  213. {
  214. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
  215. }
  216. ftcp += SegmentLength;
  217. TotalRowLength += SegmentLength;
  218. }while(TotalRowLength<SpriteWidth);
  219. RowAdd=SpriteWidth-TotalRowLength;
  220. if (RightMargin>0)
  221. ftcp += RightMargin;
  222. if (add>0)
  223. ftcp += add+RowAdd;
  224. }
  225. }
  226. break;
  227. case 2:
  228. {
  229. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor);
  230. for (ui32 i=0;i<SpriteHeight;i++)
  231. {
  232. //BaseOffset = BaseOffsetor+RWEntries[i];
  233. if (LeftMargin>0)
  234. ftcp += LeftMargin;
  235. TotalRowLength=0;
  236. do
  237. {
  238. SegmentType=FDef[BaseOffset++];
  239. ui8 code = SegmentType / 32;
  240. ui8 value = (SegmentType & 31) + 1;
  241. if(code==7)
  242. {
  243. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  244. ftcp += value;
  245. BaseOffset += value;
  246. }
  247. else
  248. {
  249. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
  250. ftcp += value;
  251. }
  252. TotalRowLength+=value;
  253. } while(TotalRowLength<SpriteWidth);
  254. if (RightMargin>0)
  255. ftcp += RightMargin;
  256. RowAdd=SpriteWidth-TotalRowLength;
  257. if (add>0)
  258. ftcp += add+RowAdd;
  259. }
  260. }
  261. break;
  262. case 3:
  263. {
  264. for (ui32 i=0;i<SpriteHeight;i++)
  265. {
  266. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32));
  267. if (LeftMargin>0)
  268. ftcp += LeftMargin;
  269. TotalRowLength=0;
  270. do
  271. {
  272. SegmentType=FDef[BaseOffset++];
  273. ui8 code = SegmentType / 32;
  274. ui8 value = (SegmentType & 31) + 1;
  275. int len = std::min<ui32>(value, SpriteWidth - TotalRowLength) - std::max(0, -LeftMargin);
  276. vstd::amax(len, 0);
  277. if(code==7)
  278. {
  279. memcpy((ui8*)ret->pixels + ftcp, FDef + BaseOffset, len);
  280. ftcp += len;
  281. BaseOffset += len;
  282. }
  283. else
  284. {
  285. memset((ui8*)ret->pixels + ftcp, code, len);
  286. ftcp += len;
  287. }
  288. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  289. }while(TotalRowLength<SpriteWidth);
  290. if (RightMargin>0)
  291. ftcp += RightMargin;
  292. RowAdd=SpriteWidth-TotalRowLength;
  293. if (add>0)
  294. ftcp += add+RowAdd;
  295. }
  296. }
  297. break;
  298. default:
  299. throw std::runtime_error("Unknown sprite format.");
  300. break;
  301. }
  302. SDL_Color ttcol = ret->format->palette->colors[0];
  303. #if 0
  304. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  305. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  306. #else
  307. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.a);
  308. SDL_SetColorKey(ret, SDL_TRUE, keycol);
  309. #endif // 0
  310. return ret;
  311. }
  312. CDefEssential * CDefHandler::essentialize()
  313. {
  314. auto ret = new CDefEssential;
  315. ret->ourImages = ourImages;
  316. notFreeImgs = true;
  317. return ret;
  318. }
  319. CDefHandler * CDefHandler::giveDef(const std::string & defName)
  320. {
  321. ResourceID resID(std::string("SPRITES/") + defName, EResType::ANIMATION);
  322. auto data = CResourceHandler::get()->load(resID)->readAll().first;
  323. if(!data)
  324. throw std::runtime_error("bad def name!");
  325. auto nh = new CDefHandler();
  326. nh->openFromMemory(data.get(), defName);
  327. return nh;
  328. }
  329. CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
  330. {
  331. CDefEssential * ret;
  332. CDefHandler * temp = giveDef(defName);
  333. ret = temp->essentialize();
  334. delete temp;
  335. return ret;
  336. }