CCreatureAnimation.cpp 8.5 KB

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