CDefHandler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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=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. std::ifstream Fdef;
  263. unsigned char SegmentType, SegmentLength, BL, BR;
  264. i=BaseOffset=SEntries[SIndex].offset;
  265. int prSize=readNormalNr(i,4,FDef);i+=4;
  266. int defType2 = readNormalNr(i,4,FDef);i+=4;
  267. FullWidth = readNormalNr(i,4,FDef);i+=4;
  268. FullHeight = readNormalNr(i,4,FDef);i+=4;
  269. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  270. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  271. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  272. TopMargin = readNormalNr(i,4,FDef);i+=4;
  273. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  274. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  275. BMPHeader tb;
  276. tb.x = FullWidth;
  277. tb.y = FullHeight;
  278. tb.dataSize2 = tb.dataSize1 = tb.x*tb.y;
  279. tb.fullSize = tb.dataSize1+436;
  280. tb._h3=tb.fullSize-36;
  281. //add = (int)(4*(((float)1) - ((int)(((int)((float)FullWidth/(float)4))-((float)FullWidth/(float)4)))));
  282. add = 4 - FullWidth%4;
  283. if (add==4)
  284. add=0;
  285. unsigned char * FTemp = new unsigned char[(tb.x+add)*tb.y];
  286. int ftcp=0;
  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. if (defType2==0)
  311. {
  312. if (TopMargin>0)
  313. {
  314. for (int i=0;i<TopMargin;i++)
  315. {
  316. for (int j=0;j<FullWidth+add;j++)
  317. FTemp[ftcp++]='\0';
  318. }
  319. }
  320. for (int i=0;i<SpriteHeight;i++)
  321. {
  322. if (LeftMargin>0)
  323. {
  324. for (int j=0;j<LeftMargin;j++)
  325. FTemp[ftcp++]='\0';
  326. }
  327. for (int j=0; j<SpriteWidth;j++)
  328. FTemp[ftcp++]=FDef[BaseOffset++];
  329. if (RightMargin>0)
  330. {
  331. for (int j=0;j<add;j++)
  332. FTemp[ftcp++]='\0';
  333. }
  334. }
  335. if (BottomMargin>0)
  336. {
  337. for (int i=0;i<BottomMargin;i++)
  338. {
  339. for (int j=0;j<FullWidth+add;j++)
  340. FTemp[ftcp++]='\0';
  341. }
  342. }
  343. }
  344. if (defType2==1)
  345. {
  346. if (TopMargin>0)
  347. {
  348. for (int i=0;i<TopMargin;i++)
  349. {
  350. for (int j=0;j<FullWidth+add;j++)
  351. FTemp[ftcp++]='\0';
  352. }
  353. }
  354. RLEntries = new int[SpriteHeight];
  355. for (int i=0;i<SpriteHeight;i++)
  356. {
  357. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  358. }
  359. for (int i=0;i<SpriteHeight;i++)
  360. {
  361. BaseOffset=BaseOffsetor+RLEntries[i];
  362. if (LeftMargin>0)
  363. {
  364. for (int j=0;j<LeftMargin;j++)
  365. FTemp[ftcp++]='\0';
  366. }
  367. TotalRowLength=0;
  368. do
  369. {
  370. SegmentType=FDef[BaseOffset++];
  371. SegmentLength=FDef[BaseOffset++];
  372. if (SegmentType==0xFF)
  373. {
  374. for (int k=0;k<=SegmentLength;k++)
  375. {
  376. FTemp[ftcp++]=FDef[BaseOffset+k];
  377. if ((TotalRowLength+k+1)>=SpriteWidth)
  378. break;
  379. }
  380. BaseOffset+=SegmentLength+1;////
  381. TotalRowLength+=SegmentLength+1;
  382. }
  383. else
  384. {
  385. for (int k=0;k<SegmentLength+1;k++)
  386. {
  387. FTemp[ftcp++]=SegmentType;//
  388. //FTemp+='\0';
  389. }
  390. TotalRowLength+=SegmentLength+1;
  391. }
  392. }while(TotalRowLength<SpriteWidth);
  393. RowAdd=SpriteWidth-TotalRowLength;
  394. if (RightMargin>0)
  395. {
  396. for (int j=0;j<RightMargin;j++)
  397. FTemp[ftcp++]='\0';
  398. }
  399. if (add>0)
  400. {
  401. for (int j=0;j<add+RowAdd;j++)
  402. FTemp[ftcp++]='\0';
  403. }
  404. }
  405. delete RLEntries;
  406. RLEntries = NULL;
  407. if (BottomMargin>0)
  408. {
  409. for (int i=0;i<BottomMargin;i++)
  410. {
  411. for (int j=0;j<FullWidth+add;j++)
  412. FTemp[ftcp++]='\0';
  413. }
  414. }
  415. }
  416. if (defType2==2)
  417. {
  418. if (TopMargin>0)
  419. {
  420. for (int i=0;i<TopMargin;i++)
  421. {
  422. for (int j=0;j<FullWidth+add;j++)
  423. FTemp[ftcp++]='\0';
  424. }
  425. }
  426. RWEntries = new unsigned int[SpriteHeight];
  427. for (int i=0;i<SpriteHeight;i++)
  428. {
  429. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  430. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  431. }
  432. BaseOffset = BaseOffsetor+RWEntries[0];
  433. for (int i=0;i<SpriteHeight;i++)
  434. {
  435. //BaseOffset = BaseOffsetor+RWEntries[i];
  436. if (LeftMargin>0)
  437. {
  438. for (int j=0;j<LeftMargin;j++)
  439. FTemp[ftcp++]='\0';
  440. }
  441. TotalRowLength=0;
  442. do
  443. {
  444. SegmentType=FDef[BaseOffset++];
  445. unsigned char code = SegmentType / 32;
  446. unsigned char value = (SegmentType & 31) + 1;
  447. if(code==7)
  448. {
  449. for(int h=0; h<value; ++h)
  450. {
  451. FTemp[ftcp++]=FDef[BaseOffset++];
  452. }
  453. }
  454. else
  455. {
  456. for(int h=0; h<value; ++h)
  457. {
  458. FTemp[ftcp++]=code;
  459. }
  460. }
  461. TotalRowLength+=value;
  462. } while(TotalRowLength<SpriteWidth);
  463. if (RightMargin>0)
  464. {
  465. for (int j=0;j<RightMargin;j++)
  466. FTemp[ftcp++]='\0';
  467. }
  468. if (add>0)
  469. {
  470. for (int j=0;j<add+RowAdd;j++)
  471. FTemp[ftcp++]='\0';
  472. }
  473. }
  474. delete RWEntries;
  475. RWEntries = NULL;
  476. if (BottomMargin>0)
  477. {
  478. for (int i=0;i<BottomMargin;i++)
  479. {
  480. for (int j=0;j<FullWidth+add;j++)
  481. FTemp[ftcp++]='\0';
  482. }
  483. }
  484. }
  485. if (defType2==3)
  486. {
  487. if (add==4)
  488. add=0;
  489. if (TopMargin>0)
  490. {
  491. for (int i=0;i<TopMargin;i++)
  492. {
  493. for (int j=0;j<FullWidth+add;j++)
  494. FTemp[ftcp++]='\0';
  495. }
  496. }
  497. RWEntries = new unsigned int[SpriteHeight];
  498. for (int i=0;i<SpriteHeight;i++)
  499. {
  500. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  501. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  502. }
  503. for (int i=0;i<SpriteHeight;i++)
  504. {
  505. BaseOffset = BaseOffsetor+RWEntries[i];
  506. if (LeftMargin>0)
  507. {
  508. for (int j=0;j<LeftMargin;j++)
  509. FTemp[ftcp++]='\0';
  510. }
  511. TotalRowLength=0;
  512. do
  513. {
  514. SegmentType=FDef[BaseOffset++];
  515. unsigned char code = SegmentType / 32;
  516. unsigned char value = (SegmentType & 31) + 1;
  517. if(code==7)
  518. {
  519. for(int h=0; h<value; ++h)
  520. {
  521. FTemp[ftcp++]=FDef[BaseOffset++];
  522. }
  523. }
  524. else
  525. {
  526. for(int h=0; h<value; ++h)
  527. {
  528. FTemp[ftcp++]=code;
  529. }
  530. }
  531. TotalRowLength+=value;
  532. }while(TotalRowLength<SpriteWidth);
  533. if (RightMargin>0)
  534. {
  535. for (int j=0;j<RightMargin;j++)
  536. FTemp[ftcp++]='\0';
  537. }
  538. if (add>0)
  539. {
  540. for (int j=0;j<add+RowAdd;j++)
  541. FTemp[ftcp++]='\0';
  542. }
  543. }
  544. delete RWEntries;
  545. RWEntries=NULL;
  546. if (BottomMargin>0)
  547. {
  548. for (int i=0;i<BottomMargin;i++)
  549. {
  550. for (int j=0;j<FullWidth+add;j++)
  551. FTemp[ftcp++]='\0';
  552. }
  553. }
  554. }
  555. for (int i=0; i<FullHeight; ++i)
  556. {
  557. for (int j=0;j<FullWidth+add;j++)
  558. {
  559. *((char*)ret->pixels + ret->format->BytesPerPixel * (i*(fullWidth+add) + j)) = FTemp[i*(FullWidth+add)+j];
  560. }
  561. }
  562. SDL_Color ttcol = ret->format->palette->colors[0];
  563. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  564. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  565. delete [] FTemp;
  566. return ret;
  567. };
  568. CDefEssential * CDefHandler::essentialize()
  569. {
  570. CDefEssential * ret = new CDefEssential;
  571. ret->ourImages = ourImages;
  572. notFreeImgs = true;
  573. return ret;
  574. }