CDefHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. RWEntries = NULL;
  28. notFreeImgs = false;
  29. }
  30. CDefHandler::~CDefHandler()
  31. {
  32. //if (FDef)
  33. //delete [] FDef;
  34. if (RWEntries)
  35. delete [] RWEntries;
  36. if (notFreeImgs)
  37. return;
  38. for (size_t i=0; i<ourImages.size(); ++i)
  39. {
  40. if (ourImages[i].bitmap)
  41. {
  42. SDL_FreeSurface(ourImages[i].bitmap);
  43. ourImages[i].bitmap=NULL;
  44. }
  45. }
  46. }
  47. CDefEssential::~CDefEssential()
  48. {
  49. for(size_t i=0; i < ourImages.size(); ++i)
  50. SDL_FreeSurface(ourImages[i].bitmap);
  51. }
  52. void CDefHandler::openDef(std::string name)
  53. {
  54. int i,j, totalInBlock;
  55. char Buffer[13];
  56. BMPPalette palette[256];
  57. defName=name;
  58. int andame;
  59. std::ifstream * is = new std::ifstream();
  60. is -> open(name.c_str(),std::ios::binary);
  61. is->seekg(0,std::ios::end); // na koniec
  62. andame = is->tellg(); // read length
  63. is->seekg(0,std::ios::beg); // wracamy na poczatek
  64. unsigned char * FDef = new unsigned char[andame]; // allocate memory
  65. is->read((char*)FDef, andame); // read map file to buffer
  66. is->close();
  67. delete is;
  68. i = 0;
  69. DEFType = readNormalNr(i,4,FDef); i+=4;
  70. width = readNormalNr(i,4,FDef); i+=4;
  71. height = readNormalNr(i,4,FDef); i+=4;
  72. i=0xc;
  73. totalBlocks = readNormalNr(i,4,FDef); i+=4;
  74. i=0x10;
  75. for (int it=0;it<256;it++)
  76. {
  77. palette[it].R = FDef[i++];
  78. palette[it].G = FDef[i++];
  79. palette[it].B = FDef[i++];
  80. palette[it].F = 0;
  81. }
  82. i=0x310;
  83. totalEntries=0;
  84. for (int z=0; z<totalBlocks; z++)
  85. {
  86. i+=4;
  87. totalInBlock = readNormalNr(i,4,FDef); i+=4;
  88. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  89. SEntries.push_back(SEntry());
  90. i+=8;
  91. for (j=0; j<totalInBlock; j++)
  92. {
  93. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  94. i+=13;
  95. SEntries[totalEntries+j].name=Buffer;
  96. }
  97. for (j=0; j<totalInBlock; j++)
  98. {
  99. SEntries[totalEntries+j].offset = readNormalNr(i,4,FDef);
  100. i+=4;
  101. }
  102. //totalEntries+=totalInBlock;
  103. for(int hh=0; hh<totalInBlock; ++hh)
  104. {
  105. SEntries[totalEntries].group = z;
  106. ++totalEntries;
  107. }
  108. }
  109. for(j=0; j<SEntries.size(); ++j)
  110. {
  111. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  112. }
  113. for(size_t i=0; i < SEntries.size(); ++i)
  114. {
  115. Cimage nimg;
  116. nimg.bitmap = getSprite(i, FDef, palette);
  117. nimg.imName = SEntries[i].name;
  118. nimg.groupNumber = SEntries[i].group;
  119. ourImages.push_back(nimg);
  120. }
  121. delete [] FDef;
  122. FDef = NULL;
  123. }
  124. void CDefHandler::openFromMemory(unsigned char *table, std::string name)
  125. {
  126. int i,j, totalInBlock;
  127. char Buffer[13];
  128. BMPPalette palette[256];
  129. defName=name;
  130. i = 0;
  131. DEFType = readNormalNr(i,4,table); i+=4;
  132. width = readNormalNr(i,4,table); i+=4;
  133. height = readNormalNr(i,4,table); i+=4;
  134. i=0xc;
  135. totalBlocks = readNormalNr(i,4,table); i+=4;
  136. i=0x10;
  137. for (int it=0;it<256;it++)
  138. {
  139. palette[it].R = table[i++];
  140. palette[it].G = table[i++];
  141. palette[it].B = table[i++];
  142. palette[it].F = 0;
  143. }
  144. i=0x310;
  145. totalEntries=0;
  146. for (int z=0; z<totalBlocks; z++)
  147. {
  148. int unknown1 = readNormalNr(i,4,table); i+=4; //TODO use me
  149. totalInBlock = readNormalNr(i,4,table); i+=4;
  150. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  151. SEntries.push_back(SEntry());
  152. int unknown2 = readNormalNr(i,4,table); //TODO use me
  153. i+=4;
  154. int unknown3 = readNormalNr(i,4,table); //TODO use me
  155. i+=4;
  156. for (j=0; j<totalInBlock; j++)
  157. {
  158. for (int k=0;k<13;k++) Buffer[k]=table[i+k];
  159. i+=13;
  160. SEntries[totalEntries+j].name=Buffer;
  161. }
  162. for (j=0; j<totalInBlock; j++)
  163. {
  164. SEntries[totalEntries+j].offset = readNormalNr(i,4,table);
  165. int unknown4 = readNormalNr(i,4,table); //TODO use me
  166. i+=4;
  167. }
  168. //totalEntries+=totalInBlock;
  169. for(int hh=0; hh<totalInBlock; ++hh)
  170. {
  171. SEntries[totalEntries].group = z;
  172. ++totalEntries;
  173. }
  174. }
  175. for(j=0; j<SEntries.size(); ++j)
  176. {
  177. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  178. }
  179. RWEntries = new unsigned int[height];
  180. for(size_t i=0; i < SEntries.size(); ++i)
  181. {
  182. Cimage nimg;
  183. nimg.bitmap = getSprite(i, table, palette);
  184. nimg.imName = SEntries[i].name;
  185. nimg.groupNumber = SEntries[i].group;
  186. ourImages.push_back(nimg);
  187. }
  188. }
  189. unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
  190. {
  191. //int tralalalatoniedziala = 2*9+100-4*bytCon;
  192. //unsigned char * ret = new unsigned char[bytCon];
  193. unsigned char * ret = NULL;
  194. for(int jj=0; jj<100; ++jj)
  195. {
  196. ret = (unsigned char*)calloc(1, bytCon);
  197. if(ret!=NULL)
  198. break;
  199. }
  200. long long amp = pow((long long int)256,bytCon-1);
  201. for (int i=bytCon-1; i>=0;i--)
  202. {
  203. int test2 = nr/(amp);
  204. ret[i]=test2;
  205. nr -= (nr/(amp))*amp;
  206. amp/=256;
  207. }
  208. return ret;
  209. }
  210. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  211. {
  212. BL = (N & 0xE0) >> 5;
  213. BR = N & 0x1F;
  214. }
  215. int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
  216. {
  217. int ret=0;
  218. int amp=1;
  219. if (str)
  220. {
  221. for (int i=0; i<bytCon; i++)
  222. {
  223. ret+=str[pos+i]*amp;
  224. amp*=256;
  225. }
  226. }
  227. //else
  228. //{
  229. // for (int i=0; i<bytCon; i++)
  230. // {
  231. // ret+=FDef[pos+i]*amp;
  232. // amp*=256;
  233. // }
  234. //}
  235. if(cyclic && bytCon<4 && ret>=amp/2)
  236. {
  237. ret = ret-amp;
  238. }
  239. return ret;
  240. }
  241. void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
  242. {
  243. unsigned char * temp = writeNormalNr(nr,bytcon);
  244. for (int i=0;i<bytcon;i++)
  245. stream << char(temp[i]);
  246. free(temp);
  247. }
  248. SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette)
  249. {
  250. SDL_Surface * ret=NULL;
  251. long BaseOffset,
  252. SpriteWidth, SpriteHeight, //format sprite'a
  253. LeftMargin, RightMargin, TopMargin,BottomMargin,
  254. i, add, FullHeight,FullWidth,
  255. TotalRowLength, // dlugosc przeczytanego segmentu
  256. RowAdd;//, NextSpriteOffset; //TODO use me
  257. unsigned char SegmentType;//, BL, BR; //TODO use me
  258. i=BaseOffset=SEntries[SIndex].offset;
  259. int prSize=readNormalNr(i,4,FDef);i+=4; //TODO use me
  260. int defType2 = readNormalNr(i,4,FDef);i+=4;
  261. FullWidth = readNormalNr(i,4,FDef);i+=4;
  262. FullHeight = readNormalNr(i,4,FDef);i+=4;
  263. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  264. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  265. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  266. TopMargin = readNormalNr(i,4,FDef);i+=4;
  267. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  268. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  269. //if(LeftMargin + RightMargin < 0)
  270. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  271. if(LeftMargin<0)
  272. SpriteWidth+=LeftMargin;
  273. if(RightMargin<0)
  274. SpriteWidth+=RightMargin;
  275. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  276. add = 4 - FullWidth%4;
  277. if (add==4)
  278. add=0;
  279. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  280. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  281. int BaseOffsetor = BaseOffset = i;
  282. for(int i=0; i<256; ++i)
  283. {
  284. SDL_Color pr;
  285. pr.r = palette[i].R;
  286. pr.g = palette[i].G;
  287. pr.b = palette[i].B;
  288. pr.unused = palette[i].F;
  289. (*(ret->format->palette->colors+i))=pr;
  290. }
  291. int ftcp=0;
  292. // If there's a margin anywhere, just blank out the whole surface.
  293. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  294. memset(((char *)ret->pixels), 0, FullHeight*FullWidth);
  295. }
  296. // Skip top margin
  297. if (TopMargin > 0)
  298. ftcp += TopMargin*(FullWidth+add);
  299. switch(defType2)
  300. {
  301. case 0:
  302. {
  303. for (int i=0;i<SpriteHeight;i++)
  304. {
  305. if (LeftMargin>0)
  306. ftcp += LeftMargin;
  307. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  308. ftcp += SpriteWidth;
  309. BaseOffset += SpriteWidth;
  310. if (RightMargin>0)
  311. ftcp += RightMargin;
  312. }
  313. }
  314. break;
  315. case 1:
  316. {
  317. memcpy(RWEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
  318. BaseOffset += sizeof(int) * SpriteHeight;
  319. for (int i=0;i<SpriteHeight;i++)
  320. {
  321. BaseOffset=BaseOffsetor+RWEntries[i];
  322. if (LeftMargin>0)
  323. ftcp += LeftMargin;
  324. TotalRowLength=0;
  325. do
  326. {
  327. unsigned int SegmentLength;
  328. SegmentType=FDef[BaseOffset++];
  329. SegmentLength=FDef[BaseOffset++] + 1;
  330. if (SegmentType==0xFF)
  331. {
  332. for (int k=0;k<SegmentLength;k++)
  333. {
  334. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  335. if ((TotalRowLength+k)>=SpriteWidth)
  336. break;
  337. }
  338. BaseOffset+=SegmentLength;
  339. TotalRowLength+=SegmentLength;
  340. }
  341. else
  342. {
  343. memset((char*)(ret->pixels)+ftcp, SegmentType, SegmentLength);
  344. ftcp += SegmentLength;
  345. TotalRowLength += SegmentLength;
  346. }
  347. }while(TotalRowLength<SpriteWidth);
  348. RowAdd=SpriteWidth-TotalRowLength;
  349. if (RightMargin>0)
  350. ftcp += RightMargin;
  351. if (add>0)
  352. ftcp += add+RowAdd;
  353. }
  354. }
  355. break;
  356. case 2:
  357. {
  358. for (int i=0;i<SpriteHeight;i++)
  359. {
  360. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  361. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  362. }
  363. BaseOffset = BaseOffsetor+RWEntries[0];
  364. for (int i=0;i<SpriteHeight;i++)
  365. {
  366. //BaseOffset = BaseOffsetor+RWEntries[i];
  367. if (LeftMargin>0)
  368. ftcp += LeftMargin;
  369. TotalRowLength=0;
  370. do
  371. {
  372. SegmentType=FDef[BaseOffset++];
  373. unsigned char code = SegmentType / 32;
  374. unsigned char value = (SegmentType & 31) + 1;
  375. if(code==7)
  376. {
  377. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  378. ftcp += value;
  379. BaseOffset += value;
  380. }
  381. else
  382. {
  383. memset((char*)(ret->pixels)+ftcp, code, value);
  384. ftcp += value;
  385. }
  386. TotalRowLength+=value;
  387. } while(TotalRowLength<SpriteWidth);
  388. if (RightMargin>0)
  389. ftcp += RightMargin;
  390. RowAdd=SpriteWidth-TotalRowLength;
  391. if (add>0)
  392. ftcp += add+RowAdd;
  393. }
  394. }
  395. break;
  396. case 3:
  397. {
  398. for (int i=0;i<SpriteHeight;i++)
  399. {
  400. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  401. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  402. }
  403. for (int i=0;i<SpriteHeight;i++)
  404. {
  405. BaseOffset = BaseOffsetor+RWEntries[i];
  406. if (LeftMargin>0)
  407. ftcp += LeftMargin;
  408. TotalRowLength=0;
  409. do
  410. {
  411. SegmentType=FDef[BaseOffset++];
  412. unsigned char code = SegmentType / 32;
  413. unsigned char value = (SegmentType & 31) + 1;
  414. if(code==7)
  415. {
  416. for(int h=0; h<value; ++h)
  417. {
  418. if(h<-LeftMargin)
  419. continue;
  420. if(h+TotalRowLength>=SpriteWidth)
  421. break;
  422. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  423. }
  424. }
  425. else
  426. {
  427. for(int h=0; h<value; ++h)
  428. {
  429. if(h<-LeftMargin)
  430. continue;
  431. if(h+TotalRowLength>=SpriteWidth)
  432. break;
  433. ((char*)(ret->pixels))[ftcp++]=code;
  434. }
  435. }
  436. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  437. }while(TotalRowLength<SpriteWidth);
  438. if (RightMargin>0)
  439. ftcp += RightMargin;
  440. RowAdd=SpriteWidth-TotalRowLength;
  441. if (add>0)
  442. ftcp += add+RowAdd;
  443. }
  444. }
  445. break;
  446. default:
  447. throw std::string("Unknown sprite format.");
  448. break;
  449. }
  450. SDL_Color ttcol = ret->format->palette->colors[0];
  451. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  452. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  453. return ret;
  454. };
  455. CDefEssential * CDefHandler::essentialize()
  456. {
  457. CDefEssential * ret = new CDefEssential;
  458. ret->ourImages = ourImages;
  459. notFreeImgs = true;
  460. return ret;
  461. }
  462. CDefHandler * CDefHandler::giveDef(std::string defName)
  463. {
  464. unsigned char * data = spriteh->giveFile(defName);
  465. if(!data)
  466. throw "bad def name!";
  467. CDefHandler * nh = new CDefHandler();
  468. nh->openFromMemory(data, defName);
  469. nh->alphaTransformed = false;
  470. delete [] data;
  471. return nh;
  472. }
  473. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  474. {
  475. CDefEssential * ret;
  476. CDefHandler * temp = giveDef(defName);
  477. ret = temp->essentialize();
  478. delete temp;
  479. return ret;
  480. }