CDefHandler.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "StdInc.h"
  2. #include "SDL.h"
  3. #include "CDefHandler.h"
  4. #include "../lib/CLodHandler.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include "../lib/vcmi_endian.h"
  7. #include "CBitmapHandler.h"
  8. /*
  9. * CDefHandler.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  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 (size_t i=0; i<ourImages.size(); ++i)
  36. {
  37. if (ourImages[i].bitmap)
  38. {
  39. SDL_FreeSurface(ourImages[i].bitmap);
  40. ourImages[i].bitmap=NULL;
  41. }
  42. }
  43. }
  44. CDefEssential::~CDefEssential()
  45. {
  46. for(size_t i=0; i < ourImages.size(); ++i)
  47. SDL_FreeSurface(ourImages[i].bitmap);
  48. }
  49. void CDefHandler::openFromMemory(ui8 *table, const std::string & name)
  50. {
  51. BMPPalette palette[256];
  52. SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
  53. ui8 *p;
  54. defName = name;
  55. DEFType = SDL_SwapLE32(de.DEFType);
  56. width = SDL_SwapLE32(de.width);
  57. height = SDL_SwapLE32(de.height);
  58. ui32 totalBlocks = SDL_SwapLE32(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].F = 0;
  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(ui32 j=0; j<SEntries.size(); ++j)
  99. {
  100. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].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. void CDefHandler::expand(ui8 N,ui8 & BL, ui8 & BR)
  113. {
  114. BL = (N & 0xE0) >> 5;
  115. BR = N & 0x1F;
  116. }
  117. SDL_Surface * CDefHandler::getSprite (int SIndex, const ui8 * FDef, const BMPPalette * palette) const
  118. {
  119. SDL_Surface * ret=NULL;
  120. ui32 BaseOffset,
  121. SpriteWidth, SpriteHeight, //format of sprite
  122. TotalRowLength, // length of read segment
  123. add, FullHeight,FullWidth,
  124. RowAdd, //, NextSpriteOffset; //TODO use me
  125. prSize,
  126. defType2;
  127. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  128. ui8 SegmentType;//, BL, BR; //TODO use me
  129. BaseOffset = SEntries[SIndex].offset;
  130. SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);
  131. prSize = SDL_SwapLE32(sd.prSize); //TODO use me
  132. defType2 = SDL_SwapLE32(sd.defType2);
  133. FullWidth = SDL_SwapLE32(sd.FullWidth);
  134. FullHeight = SDL_SwapLE32(sd.FullHeight);
  135. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  136. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  137. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  138. TopMargin = SDL_SwapLE32(sd.TopMargin);
  139. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  140. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  141. //if(LeftMargin + RightMargin < 0)
  142. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  143. if(LeftMargin<0)
  144. SpriteWidth+=LeftMargin;
  145. if(RightMargin<0)
  146. SpriteWidth+=RightMargin;
  147. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  148. add = 4 - FullWidth%4;
  149. if (add==4)
  150. add=0;
  151. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  152. BaseOffset += sizeof(SSpriteDef);
  153. int BaseOffsetor = BaseOffset;
  154. for(int i=0; i<256; ++i)
  155. {
  156. SDL_Color pr;
  157. pr.r = palette[i].R;
  158. pr.g = palette[i].G;
  159. pr.b = palette[i].B;
  160. pr.unused = palette[i].F;
  161. (*(ret->format->palette->colors+i))=pr;
  162. }
  163. int ftcp=0;
  164. // If there's a margin anywhere, just blank out the whole surface.
  165. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  166. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  167. }
  168. // Skip top margin
  169. if (TopMargin > 0)
  170. ftcp += TopMargin*(FullWidth+add);
  171. switch(defType2)
  172. {
  173. case 0:
  174. {
  175. for (ui32 i=0;i<SpriteHeight;i++)
  176. {
  177. if (LeftMargin>0)
  178. ftcp += LeftMargin;
  179. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  180. ftcp += SpriteWidth;
  181. BaseOffset += SpriteWidth;
  182. if (RightMargin>0)
  183. ftcp += RightMargin;
  184. }
  185. }
  186. break;
  187. case 1:
  188. {
  189. const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+BaseOffset);
  190. BaseOffset += sizeof(int) * SpriteHeight;
  191. for (ui32 i=0;i<SpriteHeight;i++)
  192. {
  193. BaseOffset=BaseOffsetor + read_le_u32(RWEntriesLoc + i);
  194. if (LeftMargin>0)
  195. ftcp += LeftMargin;
  196. TotalRowLength=0;
  197. do
  198. {
  199. ui32 SegmentLength;
  200. SegmentType=FDef[BaseOffset++];
  201. SegmentLength=FDef[BaseOffset++] + 1;
  202. if (SegmentType==0xFF)
  203. {
  204. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, FDef + BaseOffset, SegmentLength);
  205. BaseOffset+=SegmentLength;
  206. }
  207. else
  208. {
  209. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
  210. }
  211. ftcp += SegmentLength;
  212. TotalRowLength += SegmentLength;
  213. }while(TotalRowLength<SpriteWidth);
  214. RowAdd=SpriteWidth-TotalRowLength;
  215. if (RightMargin>0)
  216. ftcp += RightMargin;
  217. if (add>0)
  218. ftcp += add+RowAdd;
  219. }
  220. }
  221. break;
  222. case 2:
  223. {
  224. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor);
  225. for (ui32 i=0;i<SpriteHeight;i++)
  226. {
  227. //BaseOffset = BaseOffsetor+RWEntries[i];
  228. if (LeftMargin>0)
  229. ftcp += LeftMargin;
  230. TotalRowLength=0;
  231. do
  232. {
  233. SegmentType=FDef[BaseOffset++];
  234. ui8 code = SegmentType / 32;
  235. ui8 value = (SegmentType & 31) + 1;
  236. if(code==7)
  237. {
  238. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  239. ftcp += value;
  240. BaseOffset += value;
  241. }
  242. else
  243. {
  244. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
  245. ftcp += value;
  246. }
  247. TotalRowLength+=value;
  248. } while(TotalRowLength<SpriteWidth);
  249. if (RightMargin>0)
  250. ftcp += RightMargin;
  251. RowAdd=SpriteWidth-TotalRowLength;
  252. if (add>0)
  253. ftcp += add+RowAdd;
  254. }
  255. }
  256. break;
  257. case 3:
  258. {
  259. for (ui32 i=0;i<SpriteHeight;i++)
  260. {
  261. BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32));
  262. if (LeftMargin>0)
  263. ftcp += LeftMargin;
  264. TotalRowLength=0;
  265. do
  266. {
  267. SegmentType=FDef[BaseOffset++];
  268. ui8 code = SegmentType / 32;
  269. ui8 value = (SegmentType & 31) + 1;
  270. int len = std::min<ui32>(value, SpriteWidth - TotalRowLength) - std::max(0, -LeftMargin);
  271. vstd::amax(len, 0);
  272. if(code==7)
  273. {
  274. memcpy((ui8*)ret->pixels + ftcp, FDef + BaseOffset, len);
  275. ftcp += len;
  276. BaseOffset += len;
  277. }
  278. else
  279. {
  280. memset((ui8*)ret->pixels + ftcp, code, len);
  281. ftcp += len;
  282. }
  283. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  284. }while(TotalRowLength<SpriteWidth);
  285. if (RightMargin>0)
  286. ftcp += RightMargin;
  287. RowAdd=SpriteWidth-TotalRowLength;
  288. if (add>0)
  289. ftcp += add+RowAdd;
  290. }
  291. }
  292. break;
  293. default:
  294. throw std::string("Unknown sprite format.");
  295. break;
  296. }
  297. SDL_Color ttcol = ret->format->palette->colors[0];
  298. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  299. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  300. return ret;
  301. };
  302. CDefEssential * CDefHandler::essentialize()
  303. {
  304. CDefEssential * ret = new CDefEssential;
  305. ret->ourImages = ourImages;
  306. notFreeImgs = true;
  307. return ret;
  308. }
  309. CDefHandler * CDefHandler::giveDef(const std::string & defName)
  310. {
  311. ui8 * data = spriteh->giveFile(defName, FILE_ANIMATION);
  312. if(!data)
  313. throw "bad def name!";
  314. CDefHandler * nh = new CDefHandler();
  315. nh->openFromMemory(data, defName);
  316. delete [] data;
  317. return nh;
  318. }
  319. CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
  320. {
  321. CDefEssential * ret;
  322. CDefHandler * temp = giveDef(defName);
  323. ret = temp->essentialize();
  324. delete temp;
  325. return ret;
  326. }