CDefHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /*
  8. * CDefHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  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. CDefHandler::CDefHandler()
  25. {
  26. //FDef = NULL;
  27. notFreeImgs = false;
  28. }
  29. CDefHandler::~CDefHandler()
  30. {
  31. //if (FDef)
  32. //delete [] FDef;
  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. // Note: this function is unused
  50. void CDefHandler::openDef(std::string name)
  51. {
  52. int andame;
  53. std::ifstream * is = new std::ifstream();
  54. is -> open(name.c_str(),std::ios::binary);
  55. is->seekg(0,std::ios::end); // na koniec
  56. andame = is->tellg(); // read length
  57. is->seekg(0,std::ios::beg); // wracamy na poczatek
  58. unsigned char * FDef = new unsigned char[andame]; // allocate memory
  59. is->read((char*)FDef, andame); // read map file to buffer
  60. is->close();
  61. delete is;
  62. openFromMemory(FDef, name);
  63. delete [] FDef;
  64. }
  65. void CDefHandler::openFromMemory(unsigned char *table, std::string name)
  66. {
  67. BMPPalette palette[256];
  68. SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
  69. unsigned char *p;
  70. defName = name;
  71. DEFType = SDL_SwapLE32(de.DEFType);
  72. width = SDL_SwapLE32(de.width);
  73. height = SDL_SwapLE32(de.height);
  74. unsigned int totalBlocks = SDL_SwapLE32(de.totalBlocks);
  75. for (unsigned int it=0;it<256;it++)
  76. {
  77. palette[it].R = de.palette[it].R;
  78. palette[it].G = de.palette[it].G;
  79. palette[it].B = de.palette[it].B;
  80. palette[it].F = 0;
  81. }
  82. // The SDefEntryBlock starts just after the SDefEntry
  83. p = reinterpret_cast<unsigned char *>(&de);
  84. p += sizeof(de);
  85. int totalEntries=0;
  86. for (unsigned int z=0; z<totalBlocks; z++)
  87. {
  88. SDefEntryBlock &block = * reinterpret_cast<SDefEntryBlock *>(p);
  89. unsigned int totalInBlock;
  90. totalInBlock = SDL_SwapLE32(block.totalInBlock);
  91. for (unsigned int j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  92. SEntries.push_back(SEntry());
  93. p = block.data;
  94. for (unsigned int j=0; j<totalInBlock; j++)
  95. {
  96. char Buffer[13];
  97. memcpy(Buffer, p, 12);
  98. Buffer[12] = 0;
  99. SEntries[totalEntries+j].name=Buffer;
  100. p += 13;
  101. }
  102. for (unsigned int j=0; j<totalInBlock; j++)
  103. {
  104. SEntries[totalEntries+j].offset = SDL_SwapLE32(* reinterpret_cast<Uint32 *>(p));
  105. p += 4;
  106. }
  107. //totalEntries+=totalInBlock;
  108. for(unsigned int hh=0; hh<totalInBlock; ++hh)
  109. {
  110. SEntries[totalEntries].group = z;
  111. ++totalEntries;
  112. }
  113. }
  114. for(unsigned int j=0; j<SEntries.size(); ++j)
  115. {
  116. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  117. }
  118. //RWEntries = new unsigned int[height];
  119. for(unsigned int i=0; i < SEntries.size(); ++i)
  120. {
  121. Cimage nimg;
  122. nimg.bitmap = getSprite(i, table, palette);
  123. nimg.imName = SEntries[i].name;
  124. nimg.groupNumber = SEntries[i].group;
  125. ourImages.push_back(nimg);
  126. }
  127. }
  128. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  129. {
  130. BL = (N & 0xE0) >> 5;
  131. BR = N & 0x1F;
  132. }
  133. int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
  134. {
  135. int ret=0;
  136. int amp=1;
  137. if (str)
  138. {
  139. for (int i=0; i<bytCon; i++)
  140. {
  141. ret+=str[pos+i]*amp;
  142. amp*=256;
  143. }
  144. }
  145. //else
  146. //{
  147. // for (int i=0; i<bytCon; i++)
  148. // {
  149. // ret+=FDef[pos+i]*amp;
  150. // amp*=256;
  151. // }
  152. //}
  153. if(cyclic && bytCon<4 && ret>=amp/2)
  154. {
  155. ret = ret-amp;
  156. }
  157. return ret;
  158. }
  159. SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const
  160. {
  161. SDL_Surface * ret=NULL;
  162. unsigned int BaseOffset,
  163. SpriteWidth, SpriteHeight, //format sprite'a
  164. TotalRowLength, // dlugosc przeczytanego segmentu
  165. add, FullHeight,FullWidth,
  166. RowAdd, //, NextSpriteOffset; //TODO use me
  167. prSize,
  168. defType2;
  169. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  170. unsigned char SegmentType;//, BL, BR; //TODO use me
  171. BaseOffset = SEntries[SIndex].offset;
  172. SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);
  173. prSize = SDL_SwapLE32(sd.prSize); //TODO use me
  174. defType2 = SDL_SwapLE32(sd.defType2);
  175. FullWidth = SDL_SwapLE32(sd.FullWidth);
  176. FullHeight = SDL_SwapLE32(sd.FullHeight);
  177. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  178. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  179. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  180. TopMargin = SDL_SwapLE32(sd.TopMargin);
  181. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  182. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  183. //if(LeftMargin + RightMargin < 0)
  184. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  185. if(LeftMargin<0)
  186. SpriteWidth+=LeftMargin;
  187. if(RightMargin<0)
  188. SpriteWidth+=RightMargin;
  189. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  190. add = 4 - FullWidth%4;
  191. if (add==4)
  192. add=0;
  193. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  194. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  195. BaseOffset += sizeof(SSpriteDef);
  196. int BaseOffsetor = BaseOffset;
  197. for(int i=0; i<256; ++i)
  198. {
  199. SDL_Color pr;
  200. pr.r = palette[i].R;
  201. pr.g = palette[i].G;
  202. pr.b = palette[i].B;
  203. pr.unused = palette[i].F;
  204. (*(ret->format->palette->colors+i))=pr;
  205. }
  206. int ftcp=0;
  207. // If there's a margin anywhere, just blank out the whole surface.
  208. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  209. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  210. }
  211. // Skip top margin
  212. if (TopMargin > 0)
  213. ftcp += TopMargin*(FullWidth+add);
  214. switch(defType2)
  215. {
  216. case 0:
  217. {
  218. for (unsigned int i=0;i<SpriteHeight;i++)
  219. {
  220. if (LeftMargin>0)
  221. ftcp += LeftMargin;
  222. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  223. ftcp += SpriteWidth;
  224. BaseOffset += SpriteWidth;
  225. if (RightMargin>0)
  226. ftcp += RightMargin;
  227. }
  228. }
  229. break;
  230. case 1:
  231. {
  232. const unsigned int * RWEntriesLoc = reinterpret_cast<const unsigned int *>(FDef+BaseOffset);
  233. BaseOffset += sizeof(int) * SpriteHeight;
  234. for (unsigned int i=0;i<SpriteHeight;i++)
  235. {
  236. BaseOffset=BaseOffsetor+RWEntriesLoc[i];
  237. if (LeftMargin>0)
  238. ftcp += LeftMargin;
  239. TotalRowLength=0;
  240. do
  241. {
  242. unsigned int SegmentLength;
  243. SegmentType=FDef[BaseOffset++];
  244. SegmentLength=FDef[BaseOffset++] + 1;
  245. if (SegmentType==0xFF)
  246. {
  247. for (unsigned int k=0; k<SegmentLength;k++)
  248. {
  249. (reinterpret_cast<char*>(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  250. if ((TotalRowLength+k)>=SpriteWidth)
  251. break;
  252. }
  253. BaseOffset+=SegmentLength;
  254. TotalRowLength+=SegmentLength;
  255. }
  256. else
  257. {
  258. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
  259. ftcp += SegmentLength;
  260. TotalRowLength += SegmentLength;
  261. }
  262. }while(TotalRowLength<SpriteWidth);
  263. RowAdd=SpriteWidth-TotalRowLength;
  264. if (RightMargin>0)
  265. ftcp += RightMargin;
  266. if (add>0)
  267. ftcp += add+RowAdd;
  268. }
  269. }
  270. break;
  271. case 2:
  272. {
  273. BaseOffset = BaseOffsetor + *reinterpret_cast<const unsigned short*>( FDef + BaseOffsetor ); //was + RWEntries[0];
  274. for (unsigned int i=0;i<SpriteHeight;i++)
  275. {
  276. //BaseOffset = BaseOffsetor+RWEntries[i];
  277. if (LeftMargin>0)
  278. ftcp += LeftMargin;
  279. TotalRowLength=0;
  280. do
  281. {
  282. SegmentType=FDef[BaseOffset++];
  283. unsigned char code = SegmentType / 32;
  284. unsigned char value = (SegmentType & 31) + 1;
  285. if(code==7)
  286. {
  287. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  288. ftcp += value;
  289. BaseOffset += value;
  290. }
  291. else
  292. {
  293. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
  294. ftcp += value;
  295. }
  296. TotalRowLength+=value;
  297. } while(TotalRowLength<SpriteWidth);
  298. if (RightMargin>0)
  299. ftcp += RightMargin;
  300. RowAdd=SpriteWidth-TotalRowLength;
  301. if (add>0)
  302. ftcp += add+RowAdd;
  303. }
  304. }
  305. break;
  306. case 3:
  307. {
  308. for (unsigned int i=0;i<SpriteHeight;i++)
  309. {
  310. BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor+i*2*(SpriteWidth/32) ); //was + RWEntries[i] before speedup
  311. if (LeftMargin>0)
  312. ftcp += LeftMargin;
  313. TotalRowLength=0;
  314. do
  315. {
  316. SegmentType=FDef[BaseOffset++];
  317. unsigned char code = SegmentType / 32;
  318. unsigned char value = (SegmentType & 31) + 1;
  319. if(code==7)
  320. {
  321. for(int h=0; h<value; ++h)
  322. {
  323. if(h<-LeftMargin)
  324. continue;
  325. if(h+TotalRowLength>=SpriteWidth)
  326. break;
  327. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  328. }
  329. }
  330. else
  331. {
  332. for(int h=0; h<value; ++h)
  333. {
  334. if(h<-LeftMargin)
  335. continue;
  336. if(h+TotalRowLength>=SpriteWidth)
  337. break;
  338. ((char*)(ret->pixels))[ftcp++]=code;
  339. }
  340. }
  341. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  342. }while(TotalRowLength<SpriteWidth);
  343. if (RightMargin>0)
  344. ftcp += RightMargin;
  345. RowAdd=SpriteWidth-TotalRowLength;
  346. if (add>0)
  347. ftcp += add+RowAdd;
  348. }
  349. }
  350. break;
  351. default:
  352. throw std::string("Unknown sprite format.");
  353. break;
  354. }
  355. SDL_Color ttcol = ret->format->palette->colors[0];
  356. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  357. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  358. return ret;
  359. };
  360. CDefEssential * CDefHandler::essentialize()
  361. {
  362. CDefEssential * ret = new CDefEssential;
  363. ret->ourImages = ourImages;
  364. notFreeImgs = true;
  365. return ret;
  366. }
  367. CDefHandler * CDefHandler::giveDef(std::string defName)
  368. {
  369. unsigned char * data = spriteh->giveFile(defName);
  370. if(!data)
  371. throw "bad def name!";
  372. CDefHandler * nh = new CDefHandler();
  373. nh->openFromMemory(data, defName);
  374. nh->alphaTransformed = false;
  375. delete [] data;
  376. return nh;
  377. }
  378. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  379. {
  380. CDefEssential * ret;
  381. CDefHandler * temp = giveDef(defName);
  382. ret = temp->essentialize();
  383. delete temp;
  384. return ret;
  385. }