Images.h 5.3 KB

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