CDefHandler.cpp 8.3 KB

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