CDefHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. 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, 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, SegmentLength;//, 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. add = 4 - FullWidth%4;
  276. if (add==4)
  277. add=0;
  278. int ftcp=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. if (defType2==0)
  292. {
  293. if (TopMargin>0)
  294. {
  295. for (int i=0;i<TopMargin;i++)
  296. {
  297. for (int j=0;j<FullWidth+add;j++)
  298. ((char*)(ret->pixels))[ftcp++]='\0';
  299. }
  300. }
  301. for (int i=0;i<SpriteHeight;i++)
  302. {
  303. if (LeftMargin>0)
  304. {
  305. for (int j=0;j<LeftMargin;j++)
  306. ((char*)(ret->pixels))[ftcp++]='\0';
  307. }
  308. for (int j=0; j<SpriteWidth;j++)
  309. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  310. if (RightMargin>0)
  311. {
  312. for (int j=0;j<add;j++)
  313. ((char*)(ret->pixels))[ftcp++]='\0';
  314. }
  315. }
  316. if (BottomMargin>0)
  317. {
  318. for (int i=0;i<BottomMargin;i++)
  319. {
  320. for (int j=0;j<FullWidth+add;j++)
  321. ((char*)(ret->pixels))[ftcp++]='\0';
  322. }
  323. }
  324. }
  325. if (defType2==1)
  326. {
  327. if (TopMargin>0)
  328. {
  329. for (int i=0;i<TopMargin;i++)
  330. {
  331. for (int j=0;j<FullWidth+add;j++)
  332. ((char*)(ret->pixels))[ftcp++]='\0';
  333. }
  334. }
  335. memcpy(RWEntries, FDef+BaseOffset, SpriteHeight*sizeof(int));
  336. BaseOffset += sizeof(int) * SpriteHeight;
  337. for (int i=0;i<SpriteHeight;i++)
  338. {
  339. BaseOffset=BaseOffsetor+RWEntries[i];
  340. if (LeftMargin>0)
  341. {
  342. for (int j=0;j<LeftMargin;j++)
  343. ((char*)(ret->pixels))[ftcp++]='\0';
  344. }
  345. TotalRowLength=0;
  346. do
  347. {
  348. SegmentType=FDef[BaseOffset++];
  349. SegmentLength=FDef[BaseOffset++];
  350. if (SegmentType==0xFF)
  351. {
  352. for (int k=0;k<=SegmentLength;k++)
  353. {
  354. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  355. if ((TotalRowLength+k+1)>=SpriteWidth)
  356. break;
  357. }
  358. BaseOffset+=SegmentLength+1;////
  359. TotalRowLength+=SegmentLength+1;
  360. }
  361. else
  362. {
  363. for (int k=0;k<SegmentLength+1;k++)
  364. {
  365. ((char*)(ret->pixels))[ftcp++]=SegmentType;//
  366. //((char*)(ret->pixels))+='\0';
  367. }
  368. TotalRowLength+=SegmentLength+1;
  369. }
  370. }while(TotalRowLength<SpriteWidth);
  371. RowAdd=SpriteWidth-TotalRowLength;
  372. if (RightMargin>0)
  373. {
  374. for (int j=0;j<RightMargin;j++)
  375. ((char*)(ret->pixels))[ftcp++]='\0';
  376. }
  377. if (add>0)
  378. {
  379. for (int j=0;j<add+RowAdd;j++)
  380. ((char*)(ret->pixels))[ftcp++]='\0';
  381. }
  382. }
  383. if (BottomMargin>0)
  384. {
  385. for (int i=0;i<BottomMargin;i++)
  386. {
  387. for (int j=0;j<FullWidth+add;j++)
  388. ((char*)(ret->pixels))[ftcp++]='\0';
  389. }
  390. }
  391. }
  392. if (defType2==2)
  393. {
  394. if (TopMargin>0)
  395. {
  396. for (int i=0;i<TopMargin;i++)
  397. {
  398. for (int j=0;j<FullWidth+add;j++)
  399. ((char*)(ret->pixels))[ftcp++]='\0';
  400. }
  401. }
  402. for (int i=0;i<SpriteHeight;i++)
  403. {
  404. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  405. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  406. }
  407. BaseOffset = BaseOffsetor+RWEntries[0];
  408. for (int i=0;i<SpriteHeight;i++)
  409. {
  410. //BaseOffset = BaseOffsetor+RWEntries[i];
  411. if (LeftMargin>0)
  412. {
  413. for (int j=0;j<LeftMargin;j++)
  414. ((char*)(ret->pixels))[ftcp++]='\0';
  415. }
  416. TotalRowLength=0;
  417. do
  418. {
  419. SegmentType=FDef[BaseOffset++];
  420. unsigned char code = SegmentType / 32;
  421. unsigned char value = (SegmentType & 31) + 1;
  422. if(code==7)
  423. {
  424. for(int h=0; h<value; ++h)
  425. {
  426. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  427. }
  428. }
  429. else
  430. {
  431. for(int h=0; h<value; ++h)
  432. {
  433. ((char*)(ret->pixels))[ftcp++]=code;
  434. }
  435. }
  436. TotalRowLength+=value;
  437. } while(TotalRowLength<SpriteWidth);
  438. if (RightMargin>0)
  439. {
  440. for (int j=0;j<RightMargin;j++)
  441. ((char*)(ret->pixels))[ftcp++]='\0';
  442. }
  443. if (add>0)
  444. {
  445. for (int j=0;j<add+RowAdd;j++)
  446. ((char*)(ret->pixels))[ftcp++]='\0';
  447. }
  448. }
  449. if (BottomMargin>0)
  450. {
  451. for (int i=0;i<BottomMargin;i++)
  452. {
  453. for (int j=0;j<FullWidth+add;j++)
  454. ((char*)(ret->pixels))[ftcp++]='\0';
  455. }
  456. }
  457. }
  458. if (defType2==3)
  459. {
  460. if (add==4)
  461. add=0;
  462. if (TopMargin>0)
  463. {
  464. for (int i=0;i<TopMargin;i++)
  465. {
  466. for (int j=0;j<FullWidth+add;j++)
  467. ((char*)(ret->pixels))[ftcp++]='\0';
  468. }
  469. }
  470. for (int i=0;i<SpriteHeight;i++)
  471. {
  472. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  473. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  474. }
  475. for (int i=0;i<SpriteHeight;i++)
  476. {
  477. BaseOffset = BaseOffsetor+RWEntries[i];
  478. if (LeftMargin>0)
  479. {
  480. for (int j=0;j<LeftMargin;j++)
  481. ((char*)(ret->pixels))[ftcp++]='\0';
  482. }
  483. TotalRowLength=0;
  484. do
  485. {
  486. SegmentType=FDef[BaseOffset++];
  487. unsigned char code = SegmentType / 32;
  488. unsigned char value = (SegmentType & 31) + 1;
  489. if(code==7)
  490. {
  491. for(int h=0; h<value; ++h)
  492. {
  493. if(h<-LeftMargin)
  494. continue;
  495. if(h+TotalRowLength>=SpriteWidth)
  496. break;
  497. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  498. }
  499. }
  500. else
  501. {
  502. for(int h=0; h<value; ++h)
  503. {
  504. if(h<-LeftMargin)
  505. continue;
  506. if(h+TotalRowLength>=SpriteWidth)
  507. break;
  508. ((char*)(ret->pixels))[ftcp++]=code;
  509. }
  510. }
  511. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  512. }while(TotalRowLength<SpriteWidth);
  513. if (RightMargin>0)
  514. {
  515. for (int j=0;j<RightMargin;j++)
  516. ((char*)(ret->pixels))[ftcp++]='\0';
  517. }
  518. if (add>0)
  519. {
  520. for (int j=0;j<add+RowAdd;j++)
  521. ((char*)(ret->pixels))[ftcp++]='\0';
  522. }
  523. }
  524. if (BottomMargin>0)
  525. {
  526. for (int i=0;i<BottomMargin;i++)
  527. {
  528. for (int j=0;j<FullWidth+add;j++)
  529. ((char*)(ret->pixels))[ftcp++]='\0';
  530. }
  531. }
  532. }
  533. SDL_Color ttcol = ret->format->palette->colors[0];
  534. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  535. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  536. return ret;
  537. };
  538. CDefEssential * CDefHandler::essentialize()
  539. {
  540. CDefEssential * ret = new CDefEssential;
  541. ret->ourImages = ourImages;
  542. notFreeImgs = true;
  543. return ret;
  544. }
  545. CDefHandler * CDefHandler::giveDef(std::string defName)
  546. {
  547. unsigned char * data = spriteh->giveFile(defName);
  548. if(!data)
  549. throw "bad def name!";
  550. CDefHandler * nh = new CDefHandler();
  551. nh->openFromMemory(data, defName);
  552. nh->alphaTransformed = false;
  553. delete [] data;
  554. return nh;
  555. }
  556. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  557. {
  558. CDefEssential * ret;
  559. CDefHandler * temp = giveDef(defName);
  560. ret = temp->essentialize();
  561. delete temp;
  562. return ret;
  563. }