CDefHandler.cpp 8.8 KB

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