CreatureAnimation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * CCreatureAnimation.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CreatureAnimation.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../../lib/CCreatureHandler.h"
  14. #include "../render/Canvas.h"
  15. #include "../render/ColorFilter.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
  18. static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
  19. static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
  20. static SDL_Color genShadow(ui8 alpha)
  21. {
  22. return CSDL_Ext::makeColor(0, 0, 0, alpha);
  23. }
  24. SDL_Color AnimationControls::getBlueBorder()
  25. {
  26. return creatureBlueBorder;
  27. }
  28. SDL_Color AnimationControls::getGoldBorder()
  29. {
  30. return creatureGoldBorder;
  31. }
  32. SDL_Color AnimationControls::getNoBorder()
  33. {
  34. return creatureNoBorder;
  35. }
  36. std::shared_ptr<CreatureAnimation> AnimationControls::getAnimation(const CCreature * creature)
  37. {
  38. auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
  39. return std::make_shared<CreatureAnimation>(creature->animDefName, func);
  40. }
  41. float AnimationControls::getAnimationSpeedFactor()
  42. {
  43. // according to testing, H3 ratios between slow/medium/fast might actually be 36/60/100 (x1.666)
  44. // exact value is hard to tell due to large rounding errors
  45. // however we will assume them to be 33/66/100 since these values are better for standard 60 fps displays:
  46. // with these numbers, base frame display duration will be 100/66/33 ms - exactly 6/4/2 frames
  47. return settings["battle"]["speedFactor"].Float();
  48. }
  49. float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType type)
  50. {
  51. assert(creature->animation.walkAnimationTime != 0);
  52. assert(creature->animation.attackAnimationTime != 0);
  53. assert(anim->framesInGroup(type) != 0);
  54. // possible new fields for creature format:
  55. //split "Attack time" into "Shoot Time" and "Cast Time"
  56. // base speed for all H3 animations on slow speed is 10 frames per second (or 100ms per frame)
  57. const float baseSpeed = 10.f;
  58. const float speed = baseSpeed * getAnimationSpeedFactor();
  59. switch (type)
  60. {
  61. case ECreatureAnimType::MOVING:
  62. return speed / creature->animation.walkAnimationTime;
  63. case ECreatureAnimType::MOUSEON:
  64. return baseSpeed;
  65. case ECreatureAnimType::HOLDING:
  66. return creature->animation.idleAnimationTime;
  67. case ECreatureAnimType::SHOOT_UP:
  68. case ECreatureAnimType::SHOOT_FRONT:
  69. case ECreatureAnimType::SHOOT_DOWN:
  70. case ECreatureAnimType::SPECIAL_UP:
  71. case ECreatureAnimType::SPECIAL_FRONT:
  72. case ECreatureAnimType::SPECIAL_DOWN:
  73. case ECreatureAnimType::CAST_DOWN:
  74. case ECreatureAnimType::CAST_FRONT:
  75. case ECreatureAnimType::CAST_UP:
  76. return speed / creature->animation.attackAnimationTime;
  77. // as strange as it looks like "attackAnimationTime" does not affects melee attacks
  78. // necessary because length of these animations must be same for all creatures for synchronization
  79. case ECreatureAnimType::ATTACK_UP:
  80. case ECreatureAnimType::ATTACK_FRONT:
  81. case ECreatureAnimType::ATTACK_DOWN:
  82. case ECreatureAnimType::HITTED:
  83. case ECreatureAnimType::DEFENCE:
  84. case ECreatureAnimType::DEATH:
  85. case ECreatureAnimType::DEATH_RANGED:
  86. case ECreatureAnimType::RESURRECTION:
  87. case ECreatureAnimType::GROUP_ATTACK_DOWN:
  88. case ECreatureAnimType::GROUP_ATTACK_FRONT:
  89. case ECreatureAnimType::GROUP_ATTACK_UP:
  90. return speed;
  91. case ECreatureAnimType::TURN_L:
  92. case ECreatureAnimType::TURN_R:
  93. return speed;
  94. case ECreatureAnimType::MOVE_START:
  95. case ECreatureAnimType::MOVE_END:
  96. return speed;
  97. case ECreatureAnimType::DEAD:
  98. case ECreatureAnimType::DEAD_RANGED:
  99. return speed;
  100. default:
  101. return speed;
  102. }
  103. }
  104. float AnimationControls::getProjectileSpeed()
  105. {
  106. // H3 speed: 1250/2500/3750 pixels per second
  107. return static_cast<float>(getAnimationSpeedFactor() * 1250);
  108. }
  109. float AnimationControls::getRayProjectileSpeed()
  110. {
  111. // H3 speed: 4000/8000/12000 pixels per second
  112. return static_cast<float>(getAnimationSpeedFactor() * 4000);
  113. }
  114. float AnimationControls::getCatapultSpeed()
  115. {
  116. // H3 speed: 200/400/600 pixels per second
  117. return static_cast<float>(getAnimationSpeedFactor() * 200);
  118. }
  119. float AnimationControls::getSpellEffectSpeed()
  120. {
  121. // H3 speed: 10/20/30 frames per second
  122. return static_cast<float>(getAnimationSpeedFactor() * 10);
  123. }
  124. float AnimationControls::getMovementDistance(const CCreature * creature)
  125. {
  126. // H3 speed: 2/4/6 tiles per second
  127. return static_cast<float>( 2.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  128. }
  129. float AnimationControls::getFlightDistance(const CCreature * creature)
  130. {
  131. // Note: for whatever reason, H3 uses "Walk Animation Time" here, even though "Flight Animation Distance" also exists
  132. // H3 speed: 250/500/750 pixels per second
  133. return static_cast<float>( 250.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  134. }
  135. float AnimationControls::getFadeInDuration()
  136. {
  137. // H3 speed: 500/250/166 ms
  138. return 0.5f / getAnimationSpeedFactor();
  139. }
  140. float AnimationControls::getObstaclesSpeed()
  141. {
  142. // H3 speed: 20 frames per second, irregardless of speed setting.
  143. return 20.f;
  144. }
  145. ECreatureAnimType CreatureAnimation::getType() const
  146. {
  147. return type;
  148. }
  149. void CreatureAnimation::setType(ECreatureAnimType type)
  150. {
  151. this->type = type;
  152. currentFrame = 0;
  153. once = false;
  154. play();
  155. }
  156. CreatureAnimation::CreatureAnimation(const std::string & name_, TSpeedController controller)
  157. : name(name_),
  158. speed(0.1f),
  159. shadowAlpha(128),
  160. currentFrame(0),
  161. elapsedTime(0),
  162. type(ECreatureAnimType::HOLDING),
  163. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  164. speedController(controller),
  165. once(false)
  166. {
  167. forward = std::make_shared<CAnimation>(name_);
  168. reverse = std::make_shared<CAnimation>(name_);
  169. //todo: optimize
  170. forward->preload();
  171. reverse->preload();
  172. // if necessary, add one frame into vcmi-only group DEAD
  173. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  174. {
  175. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  176. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  177. }
  178. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  179. {
  180. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  181. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  182. }
  183. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  184. {
  185. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  186. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  187. }
  188. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  189. {
  190. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  191. {
  192. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  193. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  194. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  195. }
  196. }
  197. //TODO: get dimensions form CAnimation
  198. auto first = forward->getImage(0, size_t(type), true);
  199. if(!first)
  200. {
  201. fullWidth = 0;
  202. fullHeight = 0;
  203. return;
  204. }
  205. fullWidth = first->width();
  206. fullHeight = first->height();
  207. reverse->verticalFlip();
  208. play();
  209. }
  210. void CreatureAnimation::endAnimation()
  211. {
  212. once = false;
  213. auto copy = onAnimationReset;
  214. onAnimationReset.clear();
  215. copy();
  216. }
  217. bool CreatureAnimation::incrementFrame(float timePassed)
  218. {
  219. elapsedTime += timePassed;
  220. currentFrame += timePassed * speed;
  221. const auto framesNumber = framesInGroup(type);
  222. if(framesNumber <= 0)
  223. {
  224. endAnimation();
  225. }
  226. else if(currentFrame >= float(framesNumber))
  227. {
  228. // just in case of extremely low fps (or insanely high speed)
  229. while(currentFrame >= float(framesNumber))
  230. currentFrame -= framesNumber;
  231. if(once)
  232. setType(ECreatureAnimType::HOLDING);
  233. endAnimation();
  234. return true;
  235. }
  236. return false;
  237. }
  238. void CreatureAnimation::setBorderColor(SDL_Color palette)
  239. {
  240. border = palette;
  241. }
  242. int CreatureAnimation::getWidth() const
  243. {
  244. return fullWidth;
  245. }
  246. int CreatureAnimation::getHeight() const
  247. {
  248. return fullHeight;
  249. }
  250. float CreatureAnimation::getCurrentFrame() const
  251. {
  252. return currentFrame;
  253. }
  254. void CreatureAnimation::playOnce( ECreatureAnimType type )
  255. {
  256. setType(type);
  257. once = true;
  258. }
  259. inline int getBorderStrength(float time)
  260. {
  261. float borderStrength = fabs(std::round(time) - time) * 2; // generate value in range 0-1
  262. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  263. }
  264. static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
  265. {
  266. return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  267. }
  268. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  269. {
  270. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  271. }
  272. static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
  273. {
  274. return CSDL_Ext::makeColor(
  275. mixChannels(over.r, base.r, over.a, base.a),
  276. mixChannels(over.g, base.g, over.a, base.a),
  277. mixChannels(over.b, base.b, over.a, base.a),
  278. ui8(over.a + base.a * (255 - over.a) / 256)
  279. );
  280. }
  281. void CreatureAnimation::genSpecialPalette(IImage::SpecialPalette & target)
  282. {
  283. target[0] = genShadow(shadowAlpha / 2);
  284. target[1] = genShadow(shadowAlpha / 2);
  285. target[2] = genShadow(shadowAlpha);
  286. target[3] = genShadow(shadowAlpha);
  287. target[4] = genBorderColor(getBorderStrength(elapsedTime), border);
  288. target[5] = addColors(genShadow(shadowAlpha), genBorderColor(getBorderStrength(elapsedTime), border));
  289. target[6] = addColors(genShadow(shadowAlpha / 2), genBorderColor(getBorderStrength(elapsedTime), border));
  290. }
  291. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  292. {
  293. SDL_Color shadowTest = shifter.shiftColor(genShadow(128));
  294. shadowAlpha = shadowTest.a;
  295. size_t frame = static_cast<size_t>(floor(currentFrame));
  296. std::shared_ptr<IImage> image;
  297. if(facingRight)
  298. image = forward->getImage(frame, size_t(type));
  299. else
  300. image = reverse->getImage(frame, size_t(type));
  301. if(image)
  302. {
  303. IImage::SpecialPalette SpecialPalette;
  304. genSpecialPalette(SpecialPalette);
  305. image->setSpecialPallete(SpecialPalette);
  306. image->adjustPalette(shifter, 8);
  307. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  308. }
  309. }
  310. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  311. {
  312. return static_cast<int>(forward->size(size_t(group)));
  313. }
  314. bool CreatureAnimation::isDead() const
  315. {
  316. return getType() == ECreatureAnimType::DEAD
  317. || getType() == ECreatureAnimType::DEAD_RANGED;
  318. }
  319. bool CreatureAnimation::isDying() const
  320. {
  321. return getType() == ECreatureAnimType::DEATH
  322. || getType() == ECreatureAnimType::DEATH_RANGED;
  323. }
  324. bool CreatureAnimation::isDeadOrDying() const
  325. {
  326. return getType() == ECreatureAnimType::DEAD
  327. || getType() == ECreatureAnimType::DEATH
  328. || getType() == ECreatureAnimType::DEAD_RANGED
  329. || getType() == ECreatureAnimType::DEATH_RANGED;
  330. }
  331. bool CreatureAnimation::isIdle() const
  332. {
  333. return getType() == ECreatureAnimType::HOLDING
  334. || getType() == ECreatureAnimType::MOUSEON;
  335. }
  336. bool CreatureAnimation::isMoving() const
  337. {
  338. return getType() == ECreatureAnimType::MOVE_START
  339. || getType() == ECreatureAnimType::MOVING
  340. || getType() == ECreatureAnimType::MOVE_END
  341. || getType() == ECreatureAnimType::TURN_L
  342. || getType() == ECreatureAnimType::TURN_R;
  343. }
  344. bool CreatureAnimation::isShooting() const
  345. {
  346. return getType() == ECreatureAnimType::SHOOT_UP
  347. || getType() == ECreatureAnimType::SHOOT_FRONT
  348. || getType() == ECreatureAnimType::SHOOT_DOWN;
  349. }
  350. void CreatureAnimation::pause()
  351. {
  352. speed = 0;
  353. }
  354. void CreatureAnimation::play()
  355. {
  356. //logAnim->trace("Play %s group %d at %d:%d", name, static_cast<int>(getType()), pos.x, pos.y);
  357. speed = speedController(this, type);
  358. }