CDefHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. long BaseOffset,
  152. SpriteWidth, SpriteHeight, //format sprite'a
  153. LeftMargin, RightMargin, TopMargin,BottomMargin, //Отступы от края полного изображения
  154. i, add, FullHeight,FullWidth,
  155. TotalRowLength, // dlugosc przeczytanego segmentu
  156. NextSpriteOffset, RowAdd;
  157. std::ifstream Fdef;
  158. unsigned char SegmentType, SegmentLength, BL, BR;
  159. unsigned char * TempDef; //memory
  160. std::string FTemp;
  161. BaseOffset=SEntries[SIndex].offset;//Запоминаем начальное смещение спрайта
  162. i=BaseOffset+4;
  163. int defType2 = readNormalNr(i,4,FDef);i+=4;
  164. FullWidth = readNormalNr(i,4,FDef);i+=4;
  165. FullHeight = readNormalNr(i,4,FDef);i+=4;
  166. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  167. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  168. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  169. TopMargin = readNormalNr(i,4,FDef);i+=4;
  170. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  171. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  172. BMPHeader tb;
  173. tb.x = FullWidth;
  174. tb.y = FullHeight;
  175. tb.dataSize2 = tb.dataSize1 = tb.x*tb.y;
  176. tb.fullSize = tb.dataSize1+436;
  177. tb._h3=tb.fullSize-36;
  178. add = (int)(4*(((float)1) - ((int)(((int)((float)FullWidth/(float)4))-((float)FullWidth/(float)4)))));
  179. if (add==4)
  180. add=0;
  181. BMP << "BM";
  182. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  183. print(BMP,tb.fullSize,4);
  184. BMP << '\0' << '\0' << '\0' << '\0';
  185. print(BMP,0x436,4);
  186. print(BMP,0x28,4);
  187. print(BMP,tb.x,4); //w
  188. print(BMP,tb.y,4); //h
  189. print(BMP,1,2); //layers
  190. print(BMP,8,2);
  191. print(BMP,0,4);
  192. print(BMP,0,4);
  193. print(BMP,tb.dataSize1,4);
  194. print(BMP,tb.dataSize2,4);
  195. print(BMP,0,4);
  196. print(BMP,0,4);
  197. int BaseOffsetor= BaseOffset = i;
  198. for (int i=0;i<256;i++)
  199. {
  200. print(BMP,palette[i].B,1);
  201. print(BMP,palette[i].G,1);
  202. print(BMP,palette[i].R,1);
  203. print(BMP,palette[i].F,1);
  204. }
  205. for (int i=0;i<800;i++)
  206. fbuffer[i]=0;
  207. BMP << std::flush;
  208. if (defType2==0)
  209. {
  210. if (TopMargin>0)
  211. {
  212. for (int i=0;i<TopMargin;i++)
  213. {
  214. for (int j=0;j<FullWidth+add;j++)
  215. FTemp+=fbuffer[j];
  216. }
  217. }
  218. for (int i=0;i<SpriteHeight;i++)
  219. {
  220. if (LeftMargin>0)
  221. {
  222. for (int j=0;j<LeftMargin;j++)
  223. FTemp+=fbuffer[j];
  224. }
  225. for (int j=0; j<SpriteWidth;j++)
  226. FTemp+=FDef[BaseOffset++];
  227. if (RightMargin>0)
  228. {
  229. for (int j=0;j<add;j++)
  230. FTemp+=fbuffer[j];
  231. }
  232. }
  233. if (BottomMargin>0)
  234. {
  235. for (int i=0;i<BottomMargin;i++)
  236. {
  237. for (int j=0;j<FullWidth+add;j++)
  238. FTemp+=fbuffer[j];
  239. }
  240. }
  241. }
  242. if (defType2==1)
  243. {
  244. if (TopMargin>0)
  245. {
  246. for (int i=0;i<TopMargin;i++)
  247. {
  248. for (int j=0;j<FullWidth+add;j++)
  249. FTemp+=fbuffer[j];
  250. }
  251. }
  252. RLEntries = new int[SpriteHeight];
  253. for (int i=0;i<SpriteHeight;i++)
  254. {
  255. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  256. }
  257. for (int i=0;i<SpriteHeight;i++)
  258. {
  259. BaseOffset=BaseOffsetor+RLEntries[i];
  260. if (LeftMargin>0)
  261. {
  262. for (int j=0;j<LeftMargin;j++)
  263. FTemp+=fbuffer[j];
  264. }
  265. TotalRowLength=0;
  266. do
  267. {
  268. SegmentType=FDef[BaseOffset++];
  269. SegmentLength=FDef[BaseOffset++];
  270. if (SegmentType==0xFF)
  271. {
  272. for (int k=0;k<=SegmentLength;k++)
  273. {
  274. FTemp+=FDef[BaseOffset+k];
  275. if ((TotalRowLength+k+1)>=SpriteWidth)
  276. break;
  277. }
  278. TotalRowLength+=SegmentLength+1;
  279. }
  280. else
  281. {
  282. for (int k=0;k<SegmentLength+1;k++)
  283. {
  284. //FTemp+=FDef[BaseOffset+k];//
  285. FTemp+='\0';
  286. }
  287. TotalRowLength+=SegmentLength+1;
  288. }
  289. }while(TotalRowLength<SpriteWidth);
  290. RowAdd=SpriteWidth-TotalRowLength;
  291. if (RightMargin>0)
  292. {
  293. for (int j=0;j<RightMargin;j++)
  294. FTemp+=fbuffer[j];
  295. }
  296. if (add>0)
  297. {
  298. for (int j=0;j<add+RowAdd;j++)
  299. FTemp+=fbuffer[j];
  300. }
  301. }
  302. if (BottomMargin>0)
  303. {
  304. for (int i=0;i<BottomMargin;i++)
  305. {
  306. for (int j=0;j<FullWidth+add;j++)
  307. FTemp+=fbuffer[j];
  308. }
  309. }
  310. }
  311. if (defType2==3)
  312. {
  313. if (TopMargin>0)
  314. {
  315. for (int i=0;i<TopMargin;i++)
  316. {
  317. for (int j=0;j<FullWidth+add;j++)
  318. FTemp+=fbuffer[j];
  319. }
  320. }
  321. RWEntries = new unsigned int[SpriteHeight];
  322. for (int i=0;i<SpriteHeight;i++)
  323. {
  324. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  325. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  326. }
  327. for (int i=0;i<SpriteHeight;i++)
  328. {
  329. BaseOffset = BaseOffsetor+RWEntries[i];
  330. if (LeftMargin>0)
  331. {
  332. for (int j=0;j<LeftMargin;j++)
  333. FTemp+=fbuffer[j];
  334. }
  335. TotalRowLength=0;
  336. do
  337. {
  338. SegmentType=FDef[BaseOffset++];
  339. expand(SegmentType,BL,BR);
  340. if (BL==0)
  341. {
  342. for (int k=0;k<100;k++)
  343. fbuffer[k]='\0';
  344. for (int k=0;k<BR+1;k++)
  345. {
  346. FTemp+=fbuffer[k];
  347. }
  348. TotalRowLength=TotalRowLength+BR+1;
  349. }
  350. if (BL==1)
  351. {
  352. for (int k=0;k<100;k++)
  353. fbuffer[k]='\0';
  354. for (int k=0;k<BR+1;k++)
  355. {
  356. FTemp+=fbuffer[k];
  357. }
  358. TotalRowLength=TotalRowLength+BR+1;
  359. }
  360. if (BL==4)
  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==5)
  371. {
  372. for (int k=0;k<100;k++)
  373. fbuffer[k]='\0';
  374. for (int k=0;k<BR+1;k++)
  375. {
  376. FTemp+=fbuffer[k];
  377. }
  378. TotalRowLength=TotalRowLength+BR+1;
  379. }
  380. if (BL==7)
  381. {
  382. for (int k=0;k<BR+1;k++)
  383. {
  384. FTemp+=FDef[BaseOffset++];
  385. }
  386. TotalRowLength=TotalRowLength+BR+1;
  387. }
  388. }while(TotalRowLength<SpriteWidth);
  389. if (RightMargin>0)
  390. {
  391. for (int j=0;j<RightMargin;j++)
  392. FTemp+=fbuffer[j];
  393. }
  394. if (add>0)
  395. {
  396. for (int j=0;j<add+RowAdd;j++)
  397. FTemp+=fbuffer[j];
  398. }
  399. }
  400. if (BottomMargin>0)
  401. {
  402. for (int i=0;i<BottomMargin;i++)
  403. {
  404. for (int j=0;j<FullWidth+add;j++)
  405. FTemp+=fbuffer[j];
  406. }
  407. }
  408. }
  409. for (int i=1;i<=FullHeight;i++)
  410. {
  411. int at = (FullHeight-i)*(FullWidth+add);
  412. for (int j=0;j<FullWidth+add;j++)
  413. {
  414. BMP << FTemp[at+j];
  415. }
  416. }
  417. // for i:=1 to FullHeight do
  418. // begin
  419. // FTemp.Seek((FullHeight-i)*(FullWidth+Add),spBegin);
  420. // Stream2Stream(SOut,FTemp,FullWidth+Add);
  421. // end;
  422. // SOut.Seek(0,spBegin);
  423. BMP.close();
  424. };