CCreatureAnimation.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "CCreatureAnimation.h"
  2. #include "../lib/CLodHandler.h"
  3. #include "../lib/VCMI_Lib.h"
  4. #include <assert.h>
  5. #include "SDL_Extensions.h"
  6. /*
  7. * CCreatureAnimation.cpp, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. CCreatureAnim::EAnimType CCreatureAnimation::getType() const
  16. {
  17. return type;
  18. }
  19. void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
  20. {
  21. assert(framesInGroup(type) > 0 && "Bad type for void CCreatureAnimation::setType(int type)!");
  22. this->type = type;
  23. internalFrame = 0;
  24. if(type!=-1)
  25. {
  26. curFrame = frameGroups[type][0];
  27. }
  28. else
  29. {
  30. if(curFrame>=frames)
  31. {
  32. curFrame = 0;
  33. }
  34. }
  35. }
  36. CCreatureAnimation::CCreatureAnimation(std::string name) : internalFrame(0), once(false)
  37. {
  38. FDef = spriteh->giveFile(name, FILE_ANIMATION); //load main file
  39. //init anim data
  40. int i,j, totalInBlock;
  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; //omitting 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 = CCreatureAnim::WHOLE_ANIM;
  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 = CCreatureAnim::HOLDING;
  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. int CCreatureAnimation::getAnimationFrame() const
  125. {
  126. return internalFrame;
  127. }
  128. bool CCreatureAnimation::onFirstFrameInGroup()
  129. {
  130. return internalFrame == 0;
  131. }
  132. bool CCreatureAnimation::onLastFrameInGroup()
  133. {
  134. if(internalFrame == frameGroups[type].size() - 1)
  135. return true;
  136. return false;
  137. }
  138. void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
  139. {
  140. setType(type);
  141. once = true;
  142. }
  143. template<int bpp>
  144. int CCreatureAnimation::nextFrameT(SDL_Surface * dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame /*= true*/, bool yellowBorder /*= false*/, bool blueBorder /*= false*/, SDL_Rect * destRect /*= NULL*/)
  145. {
  146. //increasing frame number
  147. int SIndex = curFrame;
  148. if (IncrementFrame)
  149. incrementFrame();
  150. long BaseOffset,
  151. SpriteWidth, SpriteHeight, //sprite format
  152. LeftMargin, RightMargin, TopMargin,BottomMargin,
  153. i, FullHeight,FullWidth,
  154. TotalRowLength; // length of read segment
  155. unsigned char SegmentType, SegmentLength;
  156. i = BaseOffset = SEntries[SIndex].offset;
  157. /*int prSize = readNormalNr<4>(i, FDef);*/ i += 4; //TODO use me
  158. int defType2 = readNormalNr<4>(i, FDef); i += 4;
  159. FullWidth = readNormalNr<4>(i, FDef); i += 4;
  160. FullHeight = readNormalNr<4>(i, FDef); i += 4;
  161. SpriteWidth = readNormalNr<4>(i, FDef); i += 4;
  162. SpriteHeight = readNormalNr<4>(i, FDef); i += 4;
  163. LeftMargin = readNormalNr<4>(i, FDef); i += 4;
  164. TopMargin = readNormalNr<4>(i, FDef); i += 4;
  165. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  166. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  167. int BaseOffsetor = BaseOffset = i;
  168. int ftcp = 0;
  169. if (defType2 == 1) //as it should be always in creature animations
  170. {
  171. if (TopMargin > 0)
  172. {
  173. ftcp += FullWidth * TopMargin;
  174. }
  175. int *RLEntries = (int*)(FDef + BaseOffset);
  176. BaseOffset += sizeof(int) * SpriteHeight;
  177. for (int i = 0; i < SpriteHeight; i++)
  178. {
  179. BaseOffset = BaseOffsetor + RLEntries[i];
  180. if (LeftMargin > 0)
  181. {
  182. ftcp += LeftMargin;
  183. }
  184. TotalRowLength = 0;
  185. // Note: Bug fixed (Rev 2115): The implementation of omitting lines was false.
  186. // We've to calculate several things so not showing/putting pixels should suffice.
  187. int yB = ftcp / FullWidth + y;
  188. do
  189. {
  190. SegmentType = FDef[BaseOffset++];
  191. SegmentLength = FDef[BaseOffset++];
  192. int xB = (attacker ? ftcp % FullWidth : FullWidth - ftcp % FullWidth - 1) + x;
  193. unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e) >> 1) << 4 : (0x0f - ((animCount & 0x1e) >> 1)) << 4;
  194. for (int k = 0; k <= SegmentLength; k++)
  195. {
  196. if(xB >= 0 && xB < dest->w && yB >= 0 && yB < dest->h)
  197. {
  198. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB && destRect->y <= yB && destRect->y + destRect->h > yB))
  199. {
  200. const ui8 colorNr = SegmentType == 0xff ? FDef[BaseOffset+k] : SegmentType;
  201. putPixel<bpp>(dest, xB, yB, palette[colorNr], colorNr, yellowBorder, blueBorder, aCountMod);
  202. }
  203. }
  204. ftcp++; //increment pos
  205. if(attacker)
  206. xB++;
  207. else
  208. xB--;
  209. if ( SegmentType == 0xFF && TotalRowLength+k+1 >= SpriteWidth )
  210. break;
  211. }
  212. if (SegmentType == 0xFF)
  213. {
  214. BaseOffset += SegmentLength+1;
  215. }
  216. TotalRowLength+=SegmentLength+1;
  217. } while(TotalRowLength < SpriteWidth);
  218. if (RightMargin > 0)
  219. {
  220. ftcp += RightMargin;
  221. }
  222. }
  223. if (BottomMargin > 0)
  224. {
  225. ftcp += BottomMargin * FullWidth;
  226. }
  227. }
  228. return 0;
  229. }
  230. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  231. {
  232. switch(dest->format->BytesPerPixel)
  233. {
  234. case 2: return nextFrameT<2>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  235. case 3: return nextFrameT<3>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  236. case 4: return nextFrameT<4>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  237. default:
  238. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  239. return -1;
  240. }
  241. }
  242. int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
  243. {
  244. if(frameGroups.find(group) == frameGroups.end())
  245. return 0;
  246. return frameGroups.find(group)->second.size();
  247. }
  248. CCreatureAnimation::~CCreatureAnimation()
  249. {
  250. delete [] FDef;
  251. }
  252. template<int bpp>
  253. inline void CCreatureAnimation::putPixel(
  254. SDL_Surface * dest,
  255. const int & ftcpX,
  256. const int & ftcpY,
  257. const BMPPalette & color,
  258. const unsigned char & palc,
  259. const bool & yellowBorder,
  260. const bool & blueBorder,
  261. const unsigned char & animCount
  262. ) const
  263. {
  264. if(palc!=0)
  265. {
  266. Uint8 * p = (Uint8*)dest->pixels + ftcpX*dest->format->BytesPerPixel + ftcpY*dest->pitch;
  267. if(palc > 7) //normal color
  268. {
  269. ColorPutter<bpp, 0>::PutColor(p, color.R, color.G, color.B);
  270. }
  271. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //selection highlight
  272. {
  273. if(blueBorder)
  274. ColorPutter<bpp, 0>::PutColor(p, 0, 0x0f + animCount, 0x0f + animCount);
  275. else
  276. ColorPutter<bpp, 0>::PutColor(p, 0x0f + animCount, 0x0f + animCount, 0);
  277. }
  278. else if (palc == 5) //selection highlight or transparent
  279. {
  280. if(blueBorder)
  281. ColorPutter<bpp, 0>::PutColor(p, color.B, color.G - 0xf0 + animCount, color.R - 0xf0 + animCount); //shouldn't it be reversed? its bgr instead of rgb
  282. else if (yellowBorder)
  283. ColorPutter<bpp, 0>::PutColor(p, color.R - 0xf0 + animCount, color.G - 0xf0 + animCount, color.B);
  284. }
  285. else //shadow
  286. {
  287. //determining transparency value, 255 or 0 should be already filtered
  288. static Uint16 colToAlpha[8] = {255,192,128,128,128,255,128,192};
  289. Uint16 alpha = colToAlpha[palc];
  290. if(bpp != 3 && bpp != 4)
  291. {
  292. ColorPutter<bpp, 0>::PutColor(p, 0, 0, 0, alpha);
  293. }
  294. else
  295. {
  296. p[0] = (p[0] * alpha)>>8;
  297. p[1] = (p[1] * alpha)>>8;
  298. p[2] = (p[2] * alpha)>>8;
  299. }
  300. }
  301. }
  302. }