CCreatureAnimation.h 4.7 KB

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