CDefHandler.cpp 9.2 KB

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