CCreatureAnimation.cpp 9.3 KB

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