CDefHandler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 = true;
  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. void CDefHandler::openDef(std::string name)
  58. {
  59. int i,j, totalInBlock;
  60. char Buffer[13];
  61. defName=name;
  62. int andame;
  63. std::ifstream * is = new std::ifstream();
  64. is -> open(name.c_str(),std::ios::binary);
  65. is->seekg(0,std::ios::end); // na koniec
  66. andame = is->tellg(); // read length
  67. is->seekg(0,std::ios::beg); // wracamy na poczatek
  68. FDef = new unsigned char[andame]; // allocate memory
  69. is->read((char*)FDef, andame); // read map file to buffer
  70. is->close();
  71. delete is;
  72. i = 0;
  73. DEFType = readNormalNr(i,4); i+=4;
  74. fullWidth = readNormalNr(i,4); i+=4;
  75. fullHeight = readNormalNr(i,4); i+=4;
  76. i=0xc;
  77. totalBlocks = readNormalNr(i,4); i+=4;
  78. i=0x10;
  79. for (int it=0;it<256;it++)
  80. {
  81. palette[it].R = FDef[i++];
  82. palette[it].G = FDef[i++];
  83. palette[it].B = FDef[i++];
  84. palette[it].F = 0;
  85. }
  86. i=0x310;
  87. totalEntries=0;
  88. for (int z=0; z<totalBlocks; z++)
  89. {
  90. i+=4;
  91. totalInBlock = readNormalNr(i,4); i+=4;
  92. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  93. SEntries.push_back(SEntry());
  94. i+=8;
  95. for (j=0; j<totalInBlock; j++)
  96. {
  97. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  98. i+=13;
  99. SEntries[totalEntries+j].name=Buffer;
  100. }
  101. for (j=0; j<totalInBlock; j++)
  102. {
  103. SEntries[totalEntries+j].offset = readNormalNr(i,4);
  104. i+=4;
  105. }
  106. //totalEntries+=totalInBlock;
  107. for(int hh=0; hh<totalInBlock; ++hh)
  108. {
  109. SEntries[totalEntries].group = z;
  110. ++totalEntries;
  111. }
  112. }
  113. for(j=0; j<SEntries.size(); ++j)
  114. {
  115. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  116. }
  117. for(int i=0; i<SEntries.size(); ++i)
  118. {
  119. Cimage nimg;
  120. nimg.bitmap = getSprite(i);
  121. nimg.imName = SEntries[i].name;
  122. nimg.groupNumber = SEntries[i].group;
  123. ourImages.push_back(nimg);
  124. }
  125. delete FDef;
  126. FDef = NULL;
  127. }
  128. void CDefHandler::openFromMemory(unsigned char *table, int size, std::string name)
  129. {
  130. int i,j, totalInBlock;
  131. char Buffer[13];
  132. defName=name;
  133. FDef = new unsigned char[size]; // allocate memory
  134. for (int i=0;i<size;i++)
  135. FDef[i]=table[i];
  136. int andame = size;
  137. i = 0;
  138. DEFType = readNormalNr(i,4); i+=4;
  139. fullWidth = readNormalNr(i,4); i+=4;
  140. fullHeight = readNormalNr(i,4); i+=4;
  141. i=0xc;
  142. totalBlocks = readNormalNr(i,4); i+=4;
  143. i=0x10;
  144. for (int it=0;it<256;it++)
  145. {
  146. palette[it].R = FDef[i++];
  147. palette[it].G = FDef[i++];
  148. palette[it].B = FDef[i++];
  149. palette[it].F = 0;
  150. }
  151. i=0x310;
  152. totalEntries=0;
  153. for (int z=0; z<totalBlocks; z++)
  154. {
  155. int unknown1 = readNormalNr(i,4); i+=4;
  156. totalInBlock = readNormalNr(i,4); i+=4;
  157. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  158. SEntries.push_back(SEntry());
  159. int unknown2 = readNormalNr(i,4); i+=4;
  160. int unknown3 = readNormalNr(i,4); i+=4;
  161. for (j=0; j<totalInBlock; j++)
  162. {
  163. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  164. i+=13;
  165. SEntries[totalEntries+j].name=Buffer;
  166. }
  167. for (j=0; j<totalInBlock; j++)
  168. {
  169. SEntries[totalEntries+j].offset = readNormalNr(i,4);
  170. int unknown4 = readNormalNr(i,4); i+=4;
  171. }
  172. //totalEntries+=totalInBlock;
  173. for(int hh=0; hh<totalInBlock; ++hh)
  174. {
  175. SEntries[totalEntries].group = z;
  176. ++totalEntries;
  177. }
  178. }
  179. for(j=0; j<SEntries.size(); ++j)
  180. {
  181. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  182. }
  183. for(int i=0; i<SEntries.size(); ++i)
  184. {
  185. Cimage nimg;
  186. nimg.bitmap = getSprite(i);
  187. nimg.imName = SEntries[i].name;
  188. nimg.groupNumber = SEntries[i].group;
  189. ourImages.push_back(nimg);
  190. }
  191. delete FDef;
  192. FDef = NULL;
  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)
  254. {
  255. SDL_Surface * ret;
  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. std::ifstream Fdef;
  263. unsigned char SegmentType, SegmentLength, BL, BR;
  264. unsigned char * TempDef; //memory
  265. std::string FTemp;
  266. i=BaseOffset=SEntries[SIndex].offset;
  267. int prSize=readNormalNr(i,4,FDef);i+=4;
  268. int defType2 = readNormalNr(i,4,FDef);i+=4;
  269. FullWidth = readNormalNr(i,4,FDef);i+=4;
  270. FullHeight = readNormalNr(i,4,FDef);i+=4;
  271. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  272. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  273. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  274. TopMargin = readNormalNr(i,4,FDef);i+=4;
  275. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  276. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  277. BMPHeader tb;
  278. tb.x = FullWidth;
  279. tb.y = FullHeight;
  280. tb.dataSize2 = tb.dataSize1 = tb.x*tb.y;
  281. tb.fullSize = tb.dataSize1+436;
  282. tb._h3=tb.fullSize-36;
  283. //add = (int)(4*(((float)1) - ((int)(((int)((float)FullWidth/(float)4))-((float)FullWidth/(float)4)))));
  284. add = 4 - FullWidth%4;
  285. /*if (add==4)
  286. add=0;*/ //moved to defcompression dependent block
  287. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  288. int rmask = 0xff000000;
  289. int gmask = 0x00ff0000;
  290. int bmask = 0x0000ff00;
  291. int amask = 0x000000ff;
  292. #else
  293. int rmask = 0x000000ff;
  294. int gmask = 0x0000ff00;
  295. int bmask = 0x00ff0000;
  296. int amask = 0xff000000;
  297. #endif
  298. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  299. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  300. int BaseOffsetor = BaseOffset = i;
  301. for(int i=0; i<256; ++i)
  302. {
  303. SDL_Color pr;
  304. pr.r = palette[i].R;
  305. pr.g = palette[i].G;
  306. pr.b = palette[i].B;
  307. pr.unused = palette[i].F;
  308. (*(ret->format->palette->colors+i))=pr;
  309. }
  310. for (int i=0;i<800;i++)
  311. fbuffer[i]=0;
  312. if (defType2==0)
  313. {
  314. if (add==4)
  315. add=0;
  316. if (TopMargin>0)
  317. {
  318. for (int i=0;i<TopMargin;i++)
  319. {
  320. for (int j=0;j<FullWidth+add;j++)
  321. FTemp+=fbuffer[j];
  322. }
  323. }
  324. for (int i=0;i<SpriteHeight;i++)
  325. {
  326. if (LeftMargin>0)
  327. {
  328. for (int j=0;j<LeftMargin;j++)
  329. FTemp+=fbuffer[j];
  330. }
  331. for (int j=0; j<SpriteWidth;j++)
  332. FTemp+=FDef[BaseOffset++];
  333. if (RightMargin>0)
  334. {
  335. for (int j=0;j<add;j++)
  336. FTemp+=fbuffer[j];
  337. }
  338. }
  339. if (BottomMargin>0)
  340. {
  341. for (int i=0;i<BottomMargin;i++)
  342. {
  343. for (int j=0;j<FullWidth+add;j++)
  344. FTemp+=fbuffer[j];
  345. }
  346. }
  347. }
  348. if (defType2==1)
  349. {
  350. if (add==4)
  351. add=0; ////////was 3
  352. if (TopMargin>0)
  353. {
  354. for (int i=0;i<TopMargin;i++)
  355. {
  356. for (int j=0;j<FullWidth+add;j++)
  357. FTemp+=fbuffer[j];
  358. }
  359. }
  360. RLEntries = new int[SpriteHeight];
  361. for (int i=0;i<SpriteHeight;i++)
  362. {
  363. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  364. }
  365. for (int i=0;i<SpriteHeight;i++)
  366. {
  367. BaseOffset=BaseOffsetor+RLEntries[i];
  368. if (LeftMargin>0)
  369. {
  370. for (int j=0;j<LeftMargin;j++)
  371. FTemp+=fbuffer[j];
  372. }
  373. TotalRowLength=0;
  374. do
  375. {
  376. SegmentType=FDef[BaseOffset++];
  377. SegmentLength=FDef[BaseOffset++];
  378. if (SegmentType==0xFF)
  379. {
  380. for (int k=0;k<=SegmentLength;k++)
  381. {
  382. FTemp+=FDef[BaseOffset+k];
  383. if ((TotalRowLength+k+1)>=SpriteWidth)
  384. break;
  385. }
  386. BaseOffset+=SegmentLength+1;////
  387. TotalRowLength+=SegmentLength+1;
  388. }
  389. else
  390. {
  391. for (int k=0;k<SegmentLength+1;k++)
  392. {
  393. FTemp+=SegmentType;//
  394. //FTemp+='\0';
  395. }
  396. TotalRowLength+=SegmentLength+1;
  397. }
  398. }while(TotalRowLength<SpriteWidth);
  399. RowAdd=SpriteWidth-TotalRowLength;
  400. if (RightMargin>0)
  401. {
  402. for (int j=0;j<RightMargin;j++)
  403. FTemp+=fbuffer[j];
  404. }
  405. if (add>0)
  406. {
  407. for (int j=0;j<add+RowAdd;j++)
  408. FTemp+=fbuffer[j];
  409. }
  410. }
  411. delete RLEntries;
  412. RLEntries = NULL;
  413. if (BottomMargin>0)
  414. {
  415. for (int i=0;i<BottomMargin;i++)
  416. {
  417. for (int j=0;j<FullWidth+add;j++)
  418. FTemp+=fbuffer[j];
  419. }
  420. }
  421. }
  422. if (defType2==2)
  423. {
  424. if (add==4)
  425. add=0;
  426. if (TopMargin>0)
  427. {
  428. for (int i=0;i<TopMargin;i++)
  429. {
  430. for (int j=0;j<FullWidth+add;j++)
  431. FTemp+=fbuffer[j];
  432. }
  433. }
  434. RWEntries = new unsigned int[SpriteHeight];
  435. for (int i=0;i<SpriteHeight;i++)
  436. {
  437. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  438. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  439. }
  440. BaseOffset = BaseOffsetor+RWEntries[0];
  441. for (int i=0;i<SpriteHeight;i++)
  442. {
  443. //BaseOffset = BaseOffsetor+RWEntries[i];
  444. if (LeftMargin>0)
  445. {
  446. for (int j=0;j<LeftMargin;j++)
  447. FTemp+=fbuffer[j];
  448. }
  449. TotalRowLength=0;
  450. do
  451. {
  452. SegmentType=FDef[BaseOffset++];
  453. unsigned char code = SegmentType / 32;
  454. unsigned char value = (SegmentType & 31) + 1;
  455. if(code==7)
  456. {
  457. for(int h=0; h<value; ++h)
  458. {
  459. FTemp+=FDef[BaseOffset++];
  460. }
  461. }
  462. else
  463. {
  464. for(int h=0; h<value; ++h)
  465. {
  466. FTemp+=code;
  467. }
  468. }
  469. TotalRowLength+=value;
  470. } while(TotalRowLength<SpriteWidth);
  471. if (RightMargin>0)
  472. {
  473. for (int j=0;j<RightMargin;j++)
  474. FTemp+=fbuffer[j];
  475. }
  476. if (add>0)
  477. {
  478. for (int j=0;j<add+RowAdd;j++)
  479. FTemp+=fbuffer[j];
  480. }
  481. }
  482. delete RWEntries;
  483. RWEntries = NULL;
  484. if (BottomMargin>0)
  485. {
  486. for (int i=0;i<BottomMargin;i++)
  487. {
  488. for (int j=0;j<FullWidth+add;j++)
  489. FTemp+=fbuffer[j];
  490. }
  491. }
  492. }
  493. if (defType2==3)
  494. {
  495. if (add==4)
  496. add=0;
  497. if (TopMargin>0)
  498. {
  499. for (int i=0;i<TopMargin;i++)
  500. {
  501. for (int j=0;j<FullWidth+add;j++)
  502. FTemp+=fbuffer[j];
  503. }
  504. }
  505. RWEntries = new unsigned int[SpriteHeight];
  506. for (int i=0;i<SpriteHeight;i++)
  507. {
  508. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  509. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  510. }
  511. for (int i=0;i<SpriteHeight;i++)
  512. {
  513. BaseOffset = BaseOffsetor+RWEntries[i];
  514. if (LeftMargin>0)
  515. {
  516. for (int j=0;j<LeftMargin;j++)
  517. FTemp+=fbuffer[j];
  518. }
  519. TotalRowLength=0;
  520. do
  521. {
  522. SegmentType=FDef[BaseOffset++];
  523. unsigned char code = SegmentType / 32;
  524. unsigned char value = (SegmentType & 31) + 1;
  525. if(code==7)
  526. {
  527. for(int h=0; h<value; ++h)
  528. {
  529. FTemp+=FDef[BaseOffset++];
  530. }
  531. }
  532. else
  533. {
  534. for(int h=0; h<value; ++h)
  535. {
  536. FTemp+=code;
  537. }
  538. }
  539. TotalRowLength+=value;
  540. }while(TotalRowLength<SpriteWidth);
  541. if (RightMargin>0)
  542. {
  543. for (int j=0;j<RightMargin;j++)
  544. FTemp+=fbuffer[j];
  545. }
  546. if (add>0)
  547. {
  548. for (int j=0;j<add+RowAdd;j++)
  549. FTemp+=fbuffer[j];
  550. }
  551. }
  552. delete RWEntries;
  553. RWEntries=NULL;
  554. if (BottomMargin>0)
  555. {
  556. for (int i=0;i<BottomMargin;i++)
  557. {
  558. for (int j=0;j<FullWidth+add;j++)
  559. FTemp+=fbuffer[j];
  560. }
  561. }
  562. }
  563. for (int i=0; i<FullHeight; ++i)
  564. {
  565. for (int j=0;j<FullWidth+add;j++)
  566. {
  567. *((char*)ret->pixels + ret->format->BytesPerPixel * (i*(fullWidth+add) + j)) = FTemp[i*(FullWidth+add)+j];
  568. }
  569. }
  570. SDL_Color ttcol = ret->format->palette->colors[0];
  571. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  572. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  573. return ret;
  574. };
  575. CDefEssential * CDefHandler::essentialize()
  576. {
  577. CDefEssential * ret = new CDefEssential;
  578. ret->ourImages = ourImages;
  579. notFreeImgs = true;
  580. return ret;
  581. }