2
0

CCreatureAnimation.cpp 8.6 KB

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