Images.h 5.3 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. void setSurface(SDL_Surface *to);
  26. SDL_Surface * bg;
  27. void convertToScreenBPP();
  28. public:
  29. /// if set, only specified section of internal image will be rendered
  30. boost::optional<Rect> srcRect;
  31. /// If set to true, iamge will be redrawn on each frame
  32. bool needRefresh;
  33. /// If set to false, image will not be rendered
  34. /// Deprecated, use CIntObject::disable()/enable() instead
  35. bool visible;
  36. SDL_Surface * getSurface()
  37. {
  38. return bg;
  39. }
  40. /// wrap existing SDL_Surface
  41. /// deprecated, do not use
  42. CPicture(SDL_Surface * BG, const Point & position);
  43. /// wrap section of existing SDL_Surface
  44. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
  45. /// Loads image from specified file name
  46. CPicture(const std::string & bmpname);
  47. CPicture(const std::string & bmpname, const Point & position);
  48. CPicture(const std::string & bmpname, int x, int y);
  49. ~CPicture();
  50. /// set alpha value for whole surface. Note: may be messed up if surface is shared
  51. /// 0=transparent, 255=opaque
  52. void setAlpha(int value);
  53. void scaleTo(Point size);
  54. void colorize(PlayerColor player);
  55. void show(SDL_Surface * to) override;
  56. void showAll(SDL_Surface * to) override;
  57. };
  58. /// area filled with specific texture
  59. class CFilledTexture : public CIntObject
  60. {
  61. SDL_Surface * texture;
  62. public:
  63. CFilledTexture(std::string imageName, Rect position);
  64. ~CFilledTexture();
  65. void showAll(SDL_Surface *to) override;
  66. };
  67. /// Class for displaying one image from animation
  68. class CAnimImage: public CIntObject
  69. {
  70. private:
  71. std::shared_ptr<CAnimation> anim;
  72. //displayed frame/group
  73. size_t frame;
  74. size_t group;
  75. PlayerColor player;
  76. ui8 flags;
  77. const Point scaledSize;
  78. bool isScaled() const;
  79. void setSizeFromImage(const IImage &img);
  80. void init();
  81. public:
  82. bool visible;
  83. CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  84. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  85. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
  86. ~CAnimImage();
  87. //size of animation
  88. size_t size();
  89. //change displayed frame on this one
  90. void setFrame(size_t Frame, size_t Group=0);
  91. //makes image player-colored
  92. void playerColored(PlayerColor player);
  93. void showAll(SDL_Surface * to) override;
  94. };
  95. /// Base class for displaying animation, used as superclass for different animations
  96. class CShowableAnim: public CIntObject
  97. {
  98. public:
  99. enum EFlags
  100. {
  101. BASE=1, //base frame will be blitted before current one
  102. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  103. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  104. PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
  105. PLAY_ONCE=32 //play animation only once and stop at last frame
  106. };
  107. protected:
  108. std::shared_ptr<CAnimation> anim;
  109. size_t group, frame;//current frame
  110. size_t first, last; //animation range
  111. //TODO: replace with time delay(needed for battles)
  112. ui32 frameDelay;//delay in frames of each image
  113. ui32 value;//how many times current frame was showed
  114. ui8 flags;//Flags from EFlags enum
  115. //blit image with optional rotation, fitting into rect, etc
  116. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  117. //For clipping in rect, offsets of picture coordinates
  118. int xOffset, yOffset;
  119. ui8 alpha;
  120. public:
  121. //called when next animation sequence is required
  122. std::function<void()> callback;
  123. //Set per-surface alpha, 0 = transparent, 255 = opaque
  124. void setAlpha(ui32 alphaValue);
  125. CShowableAnim(int x, int y, std::string name, ui8 flags=0, ui32 Delay=4, size_t Group=0, uint8_t alpha = UINT8_MAX);
  126. ~CShowableAnim();
  127. //set animation to group or part of group
  128. bool set(size_t Group);
  129. bool set(size_t Group, size_t from, size_t to=-1);
  130. //set rotation flags
  131. void rotate(bool on, bool vertical=false);
  132. //move displayed part of picture (if picture is clipped to rect)
  133. void clipRect(int posX, int posY, int width, int height);
  134. //set frame to first, call callback
  135. virtual void reset();
  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. };