CDefHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "../stdafx.h"
  2. #include "SDL.h"
  3. #include "CDefHandler.h"
  4. #include <sstream>
  5. #include "CLodHandler.h"
  6. #include "../lib/VCMI_Lib.h"
  7. /*
  8. * CDefHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. static long long pow(long long a, int b)
  17. {
  18. if (!b) return 1;
  19. long c = a;
  20. while (--b)
  21. a*=c;
  22. return a;
  23. }
  24. CDefHandler::CDefHandler()
  25. {
  26. //FDef = NULL;
  27. notFreeImgs = false;
  28. }
  29. CDefHandler::~CDefHandler()
  30. {
  31. //if (FDef)
  32. //delete [] FDef;
  33. if (notFreeImgs)
  34. return;
  35. for (size_t i=0; i<ourImages.size(); ++i)
  36. {
  37. if (ourImages[i].bitmap)
  38. {
  39. SDL_FreeSurface(ourImages[i].bitmap);
  40. ourImages[i].bitmap=NULL;
  41. }
  42. }
  43. }
  44. CDefEssential::~CDefEssential()
  45. {
  46. for(size_t i=0; i < ourImages.size(); ++i)
  47. SDL_FreeSurface(ourImages[i].bitmap);
  48. }
  49. // Note: this function is unused
  50. void CDefHandler::openDef(std::string name)
  51. {
  52. int andame;
  53. std::ifstream * is = new std::ifstream();
  54. is -> open(name.c_str(),std::ios::binary);
  55. is->seekg(0,std::ios::end); // na koniec
  56. andame = is->tellg(); // read length
  57. is->seekg(0,std::ios::beg); // wracamy na poczatek
  58. unsigned char * FDef = new unsigned char[andame]; // allocate memory
  59. is->read((char*)FDef, andame); // read map file to buffer
  60. is->close();
  61. delete is;
  62. openFromMemory(FDef, name);
  63. delete [] FDef;
  64. }
  65. void CDefHandler::openFromMemory(unsigned char *table, std::string name)
  66. {
  67. BMPPalette palette[256];
  68. struct defEntry &de = *(struct defEntry *)table;
  69. unsigned char *p;
  70. defName = name;
  71. DEFType = SDL_SwapLE32(de.DEFType);
  72. width = SDL_SwapLE32(de.width);
  73. height = SDL_SwapLE32(de.height);
  74. totalBlocks = SDL_SwapLE32(de.totalBlocks);
  75. for (unsigned int it=0;it<256;it++)
  76. {
  77. palette[it].R = de.palette[it].R;
  78. palette[it].G = de.palette[it].G;
  79. palette[it].B = de.palette[it].B;
  80. palette[it].F = 0;
  81. }
  82. p = (unsigned char *)de.blocks;
  83. totalEntries=0;
  84. for (unsigned int z=0; z<totalBlocks; z++)
  85. {
  86. struct defEntryBlock &block = *(struct defEntryBlock *)p;
  87. unsigned int totalInBlock;
  88. totalInBlock = SDL_SwapLE32(block.totalInBlock);
  89. for (unsigned int j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  90. SEntries.push_back(SEntry());
  91. p = block.data;
  92. for (unsigned int j=0; j<totalInBlock; j++)
  93. {
  94. char Buffer[13];
  95. memcpy(Buffer, p, 12);
  96. Buffer[12] = 0;
  97. SEntries[totalEntries+j].name=Buffer;
  98. p += 13;
  99. }
  100. for (unsigned int j=0; j<totalInBlock; j++)
  101. {
  102. SEntries[totalEntries+j].offset = SDL_SwapLE32(*(Uint32 *)p);
  103. p += 4;
  104. }
  105. //totalEntries+=totalInBlock;
  106. for(unsigned int hh=0; hh<totalInBlock; ++hh)
  107. {
  108. SEntries[totalEntries].group = z;
  109. ++totalEntries;
  110. }
  111. }
  112. for(unsigned int j=0; j<SEntries.size(); ++j)
  113. {
  114. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  115. }
  116. //RWEntries = new unsigned int[height];
  117. for(unsigned int i=0; i < SEntries.size(); ++i)
  118. {
  119. Cimage nimg;
  120. nimg.bitmap = getSprite(i, table, palette);
  121. nimg.imName = SEntries[i].name;
  122. nimg.groupNumber = SEntries[i].group;
  123. ourImages.push_back(nimg);
  124. }
  125. }
  126. unsigned char * CDefHandler::writeNormalNr (int nr, int bytCon)
  127. {
  128. //int tralalalatoniedziala = 2*9+100-4*bytCon;
  129. //unsigned char * ret = new unsigned char[bytCon];
  130. unsigned char * ret = NULL;
  131. for(int jj=0; jj<100; ++jj)
  132. {
  133. ret = (unsigned char*)calloc(1, bytCon);
  134. if(ret!=NULL)
  135. break;
  136. }
  137. long long amp = pow((long long int)256,bytCon-1);
  138. for (int i=bytCon-1; i>=0;i--)
  139. {
  140. int test2 = nr/(amp);
  141. ret[i]=test2;
  142. nr -= (nr/(amp))*amp;
  143. amp/=256;
  144. }
  145. return ret;
  146. }
  147. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  148. {
  149. BL = (N & 0xE0) >> 5;
  150. BR = N & 0x1F;
  151. }
  152. int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
  153. {
  154. int ret=0;
  155. int amp=1;
  156. if (str)
  157. {
  158. for (int i=0; i<bytCon; i++)
  159. {
  160. ret+=str[pos+i]*amp;
  161. amp*=256;
  162. }
  163. }
  164. //else
  165. //{
  166. // for (int i=0; i<bytCon; i++)
  167. // {
  168. // ret+=FDef[pos+i]*amp;
  169. // amp*=256;
  170. // }
  171. //}
  172. if(cyclic && bytCon<4 && ret>=amp/2)
  173. {
  174. ret = ret-amp;
  175. }
  176. return ret;
  177. }
  178. void CDefHandler::print (std::ostream & stream, int nr, int bytcon)
  179. {
  180. unsigned char * temp = writeNormalNr(nr,bytcon);
  181. for (int i=0;i<bytcon;i++)
  182. stream << char(temp[i]);
  183. free(temp);
  184. }
  185. SDL_Surface * CDefHandler::getSprite (int SIndex, unsigned char * FDef, BMPPalette * palette)
  186. {
  187. SDL_Surface * ret=NULL;
  188. unsigned int BaseOffset,
  189. SpriteWidth, SpriteHeight, //format sprite'a
  190. TotalRowLength, // dlugosc przeczytanego segmentu
  191. add, FullHeight,FullWidth,
  192. RowAdd, //, NextSpriteOffset; //TODO use me
  193. prSize,
  194. defType2;
  195. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  196. unsigned char SegmentType;//, BL, BR; //TODO use me
  197. BaseOffset=SEntries[SIndex].offset;
  198. struct spriteDef sd = *(struct spriteDef *)(FDef + BaseOffset);
  199. prSize = SDL_SwapLE32(sd.prSize); //TODO use me
  200. defType2 = SDL_SwapLE32(sd.defType2);
  201. FullWidth = SDL_SwapLE32(sd.FullWidth);
  202. FullHeight = SDL_SwapLE32(sd.FullHeight);
  203. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  204. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  205. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  206. TopMargin = SDL_SwapLE32(sd.TopMargin);
  207. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  208. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  209. //if(LeftMargin + RightMargin < 0)
  210. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  211. if(LeftMargin<0)
  212. SpriteWidth+=LeftMargin;
  213. if(RightMargin<0)
  214. SpriteWidth+=RightMargin;
  215. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  216. add = 4 - FullWidth%4;
  217. if (add==4)
  218. add=0;
  219. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  220. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  221. BaseOffset += sizeof(struct spriteDef);
  222. int BaseOffsetor = BaseOffset;
  223. for(int i=0; i<256; ++i)
  224. {
  225. SDL_Color pr;
  226. pr.r = palette[i].R;
  227. pr.g = palette[i].G;
  228. pr.b = palette[i].B;
  229. pr.unused = palette[i].F;
  230. (*(ret->format->palette->colors+i))=pr;
  231. }
  232. int ftcp=0;
  233. // If there's a margin anywhere, just blank out the whole surface.
  234. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  235. memset(((char *)ret->pixels), 0, FullHeight*FullWidth);
  236. }
  237. // Skip top margin
  238. if (TopMargin > 0)
  239. ftcp += TopMargin*(FullWidth+add);
  240. switch(defType2)
  241. {
  242. case 0:
  243. {
  244. for (unsigned int i=0;i<SpriteHeight;i++)
  245. {
  246. if (LeftMargin>0)
  247. ftcp += LeftMargin;
  248. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  249. ftcp += SpriteWidth;
  250. BaseOffset += SpriteWidth;
  251. if (RightMargin>0)
  252. ftcp += RightMargin;
  253. }
  254. }
  255. break;
  256. case 1:
  257. {
  258. unsigned int * RWEntriesLoc = (unsigned int *)(FDef+BaseOffset);
  259. BaseOffset += sizeof(int) * SpriteHeight;
  260. for (unsigned int i=0;i<SpriteHeight;i++)
  261. {
  262. BaseOffset=BaseOffsetor+RWEntriesLoc[i];
  263. if (LeftMargin>0)
  264. ftcp += LeftMargin;
  265. TotalRowLength=0;
  266. do
  267. {
  268. unsigned int SegmentLength;
  269. SegmentType=FDef[BaseOffset++];
  270. SegmentLength=FDef[BaseOffset++] + 1;
  271. if (SegmentType==0xFF)
  272. {
  273. for (unsigned int k=0; k<SegmentLength;k++)
  274. {
  275. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  276. if ((TotalRowLength+k)>=SpriteWidth)
  277. break;
  278. }
  279. BaseOffset+=SegmentLength;
  280. TotalRowLength+=SegmentLength;
  281. }
  282. else
  283. {
  284. memset((char*)(ret->pixels)+ftcp, SegmentType, SegmentLength);
  285. ftcp += SegmentLength;
  286. TotalRowLength += SegmentLength;
  287. }
  288. }while(TotalRowLength<SpriteWidth);
  289. RowAdd=SpriteWidth-TotalRowLength;
  290. if (RightMargin>0)
  291. ftcp += RightMargin;
  292. if (add>0)
  293. ftcp += add+RowAdd;
  294. }
  295. }
  296. break;
  297. case 2:
  298. {
  299. /*for (int i=0;i<SpriteHeight;i++)
  300. {
  301. RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
  302. }*/
  303. BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor ); //was + RWEntries[0];
  304. for (unsigned int i=0;i<SpriteHeight;i++)
  305. {
  306. //BaseOffset = BaseOffsetor+RWEntries[i];
  307. if (LeftMargin>0)
  308. ftcp += LeftMargin;
  309. TotalRowLength=0;
  310. do
  311. {
  312. SegmentType=FDef[BaseOffset++];
  313. unsigned char code = SegmentType / 32;
  314. unsigned char value = (SegmentType & 31) + 1;
  315. if(code==7)
  316. {
  317. memcpy((char*)(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  318. ftcp += value;
  319. BaseOffset += value;
  320. }
  321. else
  322. {
  323. memset((char*)(ret->pixels)+ftcp, code, value);
  324. ftcp += value;
  325. }
  326. TotalRowLength+=value;
  327. } while(TotalRowLength<SpriteWidth);
  328. if (RightMargin>0)
  329. ftcp += RightMargin;
  330. RowAdd=SpriteWidth-TotalRowLength;
  331. if (add>0)
  332. ftcp += add+RowAdd;
  333. }
  334. }
  335. break;
  336. case 3:
  337. {
  338. /*for (int i=0;i<SpriteHeight;i++)
  339. {
  340. RWEntries[i] = readNormalNr(BaseOffsetor+i*2*(SpriteWidth/32), 2, FDef);
  341. }*/
  342. for (unsigned int i=0;i<SpriteHeight;i++)
  343. {
  344. BaseOffset = BaseOffsetor + *(unsigned short*)( FDef + BaseOffsetor+i*2*(SpriteWidth/32) ); //was + RWEntries[i] before speedup
  345. if (LeftMargin>0)
  346. ftcp += LeftMargin;
  347. TotalRowLength=0;
  348. do
  349. {
  350. SegmentType=FDef[BaseOffset++];
  351. unsigned char code = SegmentType / 32;
  352. unsigned char value = (SegmentType & 31) + 1;
  353. if(code==7)
  354. {
  355. for(int h=0; h<value; ++h)
  356. {
  357. if(h<-LeftMargin)
  358. continue;
  359. if(h+TotalRowLength>=SpriteWidth)
  360. break;
  361. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  362. }
  363. }
  364. else
  365. {
  366. for(int h=0; h<value; ++h)
  367. {
  368. if(h<-LeftMargin)
  369. continue;
  370. if(h+TotalRowLength>=SpriteWidth)
  371. break;
  372. ((char*)(ret->pixels))[ftcp++]=code;
  373. }
  374. }
  375. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  376. }while(TotalRowLength<SpriteWidth);
  377. if (RightMargin>0)
  378. ftcp += RightMargin;
  379. RowAdd=SpriteWidth-TotalRowLength;
  380. if (add>0)
  381. ftcp += add+RowAdd;
  382. }
  383. }
  384. break;
  385. default:
  386. throw std::string("Unknown sprite format.");
  387. break;
  388. }
  389. SDL_Color ttcol = ret->format->palette->colors[0];
  390. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  391. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  392. return ret;
  393. };
  394. CDefEssential * CDefHandler::essentialize()
  395. {
  396. CDefEssential * ret = new CDefEssential;
  397. ret->ourImages = ourImages;
  398. notFreeImgs = true;
  399. return ret;
  400. }
  401. CDefHandler * CDefHandler::giveDef(std::string defName)
  402. {
  403. unsigned char * data = spriteh->giveFile(defName);
  404. if(!data)
  405. throw "bad def name!";
  406. CDefHandler * nh = new CDefHandler();
  407. nh->openFromMemory(data, defName);
  408. nh->alphaTransformed = false;
  409. delete [] data;
  410. return nh;
  411. }
  412. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  413. {
  414. CDefEssential * ret;
  415. CDefHandler * temp = giveDef(defName);
  416. ret = temp->essentialize();
  417. delete temp;
  418. return ret;
  419. }