CDefHandler.cpp 8.6 KB

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