CCreatureAnimation.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CCreatureAnimation.h, 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. #pragma once
  11. #include "../../lib/FunctionList.h"
  12. #include "../gui/SDL_Extensions.h"
  13. #include "../widgets/Images.h"
  14. class CIntObject;
  15. class CCreatureAnimation;
  16. /// Namespace for some common controls of animations
  17. namespace AnimationControls
  18. {
  19. /// get SDL_Color for creature selection borders
  20. SDL_Color getBlueBorder();
  21. SDL_Color getGoldBorder();
  22. SDL_Color getNoBorder();
  23. /// creates animation object with preset speed control
  24. CCreatureAnimation * getAnimation(const CCreature * creature);
  25. /// returns animation speed of specific group, taking in mind game setting (in frames per second)
  26. float getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t groupID);
  27. /// returns how far projectile should move each frame
  28. /// TODO: make it time-based
  29. float getProjectileSpeed();
  30. /// returns speed of any spell effects, including any special effects like morale (in frames per second)
  31. float getSpellEffectSpeed();
  32. /// returns duration of full movement animation, in seconds. Needed to move animation on screen
  33. float getMovementDuration(const CCreature * creature);
  34. /// Returns distance on which flying creatures should during one animation loop
  35. float getFlightDistance(const CCreature * creature);
  36. }
  37. /// Class which manages animations of creatures/units inside battles
  38. /// TODO: split into constant image container and class that does *control* of animation
  39. class CCreatureAnimation : public CIntObject
  40. {
  41. public:
  42. typedef std::function<float(CCreatureAnimation *, size_t)> TSpeedController;
  43. private:
  44. std::string defName;
  45. int fullWidth, fullHeight;
  46. // palette, as read from def file
  47. std::array<SDL_Color, 256> palette;
  48. //key = id of group (note that some groups may be missing)
  49. //value = offset of pixel data for each frame, vector size = number of frames in group
  50. std::map<int, std::vector<unsigned int>> dataOffsets;
  51. //animation raw data
  52. //TODO: use vector instead?
  53. std::unique_ptr<ui8[]> pixelData;
  54. size_t pixelDataSize;
  55. // speed of animation, measure in frames per second
  56. float speed;
  57. // currently displayed frame. Float to allow H3-style animations where frames
  58. // don't display for integer number of frames
  59. float currentFrame;
  60. // cumulative, real-time duration of animation. Used for effects like selection border
  61. float elapsedTime;
  62. CCreatureAnim::EAnimType type; //type of animation being displayed
  63. // border color, disabled if alpha = 0
  64. SDL_Color border;
  65. TSpeedController speedController;
  66. bool once; // animation will be played once and the reset to idling
  67. ui8 * getPixelAddr(SDL_Surface * dest, int ftcpX, int ftcpY) const;
  68. template<int bpp>
  69. void putPixelAt(SDL_Surface * dest, int X, int Y, size_t index, const std::array<SDL_Color, 8> & special) const;
  70. template<int bpp>
  71. void putPixel( ui8 * dest, const SDL_Color & color, size_t index, const std::array<SDL_Color, 8> & special) const;
  72. template<int bpp>
  73. void nextFrameT(SDL_Surface * dest, bool rotate);
  74. void endAnimation();
  75. /// creates 8 special colors for current frame
  76. std::array<SDL_Color, 8> genSpecialPalette();
  77. public:
  78. // function(s) that will be called when animation ends, after reset to 1st frame
  79. // NOTE that these function will be fired only once
  80. CFunctionList<void()> onAnimationReset;
  81. int getWidth() const;
  82. int getHeight() const;
  83. /// Constructor
  84. /// name - path to .def file, relative to SPRITES/ directory
  85. /// controller - function that will return for how long *each* frame
  86. /// in specified group of animation should be played, measured in seconds
  87. CCreatureAnimation(std::string name, TSpeedController speedController); //c-tor
  88. void setType(CCreatureAnim::EAnimType type); //sets type of animation and cleares framecount
  89. CCreatureAnim::EAnimType getType() const; //returns type of animation
  90. void nextFrame(SDL_Surface * dest, bool rotate);
  91. // should be called every frame, return true when animation was reset to beginning
  92. bool incrementFrame(float timePassed);
  93. void setBorderColor(SDL_Color palette);
  94. float getCurrentFrame() const; // Gets the current frame ID relative to frame group.
  95. void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2
  96. int framesInGroup(CCreatureAnim::EAnimType group) const; //retirns number of fromes in given group
  97. void pause();
  98. void play();
  99. //helpers. TODO: move them somewhere else
  100. bool isDead() const;
  101. bool isIdle() const;
  102. bool isMoving() const;
  103. bool isShooting() const;
  104. };