CDefHandler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. void CDefHandler::openDef(std::string name)
  32. {
  33. int i,j, totalInBlock;
  34. char Buffer[13];
  35. defName=name;
  36. int andame;
  37. std::ifstream * is = new std::ifstream();
  38. is -> open(name.c_str(),std::ios::binary);
  39. is->seekg(0,std::ios::end); // na koniec
  40. andame = is->tellg(); // read length
  41. is->seekg(0,std::ios::beg); // wracamy na poczatek
  42. FDef = new unsigned char[andame]; // allocate memory
  43. is->read((char*)FDef, andame); // read map file to buffer
  44. is->close();
  45. delete is;
  46. i = 0;
  47. DEFType = readNormalNr(i,4); i+=4;
  48. fullWidth = readNormalNr(i,4); i+=4;
  49. fullHeight = readNormalNr(i,4); i+=4;
  50. i=0xc;
  51. totalBlocks = readNormalNr(i,4); i+=4;
  52. i=0x10;
  53. for (int it=0;it<256;it++)
  54. {
  55. palette[it].R = FDef[i++];
  56. palette[it].G = FDef[i++];
  57. palette[it].B = FDef[i++];
  58. palette[it].F = 0;
  59. }
  60. i=0x310;
  61. totalEntries=0;
  62. for (int z=0; z<totalBlocks; z++)
  63. {
  64. i+=4;
  65. totalInBlock = readNormalNr(i,4); i+=4;
  66. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  67. SEntries.push_back(SEntry());
  68. i+=8;
  69. for (j=0; j<totalInBlock; j++)
  70. {
  71. for (int k=0;k<13;k++) Buffer[k]=FDef[i+k];
  72. i+=13;
  73. SEntries[totalEntries+j].name=Buffer;
  74. }
  75. for (j=0; j<totalInBlock; j++)
  76. {
  77. SEntries[totalEntries+j].offset = readNormalNr(i,4);
  78. i+=4;
  79. }
  80. totalEntries+=totalInBlock;
  81. }
  82. for(j=0; j<SEntries.size(); ++j)
  83. {
  84. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  85. }
  86. /*for (j=0;j<totalEntries;j++)
  87. {
  88. for (int k=0; k<totalEntries-1;k++)
  89. {
  90. if (SEntries[k].offset > SEntries[k+1].offset)
  91. {
  92. int temp=SEntries[k].offset;
  93. SEntries[k].offset = SEntries[k+1].offset;
  94. SEntries[k+1].offset = temp;
  95. std::string temp2 = SEntries[k].name;
  96. SEntries[k].name = SEntries[k+1].name;
  97. SEntries[k+1].name = temp2;
  98. }
  99. }
  100. }*/ //po kiego grzyba to sortowanie tu? tylko psuje kolejność
  101. for(int i=0; i<SEntries.size(); ++i)
  102. {
  103. Cimage nimg;
  104. nimg.bitmap = getSprite(i);
  105. nimg.imName = SEntries[i].name;
  106. ourImages.push_back(nimg);
  107. }
  108. }
  109. unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
  110. {
  111. long long amp = pow((float)256,bytCon-1);
  112. unsigned char * ret = new unsigned char[bytCon];
  113. for (int i=bytCon-1; i>=0;i--)
  114. {
  115. int test2 = nr/(amp);
  116. ret[i]=test2;
  117. nr -= (nr/(amp))*amp;
  118. amp/=256;
  119. }
  120. return ret;
  121. }
  122. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  123. {
  124. BL = (N & 0xE0) >> 5;
  125. BR = N & 0x1F;
  126. }
  127. int CDefHandler::readNormalNr (int pos, int bytCon, unsigned char * str, bool cyclic)
  128. {
  129. int ret=0;
  130. int amp=1;
  131. if (str)
  132. {
  133. for (int i=0; i<bytCon; i++)
  134. {
  135. ret+=str[pos+i]*amp;
  136. amp*=256;
  137. }
  138. }
  139. else
  140. {
  141. for (int i=0; i<bytCon; i++)
  142. {
  143. ret+=FDef[pos+i]*amp;
  144. amp*=256;
  145. }
  146. }
  147. if(cyclic && bytCon<4 && ret>=amp/2)
  148. {
  149. ret = ret-amp;
  150. }
  151. return ret;
  152. }
  153. void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
  154. {
  155. unsigned char * temp = writeNormalNr(nr,bytcon);
  156. for (int i=0;i<bytcon;i++)
  157. stream << char(temp[i]);
  158. }
  159. SDL_Surface * CDefHandler::getSprite (int SIndex) //procedure GetSprite(SIndex: LongInt; var SOut: PStream);
  160. {
  161. //std::ostringstream BMP;
  162. //std::ofstream BMP("testtt.bmp");
  163. std::ofstream BMP;
  164. BMP.open("testtt.bmp", std::ios::binary);
  165. long BaseOffset,
  166. SpriteWidth, SpriteHeight, //format sprite'a
  167. LeftMargin, RightMargin, TopMargin,BottomMargin, //Отступы от края полного изображения
  168. i, add, FullHeight,FullWidth,
  169. TotalRowLength, // dlugosc przeczytanego segmentu
  170. NextSpriteOffset, RowAdd;
  171. std::ifstream Fdef;
  172. unsigned char SegmentType, SegmentLength, BL, BR;
  173. unsigned char * TempDef; //memory
  174. std::string FTemp;
  175. BaseOffset=SEntries[SIndex].offset;//Запоминаем начальное смещение спрайта
  176. i=BaseOffset+4;
  177. int defType2 = readNormalNr(i,4,FDef);i+=4;
  178. FullWidth = readNormalNr(i,4,FDef);i+=4;
  179. FullHeight = readNormalNr(i,4,FDef);i+=4;
  180. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  181. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  182. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  183. TopMargin = readNormalNr(i,4,FDef);i+=4;
  184. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  185. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  186. BMPHeader tb;
  187. tb.x = FullWidth;
  188. tb.y = FullHeight;
  189. tb.dataSize2 = tb.dataSize1 = tb.x*tb.y;
  190. tb.fullSize = tb.dataSize1+436;
  191. tb._h3=tb.fullSize-36;
  192. add = (int)(4*(((float)1) - ((int)(((int)((float)FullWidth/(float)4))-((float)FullWidth/(float)4)))));
  193. /*if (add==4)
  194. add=0;*/ //moved to defcompression dependent block
  195. BMP << "BM";
  196. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  197. print(BMP,tb.fullSize,4);
  198. BMP << '\0' << '\0' << '\0' << '\0';
  199. print(BMP,0x436,4);
  200. print(BMP,0x28,4);
  201. print(BMP,tb.x,4); //w
  202. print(BMP,tb.y,4); //h
  203. print(BMP,1,2); //layers
  204. print(BMP,8,2);
  205. print(BMP,0,4);
  206. print(BMP,0,4);
  207. print(BMP,tb.dataSize1,4);
  208. print(BMP,tb.dataSize2,4);
  209. print(BMP,0,4);
  210. print(BMP,0,4);
  211. int BaseOffsetor= BaseOffset = i;
  212. for (int i=0;i<256;i++)
  213. {
  214. print(BMP,palette[i].B,1);
  215. print(BMP,palette[i].G,1);
  216. print(BMP,palette[i].R,1);
  217. print(BMP,palette[i].F,1);
  218. }
  219. for (int i=0;i<800;i++)
  220. fbuffer[i]=0;
  221. BMP << std::flush;
  222. if (defType2==0)
  223. {
  224. if (add==4)
  225. add=0;
  226. if (TopMargin>0)
  227. {
  228. for (int i=0;i<TopMargin;i++)
  229. {
  230. for (int j=0;j<FullWidth+add;j++)
  231. FTemp+=fbuffer[j];
  232. }
  233. }
  234. for (int i=0;i<SpriteHeight;i++)
  235. {
  236. if (LeftMargin>0)
  237. {
  238. for (int j=0;j<LeftMargin;j++)
  239. FTemp+=fbuffer[j];
  240. }
  241. for (int j=0; j<SpriteWidth;j++)
  242. FTemp+=FDef[BaseOffset++];
  243. if (RightMargin>0)
  244. {
  245. for (int j=0;j<add;j++)
  246. FTemp+=fbuffer[j];
  247. }
  248. }
  249. if (BottomMargin>0)
  250. {
  251. for (int i=0;i<BottomMargin;i++)
  252. {
  253. for (int j=0;j<FullWidth+add;j++)
  254. FTemp+=fbuffer[j];
  255. }
  256. }
  257. }
  258. if (defType2==1)
  259. {
  260. if (add==4)
  261. add=2;
  262. if (TopMargin>0)
  263. {
  264. for (int i=0;i<TopMargin;i++)
  265. {
  266. for (int j=0;j<FullWidth+add;j++)
  267. FTemp+=fbuffer[j];
  268. }
  269. }
  270. RLEntries = new int[SpriteHeight];
  271. for (int i=0;i<SpriteHeight;i++)
  272. {
  273. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  274. }
  275. for (int i=0;i<SpriteHeight;i++)
  276. {
  277. BaseOffset=BaseOffsetor+RLEntries[i];
  278. if (LeftMargin>0)
  279. {
  280. for (int j=0;j<LeftMargin;j++)
  281. FTemp+=fbuffer[j];
  282. }
  283. TotalRowLength=0;
  284. do
  285. {
  286. SegmentType=FDef[BaseOffset++];
  287. SegmentLength=FDef[BaseOffset++];
  288. if (SegmentType==0xFF)
  289. {
  290. for (int k=0;k<=SegmentLength;k++)
  291. {
  292. FTemp+=FDef[BaseOffset+k];
  293. if ((TotalRowLength+k+1)>=SpriteWidth)
  294. break;
  295. }
  296. TotalRowLength+=SegmentLength+1;
  297. }
  298. else
  299. {
  300. for (int k=0;k<SegmentLength+1;k++)
  301. {
  302. //FTemp+=FDef[BaseOffset+k];//
  303. FTemp+='\0';
  304. }
  305. TotalRowLength+=SegmentLength+1;
  306. }
  307. }while(TotalRowLength<SpriteWidth);
  308. RowAdd=SpriteWidth-TotalRowLength;
  309. if (RightMargin>0)
  310. {
  311. for (int j=0;j<RightMargin;j++)
  312. FTemp+=fbuffer[j];
  313. }
  314. if (add>0)
  315. {
  316. for (int j=0;j<add+RowAdd;j++)
  317. FTemp+=fbuffer[j];
  318. }
  319. }
  320. if (BottomMargin>0)
  321. {
  322. for (int i=0;i<BottomMargin;i++)
  323. {
  324. for (int j=0;j<FullWidth+add;j++)
  325. FTemp+=fbuffer[j];
  326. }
  327. }
  328. }
  329. if (defType2==3)
  330. {
  331. if (add==4)
  332. add=0;
  333. if (TopMargin>0)
  334. {
  335. for (int i=0;i<TopMargin;i++)
  336. {
  337. for (int j=0;j<FullWidth+add;j++)
  338. FTemp+=fbuffer[j];
  339. }
  340. }
  341. RWEntries = new unsigned int[SpriteHeight];
  342. for (int i=0;i<SpriteHeight;i++)
  343. {
  344. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  345. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  346. }
  347. for (int i=0;i<SpriteHeight;i++)
  348. {
  349. BaseOffset = BaseOffsetor+RWEntries[i];
  350. if (LeftMargin>0)
  351. {
  352. for (int j=0;j<LeftMargin;j++)
  353. FTemp+=fbuffer[j];
  354. }
  355. TotalRowLength=0;
  356. do
  357. {
  358. SegmentType=FDef[BaseOffset++];
  359. expand(SegmentType,BL,BR);
  360. if (BL==0) //transparent
  361. {
  362. for (int k=0;k<100;k++)
  363. fbuffer[k]='\0';
  364. for (int k=0;k<BR+1;k++)
  365. {
  366. FTemp+=fbuffer[k];
  367. }
  368. TotalRowLength=TotalRowLength+BR+1;
  369. }
  370. if (BL==1) //shadow
  371. {
  372. for (int k=0;k<100;k++)
  373. fbuffer[k]='\1';
  374. for (int k=0;k<BR+1;k++)
  375. {
  376. FTemp+=fbuffer[k];
  377. }
  378. TotalRowLength=TotalRowLength+BR+1;
  379. }
  380. if (BL==2) //shadow
  381. {
  382. for (int k=0;k<100;k++)
  383. fbuffer[k]='\2';
  384. for (int k=0;k<BR+1;k++)
  385. {
  386. FTemp+=fbuffer[k];
  387. }
  388. TotalRowLength=TotalRowLength+BR+1;
  389. }
  390. if (BL==3) //shadow
  391. {
  392. for (int k=0;k<100;k++)
  393. fbuffer[k]='\3';
  394. for (int k=0;k<BR+1;k++)
  395. {
  396. FTemp+=fbuffer[k];
  397. }
  398. TotalRowLength=TotalRowLength+BR+1;
  399. }
  400. if (BL==4) //shadow
  401. {
  402. for (int k=0;k<100;k++)
  403. fbuffer[k]='\4';
  404. for (int k=0;k<BR+1;k++)
  405. {
  406. FTemp+=fbuffer[k];
  407. }
  408. TotalRowLength=TotalRowLength+BR+1;
  409. }
  410. if (BL==5) //team colour?
  411. {
  412. for (int k=0;k<100;k++)
  413. fbuffer[k]='\5';
  414. for (int k=0;k<BR+1;k++)
  415. {
  416. FTemp+=fbuffer[k];
  417. }
  418. TotalRowLength=TotalRowLength+BR+1;
  419. }
  420. if (BL==7) //opaque
  421. {
  422. for (int k=0;k<BR+1;k++)
  423. {
  424. FTemp+=FDef[BaseOffset++];
  425. }
  426. TotalRowLength=TotalRowLength+BR+1;
  427. }
  428. }while(TotalRowLength<SpriteWidth);
  429. if (RightMargin>0)
  430. {
  431. for (int j=0;j<RightMargin;j++)
  432. FTemp+=fbuffer[j];
  433. }
  434. if (add>0)
  435. {
  436. for (int j=0;j<add+RowAdd;j++)
  437. FTemp+=fbuffer[j];
  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. FTemp+=fbuffer[j];
  446. }
  447. }
  448. }
  449. for (int i=1;i<=FullHeight;i++)
  450. {
  451. int at = (FullHeight-i)*(FullWidth+add);
  452. for (int j=0;j<FullWidth+add;j++)
  453. {
  454. BMP << FTemp[at+j];
  455. }
  456. }
  457. BMP.close();
  458. return IMG_Load("testtt.bmp");
  459. };