CDefHandler.cpp 13 KB

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