Images.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Images.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 "../gui/CIntObject.h"
  12. #include "../battle/BattleConstants.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class Rect;
  15. VCMI_LIB_NAMESPACE_END
  16. struct SDL_Surface;
  17. struct SDL_Color;
  18. class CAnimImage;
  19. class CLabel;
  20. class CAnimation;
  21. class IImage;
  22. // Image class
  23. class CPicture : public CIntObject
  24. {
  25. std::shared_ptr<IImage> bg;
  26. public:
  27. /// if set, only specified section of internal image will be rendered
  28. std::optional<Rect> srcRect;
  29. /// If set to true, iamge will be redrawn on each frame
  30. bool needRefresh;
  31. /// If set to false, image will not be rendered
  32. /// Deprecated, use CIntObject::disable()/enable() instead
  33. bool visible;
  34. std::shared_ptr<IImage> getSurface()
  35. {
  36. return bg;
  37. }
  38. /// wrap existing image
  39. CPicture(std::shared_ptr<IImage> image, const Point & position);
  40. /// wrap section of an existing Image
  41. CPicture(std::shared_ptr<IImage> image, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
  42. /// Loads image from specified file name
  43. CPicture(const std::string & bmpname);
  44. CPicture(const std::string & bmpname, const Point & position);
  45. CPicture(const std::string & bmpname, int x, int y);
  46. /// set alpha value for whole surface. Note: may be messed up if surface is shared
  47. /// 0=transparent, 255=opaque
  48. void setAlpha(int value);
  49. void scaleTo(Point size);
  50. void colorize(PlayerColor player);
  51. void show(SDL_Surface * to) override;
  52. void showAll(SDL_Surface * to) override;
  53. };
  54. /// area filled with specific texture
  55. class CFilledTexture : public CIntObject
  56. {
  57. protected:
  58. std::shared_ptr<IImage> texture;
  59. Rect imageArea;
  60. public:
  61. CFilledTexture(std::string imageName, Rect position);
  62. CFilledTexture(std::shared_ptr<IImage> image, Rect position, Rect imageArea);
  63. void showAll(SDL_Surface *to) override;
  64. };
  65. class FilledTexturePlayerColored : public CFilledTexture
  66. {
  67. public:
  68. FilledTexturePlayerColored(std::string imageName, Rect position);
  69. void playerColored(PlayerColor player);
  70. };
  71. /// Class for displaying one image from animation
  72. class CAnimImage: public CIntObject
  73. {
  74. private:
  75. std::shared_ptr<CAnimation> anim;
  76. //displayed frame/group
  77. size_t frame;
  78. size_t group;
  79. ui8 flags;
  80. const Point scaledSize;
  81. /// If set, then image is colored using player-specific palette
  82. std::optional<PlayerColor> player;
  83. bool isScaled() const;
  84. void setSizeFromImage(const IImage &img);
  85. void init();
  86. public:
  87. bool visible;
  88. CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  89. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  90. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
  91. ~CAnimImage();
  92. /// size of animation
  93. size_t size();
  94. /// change displayed frame on this one
  95. void setFrame(size_t Frame, size_t Group=0);
  96. /// makes image player-colored to specific player
  97. void playerColored(PlayerColor player);
  98. /// returns true if image has player-colored effect applied
  99. bool isPlayerColored() const;
  100. void showAll(SDL_Surface * to) override;
  101. };
  102. /// Base class for displaying animation, used as superclass for different animations
  103. class CShowableAnim: public CIntObject
  104. {
  105. public:
  106. enum EFlags
  107. {
  108. BASE=1, //base frame will be blitted before current one
  109. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  110. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  111. PLAY_ONCE=32 //play animation only once and stop at last frame
  112. };
  113. protected:
  114. std::shared_ptr<CAnimation> anim;
  115. size_t group, frame;//current frame
  116. size_t first, last; //animation range
  117. /// total time on scren for each frame in animation
  118. ui32 frameTimeTotal;
  119. /// how long was current frame visible on screen
  120. ui32 frameTimePassed;
  121. ui8 flags;//Flags from EFlags enum
  122. //blit image with optional rotation, fitting into rect, etc
  123. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  124. //For clipping in rect, offsets of picture coordinates
  125. int xOffset, yOffset;
  126. ui8 alpha;
  127. public:
  128. //called when next animation sequence is required
  129. std::function<void()> callback;
  130. //Set per-surface alpha, 0 = transparent, 255 = opaque
  131. void setAlpha(ui32 alphaValue);
  132. CShowableAnim(int x, int y, std::string name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
  133. ~CShowableAnim();
  134. //set animation to group or part of group
  135. bool set(size_t Group);
  136. bool set(size_t Group, size_t from, size_t to=-1);
  137. //set rotation flags
  138. void rotate(bool on, bool vertical=false);
  139. //move displayed part of picture (if picture is clipped to rect)
  140. void clipRect(int posX, int posY, int width, int height);
  141. //set frame to first, call callback
  142. virtual void reset();
  143. //set animation duration
  144. void setDuration(int durationMs);
  145. //show current frame and increase counter
  146. void show(SDL_Surface * to) override;
  147. void showAll(SDL_Surface * to) override;
  148. void tick(uint32_t msPassed) override;
  149. };
  150. /// Creature-dependend animations like attacking, moving,...
  151. class CCreatureAnim: public CShowableAnim
  152. {
  153. private:
  154. //queue of animations waiting to be displayed
  155. std::queue<ECreatureAnimType> queue;
  156. //this function is used as callback if preview flag was set during construction
  157. void loopPreview(bool warMachine);
  158. public:
  159. //change anim to next if queue is not empty, call callback othervice
  160. void reset() override;
  161. //add sequence to the end of queue
  162. void addLast(ECreatureAnimType newType);
  163. void startPreview(bool warMachine);
  164. //clear queue and set animation to this sequence
  165. void clearAndSet(ECreatureAnimType type);
  166. CCreatureAnim(int x, int y, std::string name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  167. };