CDefHandler.cpp 9.2 KB

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