Images.h 5.3 KB

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