CDefHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. SDefEntry &de = * reinterpret_cast<SDefEntry *>(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. unsigned int 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. // The SDefEntryBlock starts just after the SDefEntry
  83. p = reinterpret_cast<unsigned char *>(&de);
  84. p += sizeof(de);
  85. int totalEntries=0;
  86. for (unsigned int z=0; z<totalBlocks; z++)
  87. {
  88. SDefEntryBlock &block = * reinterpret_cast<SDefEntryBlock *>(p);
  89. unsigned int totalInBlock;
  90. totalInBlock = SDL_SwapLE32(read_unaligned_u32(&block.totalInBlock));
  91. for (unsigned int j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  92. SEntries.push_back(SEntry());
  93. p = block.data;
  94. for (unsigned int j=0; j<totalInBlock; j++)
  95. {
  96. char Buffer[13];
  97. memcpy(Buffer, p, 12);
  98. Buffer[12] = 0;
  99. SEntries[totalEntries+j].name=Buffer;
  100. p += 13;
  101. }
  102. for (unsigned int j=0; j<totalInBlock; j++)
  103. {
  104. SEntries[totalEntries+j].offset = SDL_SwapLE32(read_unaligned_u32(p));
  105. p += 4;
  106. }
  107. //totalEntries+=totalInBlock;
  108. for(unsigned int hh=0; hh<totalInBlock; ++hh)
  109. {
  110. SEntries[totalEntries].group = z;
  111. ++totalEntries;
  112. }
  113. }
  114. for(unsigned int j=0; j<SEntries.size(); ++j)
  115. {
  116. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  117. }
  118. //RWEntries = new unsigned int[height];
  119. for(unsigned int i=0; i < SEntries.size(); ++i)
  120. {
  121. Cimage nimg;
  122. nimg.bitmap = getSprite(i, table, palette);
  123. nimg.imName = SEntries[i].name;
  124. nimg.groupNumber = SEntries[i].group;
  125. ourImages.push_back(nimg);
  126. }
  127. }
  128. void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
  129. {
  130. BL = (N & 0xE0) >> 5;
  131. BR = N & 0x1F;
  132. }
  133. SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const
  134. {
  135. SDL_Surface * ret=NULL;
  136. unsigned int BaseOffset,
  137. SpriteWidth, SpriteHeight, //format sprite'a
  138. TotalRowLength, // dlugosc przeczytanego segmentu
  139. add, FullHeight,FullWidth,
  140. RowAdd, //, NextSpriteOffset; //TODO use me
  141. prSize,
  142. defType2;
  143. int LeftMargin, RightMargin, TopMargin, BottomMargin;
  144. unsigned char SegmentType;//, BL, BR; //TODO use me
  145. BaseOffset = SEntries[SIndex].offset;
  146. SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);
  147. prSize = SDL_SwapLE32(sd.prSize); //TODO use me
  148. defType2 = SDL_SwapLE32(sd.defType2);
  149. FullWidth = SDL_SwapLE32(sd.FullWidth);
  150. FullHeight = SDL_SwapLE32(sd.FullHeight);
  151. SpriteWidth = SDL_SwapLE32(sd.SpriteWidth);
  152. SpriteHeight = SDL_SwapLE32(sd.SpriteHeight);
  153. LeftMargin = SDL_SwapLE32(sd.LeftMargin);
  154. TopMargin = SDL_SwapLE32(sd.TopMargin);
  155. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  156. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  157. //if(LeftMargin + RightMargin < 0)
  158. // SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
  159. if(LeftMargin<0)
  160. SpriteWidth+=LeftMargin;
  161. if(RightMargin<0)
  162. SpriteWidth+=RightMargin;
  163. // Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
  164. add = 4 - FullWidth%4;
  165. if (add==4)
  166. add=0;
  167. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
  168. //int tempee2 = readNormalNr(0,4,((unsigned char *)tempee.c_str()));
  169. BaseOffset += sizeof(SSpriteDef);
  170. int BaseOffsetor = BaseOffset;
  171. for(int i=0; i<256; ++i)
  172. {
  173. SDL_Color pr;
  174. pr.r = palette[i].R;
  175. pr.g = palette[i].G;
  176. pr.b = palette[i].B;
  177. pr.unused = palette[i].F;
  178. (*(ret->format->palette->colors+i))=pr;
  179. }
  180. int ftcp=0;
  181. // If there's a margin anywhere, just blank out the whole surface.
  182. if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
  183. memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
  184. }
  185. // Skip top margin
  186. if (TopMargin > 0)
  187. ftcp += TopMargin*(FullWidth+add);
  188. switch(defType2)
  189. {
  190. case 0:
  191. {
  192. for (unsigned int i=0;i<SpriteHeight;i++)
  193. {
  194. if (LeftMargin>0)
  195. ftcp += LeftMargin;
  196. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
  197. ftcp += SpriteWidth;
  198. BaseOffset += SpriteWidth;
  199. if (RightMargin>0)
  200. ftcp += RightMargin;
  201. }
  202. }
  203. break;
  204. case 1:
  205. {
  206. const unsigned int * RWEntriesLoc = reinterpret_cast<const unsigned int *>(FDef+BaseOffset);
  207. BaseOffset += sizeof(int) * SpriteHeight;
  208. for (unsigned int i=0;i<SpriteHeight;i++)
  209. {
  210. BaseOffset=BaseOffsetor + SDL_SwapLE32(read_unaligned_u32(RWEntriesLoc + i));
  211. if (LeftMargin>0)
  212. ftcp += LeftMargin;
  213. TotalRowLength=0;
  214. do
  215. {
  216. unsigned int SegmentLength;
  217. SegmentType=FDef[BaseOffset++];
  218. SegmentLength=FDef[BaseOffset++] + 1;
  219. if (SegmentType==0xFF)
  220. {
  221. for (unsigned int k=0; k<SegmentLength;k++)
  222. {
  223. (reinterpret_cast<char*>(ret->pixels))[ftcp++]=FDef[BaseOffset+k];
  224. if ((TotalRowLength+k)>=SpriteWidth)
  225. break;
  226. }
  227. BaseOffset+=SegmentLength;
  228. TotalRowLength+=SegmentLength;
  229. }
  230. else
  231. {
  232. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
  233. ftcp += SegmentLength;
  234. TotalRowLength += SegmentLength;
  235. }
  236. }while(TotalRowLength<SpriteWidth);
  237. RowAdd=SpriteWidth-TotalRowLength;
  238. if (RightMargin>0)
  239. ftcp += RightMargin;
  240. if (add>0)
  241. ftcp += add+RowAdd;
  242. }
  243. }
  244. break;
  245. case 2:
  246. {
  247. BaseOffset = BaseOffsetor + SDL_SwapLE16(read_unaligned_u16(FDef + BaseOffsetor));
  248. for (unsigned int i=0;i<SpriteHeight;i++)
  249. {
  250. //BaseOffset = BaseOffsetor+RWEntries[i];
  251. if (LeftMargin>0)
  252. ftcp += LeftMargin;
  253. TotalRowLength=0;
  254. do
  255. {
  256. SegmentType=FDef[BaseOffset++];
  257. unsigned char code = SegmentType / 32;
  258. unsigned char value = (SegmentType & 31) + 1;
  259. if(code==7)
  260. {
  261. memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
  262. ftcp += value;
  263. BaseOffset += value;
  264. }
  265. else
  266. {
  267. memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
  268. ftcp += value;
  269. }
  270. TotalRowLength+=value;
  271. } while(TotalRowLength<SpriteWidth);
  272. if (RightMargin>0)
  273. ftcp += RightMargin;
  274. RowAdd=SpriteWidth-TotalRowLength;
  275. if (add>0)
  276. ftcp += add+RowAdd;
  277. }
  278. }
  279. break;
  280. case 3:
  281. {
  282. for (unsigned int i=0;i<SpriteHeight;i++)
  283. {
  284. BaseOffset = BaseOffsetor + SDL_SwapLE16(read_unaligned_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32)));
  285. if (LeftMargin>0)
  286. ftcp += LeftMargin;
  287. TotalRowLength=0;
  288. do
  289. {
  290. SegmentType=FDef[BaseOffset++];
  291. unsigned char code = SegmentType / 32;
  292. unsigned char value = (SegmentType & 31) + 1;
  293. if(code==7)
  294. {
  295. for(int h=0; h<value; ++h)
  296. {
  297. if(h<-LeftMargin)
  298. continue;
  299. if(h+TotalRowLength>=SpriteWidth)
  300. break;
  301. ((char*)(ret->pixels))[ftcp++]=FDef[BaseOffset++];
  302. }
  303. }
  304. else
  305. {
  306. for(int h=0; h<value; ++h)
  307. {
  308. if(h<-LeftMargin)
  309. continue;
  310. if(h+TotalRowLength>=SpriteWidth)
  311. break;
  312. ((char*)(ret->pixels))[ftcp++]=code;
  313. }
  314. }
  315. TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
  316. }while(TotalRowLength<SpriteWidth);
  317. if (RightMargin>0)
  318. ftcp += RightMargin;
  319. RowAdd=SpriteWidth-TotalRowLength;
  320. if (add>0)
  321. ftcp += add+RowAdd;
  322. }
  323. }
  324. break;
  325. default:
  326. throw std::string("Unknown sprite format.");
  327. break;
  328. }
  329. SDL_Color ttcol = ret->format->palette->colors[0];
  330. Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.unused);
  331. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, keycol);
  332. return ret;
  333. };
  334. CDefEssential * CDefHandler::essentialize()
  335. {
  336. CDefEssential * ret = new CDefEssential;
  337. ret->ourImages = ourImages;
  338. notFreeImgs = true;
  339. return ret;
  340. }
  341. CDefHandler * CDefHandler::giveDef(std::string defName)
  342. {
  343. unsigned char * data = spriteh->giveFile(defName);
  344. if(!data)
  345. throw "bad def name!";
  346. CDefHandler * nh = new CDefHandler();
  347. nh->openFromMemory(data, defName);
  348. nh->alphaTransformed = false;
  349. delete [] data;
  350. return nh;
  351. }
  352. CDefEssential * CDefHandler::giveDefEss(std::string defName)
  353. {
  354. CDefEssential * ret;
  355. CDefHandler * temp = giveDef(defName);
  356. ret = temp->essentialize();
  357. delete temp;
  358. return ret;
  359. }