CDefHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. 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. 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. unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
  129. {
  130. //int tralalalatoniedziala = 2*9+100-4*bytCon;
  131. //unsigned char * ret = new unsigned char[bytCon];
  132. unsigned char * ret = NULL;
  133. for(int jj=0; jj<100; ++jj)
  134. {
  135. ret = (unsigned char*)calloc(1, bytCon);
  136. if(ret!=NULL)
  137. break;
  138. }
  139. long long amp = pow((long long int)256,bytCon-1);
  140. for (int i=bytCon-1; i>=0;i--)
  141. {
  142. int test2 = nr/(amp);
  143. ret[i]=test2;
  144. nr -= (nr/(amp))*amp;
  145. amp/=256;
  146. }
  147. return ret;
  148. }
  149. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  150. {
  151. BL = (N & 0xE0) >> 5;
  152. BR = N & 0x1F;
  153. }
  154. int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
  155. {
  156. int ret=0;
  157. int amp=1;
  158. if (str)
  159. {
  160. for (int i=0; i<bytCon; i++)
  161. {
  162. ret+=str[pos+i]*amp;
  163. amp*=256;
  164. }
  165. }
  166. //else
  167. //{
  168. // for (int i=0; i<bytCon; i++)
  169. // {
  170. // ret+=FDef[pos+i]*amp;
  171. // amp*=256;
  172. // }
  173. //}
  174. if(cyclic && bytCon<4 && ret>=amp/2)
  175. {
  176. ret = ret-amp;
  177. }
  178. return ret;
  179. }
  180. void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
  181. {
  182. unsigned char * temp = writeNormalNr(nr,bytcon);
  183. for (int i=0;i<bytcon;i++)
  184. stream << char(temp[i]);
  185. free(temp);
  186. }
  187. SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette)
  188. {
  189. SDL_Surface * ret=NULL;
  190. unsigned int BaseOffset,
  191. SpriteWidth, SpriteHeight, //format sprite'a
  192. TotalRowLength, // dlugosc przeczytanego segmentu
  193. add, FullHeight,FullWidth,
  194. RowAdd, //, NextSpriteOffset; //TODO use me
  195. prSize,
  196. defType2;
  197. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  198. unsigned char SegmentType;//, BL, BR; //TODO use me
  199. BaseOffset = SEntries[SIndex].offset;
  200. SSpriteDef sd = * reinterpret_cast<SSpriteDef *>(FDef + BaseOffset);
  201. prSize = SDL_SwapLE32(sd.prSize); //TODO use me
  202. defType2 = SDL_SwapLE32(sd.defType2);
  203. FullWidth = SDL_SwapLE32(sd.FullWidth);
  204. FullHeight = SDL_SwapLE32(sd.FullHeight);
  205. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  206. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  207. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  208. TopMargin = SDL_SwapLE32(sd.TopMargin);
  209. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  210. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  211. //if(LeftMargin + RightMargin < 0)
  212. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  213. if(LeftMargin<0)
  214. SpriteWidth+=LeftMargin;
  215. if(RightMargin<0)
  216. SpriteWidth+=RightMargin;
  217. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  218. add = 4 - FullWidth%4;
  219. if (add==4)
  220. add=0;
  221. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  222. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  223. BaseOffset += sizeof(SSpriteDef);
  224. int BaseOffsetor = BaseOffset;
  225. for(int i=0; i<256; ++i)
  226. {
  227. SDL_Color pr;
  228. pr.r = palette[i].R;
  229. pr.g = palette[i].G;
  230. pr.b = palette[i].B;
  231. pr.unused = palette[i].F;
  232. (*(ret->format->palette->colors+i))=pr;
  233. }
  234. int ftcp=0;
  235. // If there's a margin anywhere, just blank out the whole surface.
  236. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  237. memset(((char *)ret->pixels), 0, FullHeight*FullWidth);
  238. }
  239. // Skip top margin
  240. if (TopMargin > 0)
  241. ftcp += TopMargin*(FullWidth+add);
  242. switch(defType2)
  243. {
  244. case 0:
  245. {
  246. for (unsigned int i=0;i<SpriteHeight;i++)
  247. {
  248. if (LeftMargin>0)
  249. ftcp += LeftMargin;
  250. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  251. ftcp += SpriteWidth;
  252. BaseOffset += SpriteWidth;
  253. if (RightMargin>0)
  254. ftcp += RightMargin;
  255. }
  256. }
  257. break;
  258. case 1:
  259. {
  260. unsigned int * RWEntriesLoc = reinterpret_cast<unsigned int *>(FDef+BaseOffset);
  261. BaseOffset += sizeof(int) * SpriteHeight;
  262. for (unsigned int i=0;i<SpriteHeight;i++)
  263. {
  264. BaseOffset=BaseOffsetor+RWEntriesLoc[i];
  265. if (LeftMargin>0)
  266. ftcp += LeftMargin;
  267. TotalRowLength=0;
  268. do
  269. {
  270. unsigned int SegmentLength;
  271. SegmentType=FDef[BaseOffset++];
  272. SegmentLength=FDef[BaseOffset++] + 1;
  273. if (SegmentType==0xFF)
  274. {
  275. for (unsigned int k=0; k<SegmentLength;k++)
  276. {
  277. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  278. if ((TotalRowLength+k)>=SpriteWidth)
  279. break;
  280. }
  281. BaseOffset+=SegmentLength;
  282. TotalRowLength+=SegmentLength;
  283. }
  284. else
  285. {
  286. memset((char*)(ret->pixels)+ftcp, SegmentType, SegmentLength);
  287. ftcp += SegmentLength;
  288. TotalRowLength += SegmentLength;
  289. }
  290. }while(TotalRowLength<SpriteWidth);
  291. RowAdd=SpriteWidth-TotalRowLength;
  292. if (RightMargin>0)
  293. ftcp += RightMargin;
  294. if (add>0)
  295. ftcp += add+RowAdd;
  296. }
  297. }
  298. break;
  299. case 2:
  300. {
  301. /*for (int i=0;i<SpriteHeight;i++)
  302. {
  303. RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
  304. }*/
  305. BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor ); //was + RWEntries[0];
  306. for (unsigned int i=0;i<SpriteHeight;i++)
  307. {
  308. //BaseOffset = BaseOffsetor+RWEntries[i];
  309. if (LeftMargin>0)
  310. ftcp += LeftMargin;
  311. TotalRowLength=0;
  312. do
  313. {
  314. SegmentType=FDef[BaseOffset++];
  315. unsigned char code = SegmentType / 32;
  316. unsigned char value = (SegmentType & 31) + 1;
  317. if(code==7)
  318. {
  319. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  320. ftcp += value;
  321. BaseOffset += value;
  322. }
  323. else
  324. {
  325. memset((char*)(ret->pixels)+ftcp, code, value);
  326. ftcp += value;
  327. }
  328. TotalRowLength+=value;
  329. } while(TotalRowLength<SpriteWidth);
  330. if (RightMargin>0)
  331. ftcp += RightMargin;
  332. RowAdd=SpriteWidth-TotalRowLength;
  333. if (add>0)
  334. ftcp += add+RowAdd;
  335. }
  336. }
  337. break;
  338. case 3:
  339. {
  340. /*for (int i=0;i<SpriteHeight;i++)
  341. {
  342. RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
  343. }*/
  344. for (unsigned int i=0;i<SpriteHeight;i++)
  345. {
  346. BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor+i*2*(SpriteWidth/32) ); //was + RWEntries[i] before speedup
  347. if (LeftMargin>0)
  348. ftcp += LeftMargin;
  349. TotalRowLength=0;
  350. do
  351. {
  352. SegmentType=FDef[BaseOffset++];
  353. unsigned char code = SegmentType / 32;
  354. unsigned char value = (SegmentType & 31) + 1;
  355. if(code==7)
  356. {
  357. for(int h=0; h<value; ++h)
  358. {
  359. if(h<-LeftMargin)
  360. continue;
  361. if(h+TotalRowLength>=SpriteWidth)
  362. break;
  363. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  364. }
  365. }
  366. else
  367. {
  368. for(int h=0; h<value; ++h)
  369. {
  370. if(h<-LeftMargin)
  371. continue;
  372. if(h+TotalRowLength>=SpriteWidth)
  373. break;
  374. ((char*)(ret->pixels))[ftcp++]=code;
  375. }
  376. }
  377. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  378. }while(TotalRowLength<SpriteWidth);
  379. if (RightMargin>0)
  380. ftcp += RightMargin;
  381. RowAdd=SpriteWidth-TotalRowLength;
  382. if (add>0)
  383. ftcp += add+RowAdd;
  384. }
  385. }
  386. break;
  387. default:
  388. throw std::string("Unknown sprite format.");
  389. break;
  390. }
  391. SDL_Color ttcol = ret->format->palette->colors[0];
  392. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  393. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  394. return ret;
  395. };
  396. CDefEssential * CDefHandler::essentialize()
  397. {
  398. CDefEssential * ret = new CDefEssential;
  399. ret->ourImages = ourImages;
  400. notFreeImgs = true;
  401. return ret;
  402. }
  403. CDefHandler * CDefHandler::giveDef(std::string defName)
  404. {
  405. unsigned char * data = spriteh->giveFile(defName);
  406. if(!data)
  407. throw "bad def name!";
  408. CDefHandler * nh = new CDefHandler();
  409. nh->openFromMemory(data, defName);
  410. nh->alphaTransformed = false;
  411. delete [] data;
  412. return nh;
  413. }
  414. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  415. {
  416. CDefEssential * ret;
  417. CDefHandler * temp = giveDef(defName);
  418. ret = temp->essentialize();
  419. delete temp;
  420. return ret;
  421. }