CCreatureAnimation.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. int CCreatureAnimation::getType() const
  16. {
  17. return type;
  18. }
  19. void CCreatureAnimation::setType(int 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; //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. template<int bpp>
  140. 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*/)
  141. {
  142. //increasing frame numer
  143. int SIndex = curFrame;
  144. if(IncrementFrame)
  145. incrementFrame();
  146. //frame number increased
  147. long BaseOffset,
  148. SpriteWidth, SpriteHeight, //sprite format
  149. LeftMargin, RightMargin, TopMargin,BottomMargin,
  150. i, FullHeight,FullWidth,
  151. TotalRowLength; // length of read segment
  152. unsigned char SegmentType, SegmentLength;
  153. i=BaseOffset=SEntries[SIndex].offset;
  154. int prSize=readNormalNr<4>(i,FDef);i+=4;//TODO use me
  155. int defType2 = readNormalNr<4>(i,FDef);i+=4;
  156. FullWidth = readNormalNr<4>(i,FDef);i+=4;
  157. FullHeight = readNormalNr<4>(i,FDef);i+=4;
  158. SpriteWidth = readNormalNr<4>(i,FDef);i+=4;
  159. SpriteHeight = readNormalNr<4>(i,FDef);i+=4;
  160. LeftMargin = readNormalNr<4>(i,FDef);i+=4;
  161. TopMargin = readNormalNr<4>(i,FDef);i+=4;
  162. RightMargin = FullWidth - SpriteWidth - LeftMargin;
  163. BottomMargin = FullHeight - SpriteHeight - TopMargin;
  164. int BaseOffsetor = BaseOffset = i;
  165. int ftcp = 0;
  166. if (defType2==1) //as it should be always in creature animations
  167. {
  168. if (TopMargin>0)
  169. {
  170. ftcp+=FullWidth * TopMargin;
  171. }
  172. int * RLEntries = (int*)(FDef+BaseOffset);
  173. BaseOffset += sizeof(int) * SpriteHeight;
  174. for (int i=0;i<SpriteHeight;i++)
  175. {
  176. BaseOffset=BaseOffsetor+RLEntries[i];
  177. if (LeftMargin>0)
  178. {
  179. ftcp+=LeftMargin;
  180. }
  181. TotalRowLength=0;
  182. int yB = ftcp/FullWidth + y;
  183. bool omitIteration = false; //if true, we shouldn't try to blit this line to screen
  184. if(yB < 0 || yB >= dest->h || (destRect && (destRect->y > yB || destRect->y + destRect->h <= yB ) ) )
  185. {
  186. //update variables
  187. omitIteration = true;
  188. continue;
  189. }
  190. do
  191. {
  192. SegmentType=FDef[BaseOffset++];
  193. SegmentLength=FDef[BaseOffset++];
  194. if(omitIteration)
  195. {
  196. ftcp += SegmentLength+1;
  197. if(SegmentType == 0xFF)
  198. {
  199. BaseOffset += SegmentLength+1;
  200. }
  201. continue;
  202. }
  203. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  204. unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
  205. for (int k=0; k<=SegmentLength; k++)
  206. {
  207. if(xB>=0 && xB<dest->w)
  208. {
  209. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB ))
  210. {
  211. const ui8 colorNr = SegmentType == 0xff ? FDef[BaseOffset+k] : SegmentType;
  212. putPixel<bpp>(dest, xB + yB*dest->w, palette[colorNr], colorNr, yellowBorder, blueBorder, aCountMod);
  213. }
  214. }
  215. ftcp++; //increment pos
  216. if(attacker)
  217. xB++;
  218. else
  219. xB--;
  220. if ( SegmentType == 0xFF && TotalRowLength+k+1 >= SpriteWidth )
  221. break;
  222. }
  223. if (SegmentType == 0xFF)
  224. {
  225. BaseOffset += SegmentLength+1;
  226. }
  227. TotalRowLength+=SegmentLength+1;
  228. }while(TotalRowLength<SpriteWidth);
  229. if (RightMargin>0)
  230. {
  231. ftcp+=RightMargin;
  232. }
  233. }
  234. if (BottomMargin>0)
  235. {
  236. ftcp += BottomMargin * FullWidth;
  237. }
  238. }
  239. return 0;
  240. }
  241. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  242. {
  243. switch(dest->format->BytesPerPixel)
  244. {
  245. case 2: return nextFrameT<2>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  246. case 3: return nextFrameT<3>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  247. case 4: return nextFrameT<4>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  248. default:
  249. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  250. return -1;
  251. }
  252. }
  253. int CCreatureAnimation::framesInGroup(int group) const
  254. {
  255. if(frameGroups.find(group) == frameGroups.end())
  256. return 0;
  257. return frameGroups.find(group)->second.size();
  258. }
  259. CCreatureAnimation::~CCreatureAnimation()
  260. {
  261. delete [] FDef;
  262. }
  263. template<int bpp>
  264. inline void CCreatureAnimation::putPixel(
  265. SDL_Surface * dest,
  266. const int & ftcp,
  267. const BMPPalette & color,
  268. const unsigned char & palc,
  269. const bool & yellowBorder,
  270. const bool & blueBorder,
  271. const unsigned char & animCount
  272. ) const
  273. {
  274. if(palc!=0)
  275. {
  276. Uint8 * p = (Uint8*)dest->pixels + ftcp*dest->format->BytesPerPixel;
  277. if(palc > 7) //normal color
  278. {
  279. ColorPutter<bpp, 0>::PutColor(p, color.R, color.G, color.B);
  280. }
  281. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //selection highlight
  282. {
  283. if(blueBorder)
  284. ColorPutter<bpp, 0>::PutColor(p, 0, 0x0f + animCount, 0x0f + animCount);
  285. else
  286. ColorPutter<bpp, 0>::PutColor(p, 0x0f + animCount, 0x0f + animCount, 0);
  287. }
  288. else if (palc == 5) //selection highlight or transparent
  289. {
  290. if(blueBorder)
  291. ColorPutter<bpp, 0>::PutColor(p, color.B, color.G - 0xf0 + animCount, color.R - 0xf0 + animCount); //shouldnt it be reversed? its bgr instead of rgb
  292. else if (yellowBorder)
  293. ColorPutter<bpp, 0>::PutColor(p, color.R - 0xf0 + animCount, color.G - 0xf0 + animCount, color.B);
  294. }
  295. else //shadow
  296. {
  297. //determining transparency value, 255 or 0 should be already filtered
  298. Uint16 alpha;
  299. static Uint16 colToAlpha[8] = {255,192,128,128,128,255,128,192};
  300. alpha = colToAlpha[palc];
  301. if(bpp != 3 && bpp != 4)
  302. {
  303. ColorPutter<bpp, 0>::PutColor(p, 0, 0, 0, alpha);
  304. }
  305. else
  306. {
  307. p[0] = (p[0] * alpha)>>8;
  308. p[1] = (p[1] * alpha)>>8;
  309. p[2] = (p[2] * alpha)>>8;
  310. }
  311. }
  312. }
  313. }