CDefHandler.cpp 21 KB

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