CCreatureAnimation.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #include "../FunctionList.h"
  3. //#include "../CDefHandler.h"
  4. #include "../CAnimation.h"
  5. /*
  6. * CCreatureAnimation.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  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
  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 duration of full movement animation, in seconds. Needed to move animation on screen
  31. float getMovementDuration(const CCreature * creature);
  32. /// Returns distance on which flying creatures should during one animation loop
  33. float getFlightDistance(const CCreature * creature);
  34. }
  35. /// Class which manages animations of creatures/units inside battles
  36. /// TODO: split into constant image container and class that does *control* of animation
  37. class CCreatureAnimation : public CIntObject
  38. {
  39. public:
  40. typedef boost::function<float(CCreatureAnimation *, size_t)> TSpeedController;
  41. private:
  42. std::string defName;
  43. int fullWidth, fullHeight;
  44. // palette, as read from def file
  45. std::array<SDL_Color, 256> palette;
  46. //key = id of group (note that some groups may be missing)
  47. //value = offset of pixel data for each frame, vector size = number of frames in group
  48. std::map<int, std::vector<unsigned int>> dataOffsets;
  49. //animation raw data
  50. //TODO: use vector instead?
  51. unique_ptr<ui8[]> pixelData;
  52. size_t pixelDataSize;
  53. // speed of animation, measure in frames per second
  54. float speed;
  55. // currently displayed frame. Float to allow H3-style animations where frames
  56. // don't display for integer number of frames
  57. float currentFrame;
  58. // cumulative, real-time duration of animation. Used for effects like selection border
  59. float elapsedTime;
  60. CCreatureAnim::EAnimType type; //type of animation being displayed
  61. // border color, disabled if alpha = 0
  62. SDL_Color border;
  63. TSpeedController speedController;
  64. bool once; // animation will be played once and the reset to idling
  65. ui8 * getPixelAddr(SDL_Surface * dest, int ftcpX, int ftcpY) const;
  66. template<int bpp>
  67. void putPixelAt(SDL_Surface * dest, int X, int Y, size_t index, const std::array<SDL_Color, 8> & special, SDL_Rect * destRect = nullptr) const;
  68. template<int bpp>
  69. void putPixel( ui8 * dest, const SDL_Color & color, size_t index, const std::array<SDL_Color, 8> & special) const;
  70. template<int bpp>
  71. void nextFrameT(SDL_Surface * dest, int x, int y, bool rotate, SDL_Rect * destRect = nullptr);
  72. void endAnimation();
  73. /// creates 8 special colors for current frame
  74. std::array<SDL_Color, 8> genSpecialPalette();
  75. public:
  76. // function(s) that will be called when animation ends, after reset to 1st frame
  77. // NOTE that these function will be fired only once
  78. CFunctionList<void()> onAnimationReset;
  79. int getWidth() const;
  80. int getHeight() const;
  81. /// Constructor
  82. /// name - path to .def file, relative to SPRITES/ directory
  83. /// controller - function that will return for how long *each* frame
  84. /// in specified group of animation should be played, measured in seconds
  85. CCreatureAnimation(std::string name, TSpeedController speedController); //c-tor
  86. void setType(CCreatureAnim::EAnimType type); //sets type of animation and cleares framecount
  87. CCreatureAnim::EAnimType getType() const; //returns type of animation
  88. void nextFrame(SDL_Surface * dest, int x, int y, bool rotate, SDL_Rect * destRect = nullptr);
  89. // should be called every frame, return true when animation was reset to beginning
  90. bool incrementFrame(float timePassed);
  91. void setBorderColor(SDL_Color palette);
  92. float getCurrentFrame() const; // Gets the current frame ID relative to frame group.
  93. void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2
  94. int framesInGroup(CCreatureAnim::EAnimType group) const; //retirns number of fromes in given group
  95. void pause();
  96. void play();
  97. //helpers. TODO: move them somewhere else
  98. bool isDead() const;
  99. bool isIdle() const;
  100. };