CDefHandler.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #include "../stdafx.h"
  2. #include "CDefHandler.h"
  3. #include "SDL_image.h"
  4. #include <sstream>
  5. long long pow(long long a, int b)
  6. {
  7. if (!b) return 1;
  8. long c = a;
  9. while (--b)
  10. a*=c;
  11. return a;
  12. }
  13. void BMPHeader::print(std::ostream & out)
  14. {
  15. CDefHandler::print(out,fullSize,4);
  16. CDefHandler::print(out,_h1,4);
  17. CDefHandler::print(out,_c1,4);
  18. CDefHandler::print(out,_c2,4);
  19. CDefHandler::print(out,x,4);
  20. CDefHandler::print(out,y,4);
  21. CDefHandler::print(out,_c3,2);
  22. CDefHandler::print(out,_c4,2);
  23. CDefHandler::print(out,_h2,4);
  24. CDefHandler::print(out,_h3,4);
  25. CDefHandler::print(out,dataSize1,4);
  26. CDefHandler::print(out,dataSize2,4);
  27. for (int i=0;i<8;i++)
  28. out << _c5[i];
  29. out.flush();
  30. }
  31. CDefHandler::CDefHandler()
  32. {
  33. //FDef = NULL;
  34. RWEntries = NULL;
  35. RLEntries = NULL;
  36. notFreeImgs = false;
  37. }
  38. CDefHandler::~CDefHandler()
  39. {
  40. //if (FDef)
  41. //delete [] FDef;
  42. if (RWEntries)
  43. delete [] RWEntries;
  44. if (RLEntries)
  45. delete [] RLEntries;
  46. if (notFreeImgs)
  47. return;
  48. for (int i=0; i<ourImages.size(); i++)
  49. {
  50. if (ourImages[i].bitmap)
  51. {
  52. SDL_FreeSurface(ourImages[i].bitmap);
  53. ourImages[i].bitmap=NULL;
  54. }
  55. }
  56. }
  57. CDefEssential::~CDefEssential()
  58. {
  59. for(int i=0;i<ourImages.size();i++)
  60. SDL_FreeSurface(ourImages[i].bitmap);
  61. }
  62. void CDefHandler::openDef(std::string name)
  63. {
  64. int i,j, totalInBlock;
  65. char Buffer[13];
  66. defName=name;
  67. int andame;
  68. std::ifstream * is = new std::ifstream();
  69. is -> open(name.c_str(),std::ios::binary);
  70. is->seekg(0,std::ios::end); // na koniec
  71. andame = is->tellg(); // read length
  72. is->seekg(0,std::ios::beg); // wracamy na poczatek
  73. unsigned char * FDef = new unsigned char[andame]; // allocate memory
  74. is->read((char*)FDef, andame); // read map file to buffer
  75. is->close();
  76. delete is;
  77. i = 0;
  78. DEFType = readNormalNr(i,4,FDef); i+=4;
  79. fullWidth = readNormalNr(i,4,FDef); i+=4;
  80. fullHeight = readNormalNr(i,4,FDef); i+=4;
  81. i=0xc;
  82. totalBlocks = readNormalNr(i,4,FDef); i+=4;
  83. i=0x10;
  84. for (int it=0;it<256;it++)
  85. {
  86. palette[it].R = FDef[i++];
  87. palette[it].G = FDef[i++];
  88. palette[it].B = FDef[i++];
  89. palette[it].F = 0;
  90. }
  91. i=0x310;
  92. totalEntries=0;
  93. for (int z=0; z<totalBlocks; z++)
  94. {
  95. i+=4;
  96. totalInBlock = readNormalNr(i,4,FDef); i+=4;
  97. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  98. SEntries.push_back(SEntry());
  99. i+=8;
  100. for (j=0; j<totalInBlock; j++)
  101. {
  102. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  103. i+=13;
  104. SEntries[totalEntries+j].name=Buffer;
  105. }
  106. for (j=0; j<totalInBlock; j++)
  107. {
  108. SEntries[totalEntries+j].offset = readNormalNr(i,4,FDef);
  109. i+=4;
  110. }
  111. //totalEntries+=totalInBlock;
  112. for(int hh=0; hh<totalInBlock; ++hh)
  113. {
  114. SEntries[totalEntries].group = z;
  115. ++totalEntries;
  116. }
  117. }
  118. for(j=0; j<SEntries.size(); ++j)
  119. {
  120. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  121. }
  122. for(int i=0; i<SEntries.size(); ++i)
  123. {
  124. Cimage nimg;
  125. nimg.bitmap = getSprite(i, FDef);
  126. nimg.imName = SEntries[i].name;
  127. nimg.groupNumber = SEntries[i].group;
  128. ourImages.push_back(nimg);
  129. }
  130. delete [] FDef;
  131. FDef = NULL;
  132. }
  133. void CDefHandler::openFromMemory(unsigned char *table, int size, std::string name)
  134. {
  135. int i,j, totalInBlock;
  136. char Buffer[13];
  137. defName=name;
  138. int andame = size;
  139. i = 0;
  140. DEFType = readNormalNr(i,4,table); i+=4;
  141. fullWidth = readNormalNr(i,4,table); i+=4;
  142. fullHeight = readNormalNr(i,4,table); i+=4;
  143. i=0xc;
  144. totalBlocks = readNormalNr(i,4,table); i+=4;
  145. i=0x10;
  146. for (int it=0;it<256;it++)
  147. {
  148. palette[it].R = table[i++];
  149. palette[it].G = table[i++];
  150. palette[it].B = table[i++];
  151. palette[it].F = 0;
  152. }
  153. i=0x310;
  154. totalEntries=0;
  155. for (int z=0; z<totalBlocks; z++)
  156. {
  157. int unknown1 = readNormalNr(i,4,table); i+=4;
  158. totalInBlock = readNormalNr(i,4,table); i+=4;
  159. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  160. SEntries.push_back(SEntry());
  161. int unknown2 = readNormalNr(i,4,table); i+=4;
  162. int unknown3 = readNormalNr(i,4,table); i+=4;
  163. for (j=0; j<totalInBlock; j++)
  164. {
  165. for (int k=0;k<13;k++) Buffer[k]=table[i+k];
  166. i+=13;
  167. SEntries[totalEntries+j].name=Buffer;
  168. }
  169. for (j=0; j<totalInBlock; j++)
  170. {
  171. SEntries[totalEntries+j].offset = readNormalNr(i,4,table);
  172. int unknown4 = readNormalNr(i,4,table); i+=4;
  173. }
  174. //totalEntries+=totalInBlock;
  175. for(int hh=0; hh<totalInBlock; ++hh)
  176. {
  177. SEntries[totalEntries].group = z;
  178. ++totalEntries;
  179. }
  180. }
  181. for(j=0; j<SEntries.size(); ++j)
  182. {
  183. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  184. }
  185. for(int i=0; i<SEntries.size(); ++i)
  186. {
  187. Cimage nimg;
  188. nimg.bitmap = getSprite(i, table);
  189. nimg.imName = SEntries[i].name;
  190. nimg.groupNumber = SEntries[i].group;
  191. ourImages.push_back(nimg);
  192. }
  193. }
  194. unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
  195. {
  196. //int tralalalatoniedziala = 2*9+100-4*bytCon;
  197. //unsigned char * ret = new unsigned char[bytCon];
  198. unsigned char * ret = NULL;
  199. for(int jj=0; jj<100; ++jj)
  200. {
  201. ret = (unsigned char*)calloc(1, bytCon);
  202. if(ret!=NULL)
  203. break;
  204. }
  205. long long amp = pow((float)256,bytCon-1);
  206. for (int i=bytCon-1; i>=0;i--)
  207. {
  208. int test2 = nr/(amp);
  209. ret[i]=test2;
  210. nr -= (nr/(amp))*amp;
  211. amp/=256;
  212. }
  213. return ret;
  214. }
  215. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  216. {
  217. BL = (N & 0xE0) >> 5;
  218. BR = N & 0x1F;
  219. }
  220. int CDefHandler::readNormalNr (int pos, int bytCon, unsigned char * str, bool cyclic)
  221. {
  222. int ret=0;
  223. int amp=1;
  224. if (str)
  225. {
  226. for (int i=0; i<bytCon; i++)
  227. {
  228. ret+=str[pos+i]*amp;
  229. amp*=256;
  230. }
  231. }
  232. //else
  233. //{
  234. // for (int i=0; i<bytCon; i++)
  235. // {
  236. // ret+=FDef[pos+i]*amp;
  237. // amp*=256;
  238. // }
  239. //}
  240. if(cyclic && bytCon<4 && ret>=amp/2)
  241. {
  242. ret = ret-amp;
  243. }
  244. return ret;
  245. }
  246. void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
  247. {
  248. unsigned char * temp = writeNormalNr(nr,bytcon);
  249. for (int i=0;i<bytcon;i++)
  250. stream << char(temp[i]);
  251. free(temp);
  252. }
  253. SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef)
  254. {
  255. SDL_Surface * ret=NULL;
  256. long BaseOffset,
  257. SpriteWidth, SpriteHeight, //format sprite'a
  258. LeftMargin, RightMargin, TopMargin,BottomMargin,
  259. i, add, FullHeight,FullWidth,
  260. TotalRowLength, // dlugosc przeczytanego segmentu
  261. NextSpriteOffset, RowAdd;
  262. unsigned char SegmentType, SegmentLength, BL, BR;
  263. i=BaseOffset=SEntries[SIndex].offset;
  264. int prSize=readNormalNr(i,4,FDef);i+=4;
  265. int defType2 = readNormalNr(i,4,FDef);i+=4;
  266. FullWidth = readNormalNr(i,4,FDef);i+=4;
  267. FullHeight = readNormalNr(i,4,FDef);i+=4;
  268. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  269. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  270. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  271. TopMargin = readNormalNr(i,4,FDef);i+=4;
  272. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  273. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  274. //if(LeftMargin + RightMargin < 0)
  275. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  276. if(LeftMargin<0)
  277. SpriteWidth+=LeftMargin;
  278. if(RightMargin<0)
  279. SpriteWidth+=RightMargin;
  280. add = 4 - FullWidth%4;
  281. if (add==4)
  282. add=0;
  283. int ftcp=0;
  284. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  285. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  286. int BaseOffsetor = BaseOffset = i;
  287. for(int i=0; i<256; ++i)
  288. {
  289. SDL_Color pr;
  290. pr.r = palette[i].R;
  291. pr.g = palette[i].G;
  292. pr.b = palette[i].B;
  293. pr.unused = palette[i].F;
  294. (*(ret->format->palette->colors+i))=pr;
  295. }
  296. if (defType2==0)
  297. {
  298. if (TopMargin>0)
  299. {
  300. for (int i=0;i<TopMargin;i++)
  301. {
  302. for (int j=0;j<FullWidth+add;j++)
  303. ((char*)(ret->pixels))[ftcp++]='\0';
  304. }
  305. }
  306. for (int i=0;i<SpriteHeight;i++)
  307. {
  308. if (LeftMargin>0)
  309. {
  310. for (int j=0;j<LeftMargin;j++)
  311. ((char*)(ret->pixels))[ftcp++]='\0';
  312. }
  313. for (int j=0; j<SpriteWidth;j++)
  314. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  315. if (RightMargin>0)
  316. {
  317. for (int j=0;j<add;j++)
  318. ((char*)(ret->pixels))[ftcp++]='\0';
  319. }
  320. }
  321. if (BottomMargin>0)
  322. {
  323. for (int i=0;i<BottomMargin;i++)
  324. {
  325. for (int j=0;j<FullWidth+add;j++)
  326. ((char*)(ret->pixels))[ftcp++]='\0';
  327. }
  328. }
  329. }
  330. if (defType2==1)
  331. {
  332. if (TopMargin>0)
  333. {
  334. for (int i=0;i<TopMargin;i++)
  335. {
  336. for (int j=0;j<FullWidth+add;j++)
  337. ((char*)(ret->pixels))[ftcp++]='\0';
  338. }
  339. }
  340. RLEntries = new int[SpriteHeight];
  341. for (int i=0;i<SpriteHeight;i++)
  342. {
  343. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  344. }
  345. for (int i=0;i<SpriteHeight;i++)
  346. {
  347. BaseOffset=BaseOffsetor+RLEntries[i];
  348. if (LeftMargin>0)
  349. {
  350. for (int j=0;j<LeftMargin;j++)
  351. ((char*)(ret->pixels))[ftcp++]='\0';
  352. }
  353. TotalRowLength=0;
  354. do
  355. {
  356. SegmentType=FDef[BaseOffset++];
  357. SegmentLength=FDef[BaseOffset++];
  358. if (SegmentType==0xFF)
  359. {
  360. for (int k=0;k<=SegmentLength;k++)
  361. {
  362. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  363. if ((TotalRowLength+k+1)>=SpriteWidth)
  364. break;
  365. }
  366. BaseOffset+=SegmentLength+1;////
  367. TotalRowLength+=SegmentLength+1;
  368. }
  369. else
  370. {
  371. for (int k=0;k<SegmentLength+1;k++)
  372. {
  373. ((char*)(ret->pixels))[ftcp++]=SegmentType;//
  374. //((char*)(ret->pixels))+='\0';
  375. }
  376. TotalRowLength+=SegmentLength+1;
  377. }
  378. }while(TotalRowLength<SpriteWidth);
  379. RowAdd=SpriteWidth-TotalRowLength;
  380. if (RightMargin>0)
  381. {
  382. for (int j=0;j<RightMargin;j++)
  383. ((char*)(ret->pixels))[ftcp++]='\0';
  384. }
  385. if (add>0)
  386. {
  387. for (int j=0;j<add+RowAdd;j++)
  388. ((char*)(ret->pixels))[ftcp++]='\0';
  389. }
  390. }
  391. delete [] RLEntries;
  392. RLEntries = NULL;
  393. if (BottomMargin>0)
  394. {
  395. for (int i=0;i<BottomMargin;i++)
  396. {
  397. for (int j=0;j<FullWidth+add;j++)
  398. ((char*)(ret->pixels))[ftcp++]='\0';
  399. }
  400. }
  401. }
  402. if (defType2==2)
  403. {
  404. if (TopMargin>0)
  405. {
  406. for (int i=0;i<TopMargin;i++)
  407. {
  408. for (int j=0;j<FullWidth+add;j++)
  409. ((char*)(ret->pixels))[ftcp++]='\0';
  410. }
  411. }
  412. RWEntries = new unsigned int[SpriteHeight];
  413. for (int i=0;i<SpriteHeight;i++)
  414. {
  415. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  416. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  417. }
  418. BaseOffset = BaseOffsetor+RWEntries[0];
  419. for (int i=0;i<SpriteHeight;i++)
  420. {
  421. //BaseOffset = BaseOffsetor+RWEntries[i];
  422. if (LeftMargin>0)
  423. {
  424. for (int j=0;j<LeftMargin;j++)
  425. ((char*)(ret->pixels))[ftcp++]='\0';
  426. }
  427. TotalRowLength=0;
  428. do
  429. {
  430. SegmentType=FDef[BaseOffset++];
  431. unsigned char code = SegmentType / 32;
  432. unsigned char value = (SegmentType & 31) + 1;
  433. if(code==7)
  434. {
  435. for(int h=0; h<value; ++h)
  436. {
  437. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  438. }
  439. }
  440. else
  441. {
  442. for(int h=0; h<value; ++h)
  443. {
  444. ((char*)(ret->pixels))[ftcp++]=code;
  445. }
  446. }
  447. TotalRowLength+=value;
  448. } while(TotalRowLength<SpriteWidth);
  449. if (RightMargin>0)
  450. {
  451. for (int j=0;j<RightMargin;j++)
  452. ((char*)(ret->pixels))[ftcp++]='\0';
  453. }
  454. if (add>0)
  455. {
  456. for (int j=0;j<add+RowAdd;j++)
  457. ((char*)(ret->pixels))[ftcp++]='\0';
  458. }
  459. }
  460. delete [] RWEntries;
  461. RWEntries = NULL;
  462. if (BottomMargin>0)
  463. {
  464. for (int i=0;i<BottomMargin;i++)
  465. {
  466. for (int j=0;j<FullWidth+add;j++)
  467. ((char*)(ret->pixels))[ftcp++]='\0';
  468. }
  469. }
  470. }
  471. if (defType2==3)
  472. {
  473. if (add==4)
  474. add=0;
  475. if (TopMargin>0)
  476. {
  477. for (int i=0;i<TopMargin;i++)
  478. {
  479. for (int j=0;j<FullWidth+add;j++)
  480. ((char*)(ret->pixels))[ftcp++]='\0';
  481. }
  482. }
  483. RWEntries = new unsigned int[SpriteHeight];
  484. for (int i=0;i<SpriteHeight;i++)
  485. {
  486. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  487. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  488. }
  489. for (int i=0;i<SpriteHeight;i++)
  490. {
  491. BaseOffset = BaseOffsetor+RWEntries[i];
  492. if (LeftMargin>0)
  493. {
  494. for (int j=0;j<LeftMargin;j++)
  495. ((char*)(ret->pixels))[ftcp++]='\0';
  496. }
  497. TotalRowLength=0;
  498. do
  499. {
  500. SegmentType=FDef[BaseOffset++];
  501. unsigned char code = SegmentType / 32;
  502. unsigned char value = (SegmentType & 31) + 1;
  503. if(code==7)
  504. {
  505. for(int h=0; h<value; ++h)
  506. {
  507. if(h<-LeftMargin)
  508. continue;
  509. if(h+TotalRowLength>=SpriteWidth)
  510. break;
  511. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  512. }
  513. }
  514. else
  515. {
  516. for(int h=0; h<value; ++h)
  517. {
  518. if(h<-LeftMargin)
  519. continue;
  520. if(h+TotalRowLength>=SpriteWidth)
  521. break;
  522. ((char*)(ret->pixels))[ftcp++]=code;
  523. }
  524. }
  525. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  526. }while(TotalRowLength<SpriteWidth);
  527. if (RightMargin>0)
  528. {
  529. for (int j=0;j<RightMargin;j++)
  530. ((char*)(ret->pixels))[ftcp++]='\0';
  531. }
  532. if (add>0)
  533. {
  534. for (int j=0;j<add+RowAdd;j++)
  535. ((char*)(ret->pixels))[ftcp++]='\0';
  536. }
  537. }
  538. delete [] RWEntries;
  539. RWEntries=NULL;
  540. if (BottomMargin>0)
  541. {
  542. for (int i=0;i<BottomMargin;i++)
  543. {
  544. for (int j=0;j<FullWidth+add;j++)
  545. ((char*)(ret->pixels))[ftcp++]='\0';
  546. }
  547. }
  548. }
  549. SDL_Color ttcol = ret->format->palette->colors[0];
  550. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  551. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  552. return ret;
  553. };
  554. CDefEssential * CDefHandler::essentialize()
  555. {
  556. CDefEssential * ret = new CDefEssential;
  557. ret->ourImages = ourImages;
  558. notFreeImgs = true;
  559. return ret;
  560. }