Images.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <SDL_render.h>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class Rect;
  16. VCMI_LIB_NAMESPACE_END
  17. struct SDL_Surface;
  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. public:
  27. SDL_Surface * bg;
  28. Rect * srcRect; //if nullptr then whole surface will be used
  29. bool freeSurf; //whether surface will be freed upon CPicture destruction
  30. bool needRefresh;//Surface needs to be displayed each frame
  31. bool visible;
  32. SDL_Surface * getSurface()
  33. {
  34. return bg;
  35. }
  36. CPicture(const Rect & r, const SDL_Color & color, bool screenFormat = false); //rect filled with given color
  37. CPicture(const Rect & r, ui32 color, bool screenFormat = false); //rect filled with given color
  38. CPicture(SDL_Surface * BG, int x = 0, int y=0, bool Free = true); //wrap existing SDL_Surface
  39. CPicture(const std::string &bmpname, int x=0, int y=0);
  40. CPicture(SDL_Surface *BG, const Rect &SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  41. ~CPicture();
  42. void init();
  43. //set alpha value for whole surface. Note: may be messed up if surface is shared
  44. // 0=transparent, 255=opaque
  45. void setAlpha(int value);
  46. void scaleTo(Point size);
  47. void createSimpleRect(const Rect &r, bool screenFormat, ui32 color);
  48. void show(SDL_Surface * to) override;
  49. void showAll(SDL_Surface * to) override;
  50. void convertToScreenBPP();
  51. void colorize(PlayerColor player);
  52. };
  53. /// area filled with specific texture
  54. class CFilledTexture : public CIntObject
  55. {
  56. SDL_Surface * texture;
  57. public:
  58. CFilledTexture(std::string imageName, Rect position);
  59. ~CFilledTexture();
  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. //TODO: replace with time delay(needed for battles)
  107. ui32 frameDelay;//delay in frames of each image
  108. ui32 value;//how many times current frame was showed
  109. ui8 flags;//Flags from EFlags enum
  110. //blit image with optional rotation, fitting into rect, etc
  111. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  112. //For clipping in rect, offsets of picture coordinates
  113. int xOffset, yOffset;
  114. ui8 alpha;
  115. public:
  116. //called when next animation sequence is required
  117. std::function<void()> callback;
  118. //Set per-surface alpha, 0 = transparent, 255 = opaque
  119. void setAlpha(ui32 alphaValue);
  120. CShowableAnim(int x, int y, std::string name, ui8 flags=0, ui32 Delay=4, size_t Group=0, uint8_t alpha = UINT8_MAX);
  121. ~CShowableAnim();
  122. //set animation to group or part of group
  123. bool set(size_t Group);
  124. bool set(size_t Group, size_t from, size_t to=-1);
  125. //set rotation flags
  126. void rotate(bool on, bool vertical=false);
  127. //move displayed part of picture (if picture is clipped to rect)
  128. void clipRect(int posX, int posY, int width, int height);
  129. //set frame to first, call callback
  130. virtual void reset();
  131. //show current frame and increase counter
  132. void show(SDL_Surface * to) override;
  133. void showAll(SDL_Surface * to) override;
  134. };
  135. /// Creature-dependend animations like attacking, moving,...
  136. class CCreatureAnim: public CShowableAnim
  137. {
  138. private:
  139. //queue of animations waiting to be displayed
  140. std::queue<ECreatureAnimType> queue;
  141. //this function is used as callback if preview flag was set during construction
  142. void loopPreview(bool warMachine);
  143. public:
  144. //change anim to next if queue is not empty, call callback othervice
  145. void reset() override;
  146. //add sequence to the end of queue
  147. void addLast(ECreatureAnimType newType);
  148. void startPreview(bool warMachine);
  149. //clear queue and set animation to this sequence
  150. void clearAndSet(ECreatureAnimType type);
  151. CCreatureAnim(int x, int y, std::string name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  152. };