CDefHandler.cpp 18 KB

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