CCreatureAnimation.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "CCreatureAnimation.h"
  2. #include "../hch/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); //load main file
  39. //init anim data
  40. int i,j, totalInBlock;
  41. char Buffer[13];
  42. defName=name;
  43. i = 0;
  44. DEFType = readNormalNr<4>(i,FDef); i+=4;
  45. fullWidth = readNormalNr<4>(i,FDef); i+=4;
  46. fullHeight = readNormalNr<4>(i,FDef); i+=4;
  47. i=0xc;
  48. totalBlocks = readNormalNr<4>(i,FDef); i+=4;
  49. i=0x10;
  50. for (int it=0;it<256;it++)
  51. {
  52. palette[it].R = FDef[i++];
  53. palette[it].G = FDef[i++];
  54. palette[it].B = FDef[i++];
  55. palette[it].F = 0;
  56. }
  57. i=0x310;
  58. totalEntries=0;
  59. for (int z=0; z<totalBlocks; z++)
  60. {
  61. std::vector<int> frameIDs;
  62. int group = readNormalNr<4>(i,FDef); i+=4; //block ID
  63. totalInBlock = readNormalNr<4>(i,FDef); i+=4;
  64. for (j=SEntries.size(); j<totalEntries+totalInBlock; j++)
  65. {
  66. SEntries.push_back(SEntry());
  67. SEntries[j].group = group;
  68. frameIDs.push_back(j);
  69. }
  70. int unknown2 = readNormalNr<4>(i,FDef); i+=4; //TODO use me
  71. int unknown3 = readNormalNr<4>(i,FDef); i+=4; //TODO use me
  72. i+=13*totalInBlock; //ommiting names
  73. for (j=0; j<totalInBlock; j++)
  74. {
  75. SEntries[totalEntries+j].offset = readNormalNr<4>(i,FDef); i+=4;
  76. }
  77. //totalEntries+=totalInBlock;
  78. for(int hh=0; hh<totalInBlock; ++hh)
  79. {
  80. ++totalEntries;
  81. }
  82. frameGroups[group] = frameIDs;
  83. }
  84. //init vars
  85. curFrame = 0;
  86. type = -1;
  87. frames = totalEntries;
  88. }
  89. int CCreatureAnimation::nextFrameMiddle(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool incrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  90. {
  91. return nextFrame(dest, x-fullWidth/2, y-fullHeight/2, attacker, animCount, incrementFrame, yellowBorder, blueBorder, destRect);
  92. }
  93. void CCreatureAnimation::incrementFrame()
  94. {
  95. if(type!=-1) //when a specific part of animation is played
  96. {
  97. ++internalFrame;
  98. if(internalFrame == frameGroups[type].size()) //rewind
  99. {
  100. internalFrame = 0;
  101. if(once) //playing animation once - return to standing animation
  102. {
  103. type = 2;
  104. once = false;
  105. curFrame = frameGroups[2][0];
  106. }
  107. else //
  108. {
  109. curFrame = frameGroups[type][0];
  110. }
  111. }
  112. curFrame = frameGroups[type][internalFrame];
  113. }
  114. else //when whole animation is played
  115. {
  116. ++curFrame;
  117. if(curFrame>=frames)
  118. curFrame = 0;
  119. }
  120. }
  121. int CCreatureAnimation::getFrame() const
  122. {
  123. return curFrame;
  124. }
  125. bool CCreatureAnimation::onFirstFrameInGroup()
  126. {
  127. return internalFrame == 0;
  128. }
  129. bool CCreatureAnimation::onLastFrameInGroup()
  130. {
  131. if(internalFrame == frameGroups[type].size() - 1)
  132. return true;
  133. return false;
  134. }
  135. void CCreatureAnimation::playOnce(int type)
  136. {
  137. setType(type);
  138. once = true;
  139. }
  140. template<int bpp>
  141. 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*/)
  142. {
  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. int yB = ftcp/FullWidth + y;
  184. bool omitIteration = false; //if true, we shouldn't try to blit this line to screen
  185. if(yB < 0 || yB >= dest->h || (destRect && (destRect->y > yB || destRect->y + destRect->h <= yB ) ) )
  186. {
  187. //update variables
  188. omitIteration = true;
  189. continue;
  190. }
  191. do
  192. {
  193. SegmentType=FDef[BaseOffset++];
  194. SegmentLength=FDef[BaseOffset++];
  195. if(omitIteration)
  196. {
  197. ftcp += SegmentLength+1;
  198. if(SegmentType == 0xFF)
  199. {
  200. BaseOffset += SegmentLength+1;
  201. }
  202. continue;
  203. }
  204. int xB = (attacker ? ftcp%FullWidth : FullWidth - ftcp%FullWidth - 1) + x;
  205. unsigned char aCountMod = (animCount & 0x20) ? ((animCount & 0x1e)>>1)<<4 : 0x0f - ((animCount & 0x1e)>>1)<<4;
  206. for (int k=0; k<=SegmentLength; k++)
  207. {
  208. if(xB>=0 && xB<dest->w)
  209. {
  210. if(!destRect || (destRect->x <= xB && destRect->x + destRect->w > xB ))
  211. {
  212. const ui8 colorNr = SegmentType == 0xff ? FDef[BaseOffset+k] : SegmentType;
  213. putPixel<bpp>(dest, xB + yB*dest->w, palette[colorNr], colorNr, yellowBorder, blueBorder, aCountMod);
  214. }
  215. }
  216. ftcp++; //increment pos
  217. if(attacker)
  218. xB++;
  219. else
  220. xB--;
  221. if ( SegmentType == 0xFF && TotalRowLength+k+1 >= SpriteWidth )
  222. break;
  223. }
  224. if (SegmentType == 0xFF)
  225. {
  226. BaseOffset += SegmentLength+1;
  227. }
  228. TotalRowLength+=SegmentLength+1;
  229. }while(TotalRowLength<SpriteWidth);
  230. if (RightMargin>0)
  231. {
  232. ftcp+=RightMargin;
  233. }
  234. }
  235. if (BottomMargin>0)
  236. {
  237. ftcp += BottomMargin * FullWidth;
  238. }
  239. }
  240. return 0;
  241. }
  242. int CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, unsigned char animCount, bool IncrementFrame, bool yellowBorder, bool blueBorder, SDL_Rect * destRect)
  243. {
  244. switch(dest->format->BytesPerPixel)
  245. {
  246. case 2: return nextFrameT<2>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  247. case 3: return nextFrameT<3>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  248. case 4: return nextFrameT<4>(dest, x, y, attacker, animCount, IncrementFrame, yellowBorder, blueBorder, destRect);
  249. default:
  250. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  251. return -1;
  252. }
  253. }
  254. int CCreatureAnimation::framesInGroup(int group) const
  255. {
  256. if(frameGroups.find(group) == frameGroups.end())
  257. return 0;
  258. return frameGroups.find(group)->second.size();
  259. }
  260. CCreatureAnimation::~CCreatureAnimation()
  261. {
  262. delete [] FDef;
  263. }
  264. template<int bpp>
  265. inline void CCreatureAnimation::putPixel(
  266. SDL_Surface * dest,
  267. const int & ftcp,
  268. const BMPPalette & color,
  269. const unsigned char & palc,
  270. const bool & yellowBorder,
  271. const bool & blueBorder,
  272. const unsigned char & animCount
  273. ) const
  274. {
  275. if(palc!=0)
  276. {
  277. Uint8 * p = (Uint8*)dest->pixels + ftcp*dest->format->BytesPerPixel;
  278. if(palc > 7) //normal color
  279. {
  280. ColorPutter<bpp, 0>::PutColor(p, color.R, color.G, color.B);
  281. }
  282. else if((yellowBorder || blueBorder) && (palc == 6 || palc == 7)) //dark yellow border
  283. {
  284. if(blueBorder)
  285. ColorPutter<bpp, 0>::PutColor(p, 0, 0x0f + animCount, 0x0f + animCount);
  286. else
  287. ColorPutter<bpp, 0>::PutColor(p, 0x0f + animCount, 0x0f + animCount, 0);
  288. }
  289. else if((yellowBorder || blueBorder) && (palc == 5)) //yellow border
  290. {
  291. if(blueBorder)
  292. ColorPutter<bpp, 0>::PutColor(p, color.B, color.G - 0xf0 + animCount, color.R - 0xf0 + animCount); //shouldnt it be reversed? its bgr instead of rgb
  293. else
  294. ColorPutter<bpp, 0>::PutColor(p, color.R - 0xf0 + animCount, color.G - 0xf0 + animCount, color.B);
  295. }
  296. else if(palc < 5) //shadow
  297. {
  298. Uint16 alpha;
  299. switch(color.G)
  300. {
  301. case 0:
  302. alpha = 128;
  303. break;
  304. case 50:
  305. alpha = 50+32;
  306. break;
  307. case 100:
  308. alpha = 100+64;
  309. break;
  310. case 125:
  311. alpha = 125+64;
  312. break;
  313. case 128:
  314. alpha = 128+64;
  315. break;
  316. case 150:
  317. alpha = 150+64;
  318. break;
  319. default:
  320. alpha = 255;
  321. break;
  322. }
  323. if(bpp != 3 && bpp != 4)
  324. {
  325. ColorPutter<bpp, 0>::PutColor(p, 0, 0, 0, alpha);
  326. }
  327. else
  328. {
  329. p[0] = (p[0] * alpha)>>8;
  330. p[1] = (p[1] * alpha)>>8;
  331. p[2] = (p[2] * alpha)>>8;
  332. }
  333. }
  334. }
  335. }