CDefHandler.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. if(nullptr == ret)
  154. {
  155. logGlobal->errorStream() << __FUNCTION__ <<": Unable to create surface";
  156. logGlobal->errorStream() << FullWidth << "X" << FullHeight;
  157. logGlobal->errorStream() << SDL_GetError();
  158. throw std::runtime_error("Unable to create surface");
  159. }
  160. BaseOffset += sizeof(SSpriteDef);
  161. int BaseOffsetor = BaseOffset;
  162. #if 0
  163. for(int i=0; i<256; ++i)
  164. {
  165. SDL_Color pr;
  166. pr.r = palette[i].r;
  167. pr.g = palette[i].g;
  168. pr.b = palette[i].b;
  169. pr.unused = palette[i].unused;
  170. (*(ret->format->palette->colors+i))=pr;
  171. }
  172. #else
  173. if(SDL_SetPaletteColors(ret->format->palette,palette,0,256) != 0)
  174. {
  175. throw std::runtime_error("Unable to set palette");
  176. }
  177. #endif
  178. int ftcp=0;
  179. // If there's a margin anywhere, just blank out the whole surface.
  180. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  181. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  182. }
  183. // Skip top margin
  184. if (TopMargin > 0)
  185. ftcp += TopMargin*(FullWidth+add);
  186. switch(defType2)
  187. {
  188. case 0:
  189. {
  190. for (ui32 i=0;i<SpriteHeight;i++)
  191. {
  192. if (LeftMargin>0)
  193. ftcp += LeftMargin;
  194. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  195. ftcp += SpriteWidth;
  196. BaseOffset += SpriteWidth;
  197. if (RightMargin>0)
  198. ftcp += RightMargin;
  199. }
  200. }
  201. break;
  202. case 1:
  203. {
  204. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+BaseOffset);
  205. BaseOffset += sizeof(int) * SpriteHeight;
  206. for (ui32 i=0;i<SpriteHeight;i++)
  207. {
  208. BaseOffset=BaseOffsetor + read_le_u32(RWEntriesLoc + i);
  209. if (LeftMargin>0)
  210. ftcp += LeftMargin;
  211. TotalRowLength=0;
  212. do
  213. {
  214. ui32 SegmentLength;
  215. SegmentType=FDef[BaseOffset++];
  216. SegmentLength=FDef[BaseOffset++] + 1;
  217. if (SegmentType==0xFF)
  218. {
  219. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, FDef + BaseOffset, SegmentLength);
  220. BaseOffset+=SegmentLength;
  221. }
  222. else
  223. {
  224. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
  225. }
  226. ftcp += SegmentLength;
  227. TotalRowLength += SegmentLength;
  228. }while(TotalRowLength<SpriteWidth);
  229. RowAdd=SpriteWidth-TotalRowLength;
  230. if (RightMargin>0)
  231. ftcp += RightMargin;
  232. if (add>0)
  233. ftcp += add+RowAdd;
  234. }
  235. }
  236. break;
  237. case 2:
  238. {
  239. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor);
  240. for (ui32 i=0;i<SpriteHeight;i++)
  241. {
  242. //BaseOffset = BaseOffsetor+RWEntries[i];
  243. if (LeftMargin>0)
  244. ftcp += LeftMargin;
  245. TotalRowLength=0;
  246. do
  247. {
  248. SegmentType=FDef[BaseOffset++];
  249. ui8 code = SegmentType / 32;
  250. ui8 value = (SegmentType & 31) + 1;
  251. if(code==7)
  252. {
  253. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  254. ftcp += value;
  255. BaseOffset += value;
  256. }
  257. else
  258. {
  259. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
  260. ftcp += value;
  261. }
  262. TotalRowLength+=value;
  263. } while(TotalRowLength<SpriteWidth);
  264. if (RightMargin>0)
  265. ftcp += RightMargin;
  266. RowAdd=SpriteWidth-TotalRowLength;
  267. if (add>0)
  268. ftcp += add+RowAdd;
  269. }
  270. }
  271. break;
  272. case 3:
  273. {
  274. for (ui32 i=0;i<SpriteHeight;i++)
  275. {
  276. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32));
  277. if (LeftMargin>0)
  278. ftcp += LeftMargin;
  279. TotalRowLength=0;
  280. do
  281. {
  282. SegmentType=FDef[BaseOffset++];
  283. ui8 code = SegmentType / 32;
  284. ui8 value = (SegmentType & 31) + 1;
  285. int len = std::min<ui32>(value, SpriteWidth - TotalRowLength) - std::max(0, -LeftMargin);
  286. vstd::amax(len, 0);
  287. if(code==7)
  288. {
  289. memcpy((ui8*)ret->pixels + ftcp, FDef + BaseOffset, len);
  290. ftcp += len;
  291. BaseOffset += len;
  292. }
  293. else
  294. {
  295. memset((ui8*)ret->pixels + ftcp, code, len);
  296. ftcp += len;
  297. }
  298. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  299. }while(TotalRowLength<SpriteWidth);
  300. if (RightMargin>0)
  301. ftcp += RightMargin;
  302. RowAdd=SpriteWidth-TotalRowLength;
  303. if (add>0)
  304. ftcp += add+RowAdd;
  305. }
  306. }
  307. break;
  308. default:
  309. throw std::runtime_error("Unknown sprite format.");
  310. break;
  311. }
  312. SDL_Color ttcol = ret->format->palette->colors[0];
  313. #if 0
  314. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  315. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  316. #else
  317. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.a);
  318. SDL_SetColorKey(ret, SDL_TRUE, keycol);
  319. #endif // 0
  320. return ret;
  321. }
  322. CDefEssential * CDefHandler::essentialize()
  323. {
  324. auto ret = new CDefEssential;
  325. ret->ourImages = ourImages;
  326. notFreeImgs = true;
  327. return ret;
  328. }
  329. CDefHandler * CDefHandler::giveDef(const std::string & defName)
  330. {
  331. ResourceID resID(std::string("SPRITES/") + defName, EResType::ANIMATION);
  332. auto data = CResourceHandler::get()->load(resID)->readAll().first;
  333. if(!data)
  334. throw std::runtime_error("bad def name!");
  335. auto nh = new CDefHandler();
  336. nh->openFromMemory(data.get(), defName);
  337. return nh;
  338. }
  339. CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
  340. {
  341. CDefEssential * ret;
  342. CDefHandler * temp = giveDef(defName);
  343. ret = temp->essentialize();
  344. delete temp;
  345. return ret;
  346. }