CCreatureAnimation.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "CCreatureAnimation.h"
  2. #include "../hch/CLodHandler.h"
  3. int CCreatureAnimation::getType() const
  4. {
  5. return type;
  6. }
  7. void CCreatureAnimation::setType(int type)
  8. {
  9. this->type = type;
  10. curFrame = 0;
  11. if(type!=-1)
  12. {
  13. if(SEntries[curFrame].group!=type) //rewind
  14. {
  15. int j=-1; //first frame in displayed group
  16. for(int g=0; g<SEntries.size(); ++g)
  17. {
  18. if(SEntries[g].group==type && j==-1)
  19. {
  20. j = g;
  21. break;
  22. }
  23. }
  24. if(curFrame!=-1)
  25. curFrame = j;
  26. }
  27. }
  28. else
  29. {
  30. if(curFrame>=frames)
  31. curFrame = 0;
  32. }
  33. }
  34. CCreatureAnimation::CCreatureAnimation(std::string name) : RLEntries(NULL), RWEntries(NULL)
  35. {
  36. FDef = CDefHandler::Spriteh->giveFile(name); //load main file
  37. //init anim data
  38. int i,j, totalInBlock;
  39. char Buffer[13];
  40. defName=name;
  41. i = 0;
  42. DEFType = readNormalNr(i,4); i+=4;
  43. fullWidth = readNormalNr(i,4); i+=4;
  44. fullHeight = readNormalNr(i,4); i+=4;
  45. i=0xc;
  46. totalBlocks = readNormalNr(i,4); i+=4;
  47. i=0x10;
  48. for (int it=0;it<256;it++)
  49. {
  50. palette[it].R = FDef[i++];
  51. palette[it].G = FDef[i++];
  52. palette[it].B = FDef[i++];
  53. palette[it].F = 0;
  54. }
  55. i=0x310;
  56. totalEntries=0;
  57. for (int z=0; z<totalBlocks; z++)
  58. {
  59. int group = readNormalNr(i,4); i+=4; //block ID
  60. totalInBlock = readNormalNr(i,4); i+=4;
  61. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  62. {
  63. SEntries.push_back(SEntry());
  64. SEntries[j].group = group;
  65. }
  66. int unknown2 = readNormalNr(i,4); i+=4;
  67. int unknown3 = readNormalNr(i,4); i+=4;
  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. int unknown4 = readNormalNr(i,4); i+=4;
  78. }
  79. //totalEntries+=totalInBlock;
  80. for(int hh=0; hh<totalInBlock; ++hh)
  81. {
  82. ++totalEntries;
  83. }
  84. }
  85. for(j=0; j<SEntries.size(); ++j)
  86. {
  87. SEntries[j].name = SEntries[j].name.substr(0, SEntries[j].name.find('.')+4);
  88. }
  89. //init vars
  90. curFrame = 0;
  91. type = -1;
  92. frames = totalEntries;
  93. }
  94. int CCreatureAnimation::readNormalNr (int pos, int bytCon, unsigned char * str, bool cyclic)
  95. {
  96. int ret=0;
  97. int amp=1;
  98. if (str)
  99. {
  100. for (int i=0; i<bytCon; i++)
  101. {
  102. ret+=str[pos+i]*amp;
  103. amp*=256;
  104. }
  105. }
  106. else
  107. {
  108. for (int i=0; i<bytCon; i++)
  109. {
  110. ret+=FDef[pos+i]*amp;
  111. amp*=256;
  112. }
  113. }
  114. if(cyclic && bytCon<4 && ret>=amp/2)
  115. {
  116. ret = ret-amp;
  117. }
  118. return ret;
  119. }
  120. int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, bool incrementFrame, bool yellowBorder, SDL_Rect * destRect)
  121. {
  122. return nextFrame(dest,x-fullWidth/2,y-fullHeight/2,attacker,incrementFrame,yellowBorder,destRect);
  123. }
  124. void CCreatureAnimation::incrementFrame()
  125. {
  126. curFrame++;
  127. if(type!=-1)
  128. {
  129. if(curFrame==SEntries.size() || SEntries[curFrame].group!=type) //rewind
  130. {
  131. int j=-1; //first frame in displayed group
  132. for(int g=0; g<SEntries.size(); ++g)
  133. {
  134. if(SEntries[g].group==type && j==-1)
  135. {
  136. j = g;
  137. break;
  138. }
  139. }
  140. if(curFrame!=-1)
  141. curFrame = j;
  142. }
  143. }
  144. else
  145. {
  146. if(curFrame>=frames)
  147. curFrame = 0;
  148. }
  149. }
  150. int CCreatureAnimation::getFrame() const
  151. {
  152. return curFrame;
  153. }
  154. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, bool IncrementFrame, bool yellowBorder, SDL_Rect * destRect)
  155. {
  156. if(dest->format->BytesPerPixel<3)
  157. return -1; //not enough depth
  158. //increasing frame numer
  159. int SIndex = curFrame;
  160. if(IncrementFrame)
  161. incrementFrame();
  162. //frame number increased
  163. long BaseOffset,
  164. SpriteWidth, SpriteHeight, //sprite format
  165. LeftMargin, RightMargin, TopMargin,BottomMargin,
  166. i, add, FullHeight,FullWidth,
  167. TotalRowLength, // length of read segment
  168. RowAdd;
  169. unsigned char SegmentType, SegmentLength;
  170. i=BaseOffset=SEntries[SIndex].offset;
  171. int prSize=readNormalNr(i,4,FDef);i+=4;
  172. int defType2 = readNormalNr(i,4,FDef);i+=4;
  173. FullWidth = readNormalNr(i,4,FDef);i+=4;
  174. FullHeight = readNormalNr(i,4,FDef);i+=4;
  175. SpriteWidth = readNormalNr(i,4,FDef);i+=4;
  176. SpriteHeight = readNormalNr(i,4,FDef);i+=4;
  177. LeftMargin = readNormalNr(i,4,FDef);i+=4;
  178. TopMargin = readNormalNr(i,4,FDef);i+=4;
  179. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  180. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  181. add = 4 - FullWidth%4;
  182. int BaseOffsetor = BaseOffset = i;
  183. int ftcp = 0;
  184. if (defType2==1) //as it should be allways in creature animations
  185. {
  186. if (TopMargin>0)
  187. {
  188. for (int i=0;i<TopMargin;i++)
  189. {
  190. ftcp+=FullWidth+add;
  191. }
  192. }
  193. RLEntries = new int[SpriteHeight];
  194. for (int i=0;i<SpriteHeight;i++)
  195. {
  196. RLEntries[i]=readNormalNr(BaseOffset,4,FDef);BaseOffset+=4;
  197. }
  198. for (int i=0;i<SpriteHeight;i++)
  199. {
  200. BaseOffset=BaseOffsetor+RLEntries[i];
  201. if (LeftMargin>0)
  202. {
  203. ftcp+=LeftMargin;
  204. }
  205. TotalRowLength=0;
  206. do
  207. {
  208. SegmentType=FDef[BaseOffset++];
  209. SegmentLength=FDef[BaseOffset++];
  210. if (SegmentType==0xFF)
  211. {
  212. for (int k=0;k<=SegmentLength;k++)
  213. {
  214. int xB = (attacker ? ftcp%(FullWidth+add) : (FullWidth+add) - ftcp%(FullWidth+add) - 1) + x;
  215. int yB = ftcp/(FullWidth+add) + y;
  216. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  217. {
  218. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  219. putPixel(dest, xB + yB*dest->w, palette[FDef[BaseOffset+k]], FDef[BaseOffset+k], yellowBorder);
  220. }
  221. ftcp++; //increment pos
  222. if ((TotalRowLength+k+1)>=SpriteWidth)
  223. break;
  224. }
  225. BaseOffset+=SegmentLength+1;////
  226. TotalRowLength+=SegmentLength+1;
  227. }
  228. else
  229. {
  230. for (int k=0;k<SegmentLength+1;k++)
  231. {
  232. int xB = (attacker ? ftcp%(FullWidth+add) : (FullWidth+add) - ftcp%(FullWidth+add) - 1) + x;
  233. int yB = ftcp/(FullWidth+add) + y;
  234. if(xB>=0 && yB>=0 && xB<dest->w && yB<dest->h)
  235. {
  236. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  237. putPixel(dest, xB + yB*dest->w, palette[SegmentType], SegmentType, yellowBorder);
  238. }
  239. ftcp++; //increment pos
  240. }
  241. TotalRowLength+=SegmentLength+1;
  242. }
  243. }while(TotalRowLength<SpriteWidth);
  244. RowAdd=SpriteWidth-TotalRowLength;
  245. if (RightMargin>0)
  246. {
  247. ftcp+=RightMargin;
  248. }
  249. if (add>0)
  250. {
  251. ftcp+=add+RowAdd;
  252. }
  253. }
  254. delete [] RLEntries;
  255. RLEntries = NULL;
  256. if (BottomMargin>0)
  257. {
  258. ftcp += BottomMargin * (FullWidth+add);
  259. }
  260. }
  261. //SDL_UpdateRect(dest, x, y, FullWidth+add, FullHeight);
  262. return 0;
  263. }
  264. int CCreatureAnimation::framesInGroup(int group) const
  265. {
  266. int ret = 0; //number of frames in given group
  267. for(int g=0; g<SEntries.size(); ++g)
  268. {
  269. if(SEntries[g].group == group)
  270. ++ret;
  271. }
  272. return ret;
  273. }
  274. CCreatureAnimation::~CCreatureAnimation()
  275. {
  276. delete [] FDef;
  277. if (RWEntries)
  278. delete [] RWEntries;
  279. if (RLEntries)
  280. delete [] RLEntries;
  281. }
  282. void CCreatureAnimation::putPixel(SDL_Surface * dest, const int & ftcp, const BMPPalette & color, const unsigned char & palc, const bool & yellowBorder) const
  283. {
  284. if(palc!=0)
  285. {
  286. Uint8 * p = (Uint8*)dest->pixels + ftcp*3;
  287. if(palc > 7) //normal color
  288. {
  289. p[0] = color.B;
  290. p[1] = color.G;
  291. p[2] = color.R;
  292. }
  293. else if(yellowBorder && (palc == 6 || palc == 7)) //dark yellow border
  294. {
  295. p[0] = 0;
  296. p[1] = 0xff;
  297. p[2] = 0xff;
  298. }
  299. else if(yellowBorder && (palc == 5)) //yellow border
  300. {
  301. p[0] = color.B;
  302. p[1] = color.G;
  303. p[2] = color.R;
  304. }
  305. else if(palc < 5) //shadow
  306. {
  307. Uint16 alpha;
  308. switch(color.G)
  309. {
  310. case 0:
  311. alpha = 128;
  312. break;
  313. case 50:
  314. alpha = 50+32;
  315. break;
  316. case 100:
  317. alpha = 100+64;
  318. break;
  319. case 125:
  320. alpha = 125+64;
  321. break;
  322. case 128:
  323. alpha = 128+64;
  324. break;
  325. case 150:
  326. alpha = 150+64;
  327. break;
  328. default:
  329. alpha = 255;
  330. break;
  331. }
  332. //alpha counted
  333. p[0] = (p[0] * alpha)>>8;
  334. p[1] = (p[1] * alpha)>>8;
  335. p[2] = (p[2] * alpha)>>8;
  336. }
  337. }
  338. }