Images.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. std::shared_ptr<IImage> texture;
  58. Rect imageArea;
  59. public:
  60. CFilledTexture(std::string imageName, Rect position);
  61. CFilledTexture(std::shared_ptr<IImage> image, Rect position, Rect imageArea);
  62. void showAll(SDL_Surface *to) override;
  63. };
  64. /// Class for displaying one image from animation
  65. class CAnimImage: public CIntObject
  66. {
  67. private:
  68. std::shared_ptr<CAnimation> anim;
  69. //displayed frame/group
  70. size_t frame;
  71. size_t group;
  72. PlayerColor player;
  73. ui8 flags;
  74. const Point scaledSize;
  75. bool isScaled() const;
  76. void setSizeFromImage(const IImage &img);
  77. void init();
  78. public:
  79. bool visible;
  80. CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  81. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  82. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
  83. ~CAnimImage();
  84. //size of animation
  85. size_t size();
  86. //change displayed frame on this one
  87. void setFrame(size_t Frame, size_t Group=0);
  88. //makes image player-colored
  89. void playerColored(PlayerColor player);
  90. void showAll(SDL_Surface * to) override;
  91. };
  92. /// Base class for displaying animation, used as superclass for different animations
  93. class CShowableAnim: public CIntObject
  94. {
  95. public:
  96. enum EFlags
  97. {
  98. BASE=1, //base frame will be blitted before current one
  99. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  100. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  101. PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
  102. PLAY_ONCE=32 //play animation only once and stop at last frame
  103. };
  104. protected:
  105. std::shared_ptr<CAnimation> anim;
  106. size_t group, frame;//current frame
  107. size_t first, last; //animation range
  108. /// total time on scren for each frame in animation
  109. ui32 frameTimeTotal;
  110. /// how long was current frame visible on screen
  111. ui32 frameTimePassed;
  112. ui8 flags;//Flags from EFlags enum
  113. //blit image with optional rotation, fitting into rect, etc
  114. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  115. //For clipping in rect, offsets of picture coordinates
  116. int xOffset, yOffset;
  117. ui8 alpha;
  118. public:
  119. //called when next animation sequence is required
  120. std::function<void()> callback;
  121. //Set per-surface alpha, 0 = transparent, 255 = opaque
  122. void setAlpha(ui32 alphaValue);
  123. CShowableAnim(int x, int y, std::string name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
  124. ~CShowableAnim();
  125. //set animation to group or part of group
  126. bool set(size_t Group);
  127. bool set(size_t Group, size_t from, size_t to=-1);
  128. //set rotation flags
  129. void rotate(bool on, bool vertical=false);
  130. //move displayed part of picture (if picture is clipped to rect)
  131. void clipRect(int posX, int posY, int width, int height);
  132. //set frame to first, call callback
  133. virtual void reset();
  134. //set animation duration
  135. void setDuration(int durationMs);
  136. //show current frame and increase counter
  137. void show(SDL_Surface * to) override;
  138. void showAll(SDL_Surface * to) override;
  139. };
  140. /// Creature-dependend animations like attacking, moving,...
  141. class CCreatureAnim: public CShowableAnim
  142. {
  143. private:
  144. //queue of animations waiting to be displayed
  145. std::queue<ECreatureAnimType> queue;
  146. //this function is used as callback if preview flag was set during construction
  147. void loopPreview(bool warMachine);
  148. public:
  149. //change anim to next if queue is not empty, call callback othervice
  150. void reset() override;
  151. //add sequence to the end of queue
  152. void addLast(ECreatureAnimType newType);
  153. void startPreview(bool warMachine);
  154. //clear queue and set animation to this sequence
  155. void clearAndSet(ECreatureAnimType type);
  156. CCreatureAnim(int x, int y, std::string name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  157. };