CDefHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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;
  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 (TopMargin>0)
  213. {
  214. for (int i=0;i<TopMargin;i++)
  215. {
  216. for (int j=0;j<FullWidth+add;j++)
  217. FTemp+=fbuffer[j];
  218. }
  219. }
  220. for (int i=0;i<SpriteHeight;i++)
  221. {
  222. if (LeftMargin>0)
  223. {
  224. for (int j=0;j<LeftMargin;j++)
  225. FTemp+=fbuffer[j];
  226. }
  227. for (int j=0; j<SpriteWidth;j++)
  228. FTemp+=FDef[BaseOffset++];
  229. if (RightMargin>0)
  230. {
  231. for (int j=0;j<add;j++)
  232. FTemp+=fbuffer[j];
  233. }
  234. }
  235. if (BottomMargin>0)
  236. {
  237. for (int i=0;i<BottomMargin;i++)
  238. {
  239. for (int j=0;j<FullWidth+add;j++)
  240. FTemp+=fbuffer[j];
  241. }
  242. }
  243. }
  244. if (defType2==1)
  245. {
  246. if (TopMargin>0)
  247. {
  248. for (int i=0;i<TopMargin;i++)
  249. {
  250. for (int j=0;j<FullWidth+add;j++)
  251. FTemp+=fbuffer[j];
  252. }
  253. }
  254. RLEntries = new int[SpriteHeight];
  255. for (int i=0;i<SpriteHeight;i++)
  256. {
  257. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  258. }
  259. for (int i=0;i<SpriteHeight;i++)
  260. {
  261. BaseOffset=BaseOffsetor+RLEntries[i];
  262. if (LeftMargin>0)
  263. {
  264. for (int j=0;j<LeftMargin;j++)
  265. FTemp+=fbuffer[j];
  266. }
  267. TotalRowLength=0;
  268. do
  269. {
  270. SegmentType=FDef[BaseOffset++];
  271. SegmentLength=FDef[BaseOffset++];
  272. if (SegmentType==0xFF)
  273. {
  274. for (int k=0;k<=SegmentLength;k++)
  275. {
  276. FTemp+=FDef[BaseOffset+k];
  277. if ((TotalRowLength+k+1)>=SpriteWidth)
  278. break;
  279. }
  280. TotalRowLength+=SegmentLength+1;
  281. }
  282. else
  283. {
  284. for (int k=0;k<SegmentLength+1;k++)
  285. {
  286. //FTemp+=FDef[BaseOffset+k];//
  287. FTemp+='\0';
  288. }
  289. TotalRowLength+=SegmentLength+1;
  290. }
  291. }while(TotalRowLength<SpriteWidth);
  292. RowAdd=SpriteWidth-TotalRowLength;
  293. if (RightMargin>0)
  294. {
  295. for (int j=0;j<RightMargin;j++)
  296. FTemp+=fbuffer[j];
  297. }
  298. if (add>0)
  299. {
  300. for (int j=0;j<add+RowAdd;j++)
  301. FTemp+=fbuffer[j];
  302. }
  303. }
  304. if (BottomMargin>0)
  305. {
  306. for (int i=0;i<BottomMargin;i++)
  307. {
  308. for (int j=0;j<FullWidth+add;j++)
  309. FTemp+=fbuffer[j];
  310. }
  311. }
  312. }
  313. if (defType2==3)
  314. {
  315. if (TopMargin>0)
  316. {
  317. for (int i=0;i<TopMargin;i++)
  318. {
  319. for (int j=0;j<FullWidth+add;j++)
  320. FTemp+=fbuffer[j];
  321. }
  322. }
  323. RWEntries = new unsigned int[SpriteHeight];
  324. for (int i=0;i<SpriteHeight;i++)
  325. {
  326. BaseOffset=BaseOffsetor+i*2*(SpriteWidth/32);
  327. RWEntries[i] = readNormalNr(BaseOffset,2,FDef);
  328. }
  329. for (int i=0;i<SpriteHeight;i++)
  330. {
  331. BaseOffset = BaseOffsetor+RWEntries[i];
  332. if (LeftMargin>0)
  333. {
  334. for (int j=0;j<LeftMargin;j++)
  335. FTemp+=fbuffer[j];
  336. }
  337. TotalRowLength=0;
  338. do
  339. {
  340. SegmentType=FDef[BaseOffset++];
  341. expand(SegmentType,BL,BR);
  342. if (BL==0)
  343. {
  344. for (int k=0;k<100;k++)
  345. fbuffer[k]='\0';
  346. for (int k=0;k<BR+1;k++)
  347. {
  348. FTemp+=fbuffer[k];
  349. }
  350. TotalRowLength=TotalRowLength+BR+1;
  351. }
  352. if (BL==1)
  353. {
  354. for (int k=0;k<100;k++)
  355. fbuffer[k]='\0';
  356. for (int k=0;k<BR+1;k++)
  357. {
  358. FTemp+=fbuffer[k];
  359. }
  360. TotalRowLength=TotalRowLength+BR+1;
  361. }
  362. if (BL==4)
  363. {
  364. for (int k=0;k<100;k++)
  365. fbuffer[k]='\0';
  366. for (int k=0;k<BR+1;k++)
  367. {
  368. FTemp+=fbuffer[k];
  369. }
  370. TotalRowLength=TotalRowLength+BR+1;
  371. }
  372. if (BL==5)
  373. {
  374. for (int k=0;k<100;k++)
  375. fbuffer[k]='\0';
  376. for (int k=0;k<BR+1;k++)
  377. {
  378. FTemp+=fbuffer[k];
  379. }
  380. TotalRowLength=TotalRowLength+BR+1;
  381. }
  382. if (BL==7)
  383. {
  384. for (int k=0;k<BR+1;k++)
  385. {
  386. FTemp+=FDef[BaseOffset++];
  387. }
  388. TotalRowLength=TotalRowLength+BR+1;
  389. }
  390. }while(TotalRowLength<SpriteWidth);
  391. if (RightMargin>0)
  392. {
  393. for (int j=0;j<RightMargin;j++)
  394. FTemp+=fbuffer[j];
  395. }
  396. if (add>0)
  397. {
  398. for (int j=0;j<add+RowAdd;j++)
  399. FTemp+=fbuffer[j];
  400. }
  401. }
  402. if (BottomMargin>0)
  403. {
  404. for (int i=0;i<BottomMargin;i++)
  405. {
  406. for (int j=0;j<FullWidth+add;j++)
  407. FTemp+=fbuffer[j];
  408. }
  409. }
  410. }
  411. for (int i=1;i<=FullHeight;i++)
  412. {
  413. int at = (FullHeight-i)*(FullWidth+add);
  414. for (int j=0;j<FullWidth+add;j++)
  415. {
  416. BMP << FTemp[at+j];
  417. }
  418. }
  419. // for i:=1 to FullHeight do
  420. // begin
  421. // FTemp.Seek((FullHeight-i)*(FullWidth+Add),spBegin);
  422. // Stream2Stream(SOut,FTemp,FullWidth+Add);
  423. // end;
  424. // SOut.Seek(0,spBegin);
  425. BMP.close();
  426. };