Images.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. struct SDL_Surface;
  14. struct Rect;
  15. class CAnimImage;
  16. class CLabel;
  17. class CAnimation;
  18. // Image class
  19. class CPicture : public CIntObject
  20. {
  21. void setSurface(SDL_Surface * to);
  22. public:
  23. SDL_Surface * bg;
  24. Rect * srcRect; //if nullptr then whole surface will be used
  25. bool freeSurf; //whether surface will be freed upon CPicture destruction
  26. bool needRefresh; //Surface needs to be displayed each frame
  27. bool visible;
  28. operator SDL_Surface *()
  29. {
  30. return bg;
  31. }
  32. CPicture(const Rect & r, const SDL_Color & color, bool screenFormat = false); //rect filled with given color
  33. CPicture(const Rect & r, ui32 color, bool screenFormat = false); //rect filled with given color
  34. CPicture(SDL_Surface * BG, int x = 0, int y = 0, bool Free = true); //wrap existing SDL_Surface
  35. CPicture(const std::string & bmpname, int x = 0, int y = 0);
  36. CPicture(SDL_Surface * BG, const Rect & SrcRext, int x = 0, int y = 0, bool free = false); //wrap subrect of given surface
  37. ~CPicture();
  38. void init();
  39. //set alpha value for whole surface. Note: may be messed up if surface is shared
  40. // 0=transparent, 255=opaque
  41. void setAlpha(int value);
  42. void scaleTo(Point size);
  43. void createSimpleRect(const Rect & r, bool screenFormat, ui32 color);
  44. void show(SDL_Surface * to) override;
  45. void showAll(SDL_Surface * to) override;
  46. void convertToScreenBPP();
  47. void colorizeAndConvert(PlayerColor player);
  48. void colorize(PlayerColor player);
  49. };
  50. /// area filled with specific texture
  51. class CFilledTexture : CIntObject
  52. {
  53. SDL_Surface * texture;
  54. public:
  55. CFilledTexture(std::string imageName, Rect position);
  56. ~CFilledTexture();
  57. void showAll(SDL_Surface * to) override;
  58. };
  59. /// Class for displaying one image from animation
  60. class CAnimImage : public CIntObject
  61. {
  62. private:
  63. std::shared_ptr<CAnimation> anim;
  64. //displayed frame/group
  65. size_t frame;
  66. size_t group;
  67. PlayerColor player;
  68. ui8 flags;
  69. void init();
  70. public:
  71. bool visible;
  72. CAnimImage(const std::string & name, size_t Frame, size_t Group = 0, int x = 0, int y = 0, ui8 Flags = 0);
  73. CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group = 0, int x = 0, int y = 0, ui8 Flags = 0);
  74. ~CAnimImage();
  75. //size of animation
  76. size_t size();
  77. //change displayed frame on this one
  78. void setFrame(size_t Frame, size_t Group = 0);
  79. //makes image player-colored
  80. void playerColored(PlayerColor player);
  81. void showAll(SDL_Surface * to) override;
  82. };
  83. /// Base class for displaying animation, used as superclass for different animations
  84. class CShowableAnim : public CIntObject
  85. {
  86. public:
  87. enum EFlags
  88. {
  89. BASE=1,
  90. //base frame will be blitted before current one
  91. HORIZONTAL_FLIP=2,
  92. //TODO: will be displayed rotated
  93. VERTICAL_FLIP=4,
  94. //TODO: will be displayed rotated
  95. USE_RLE=8,
  96. //RLE-d version, support full alpha-channel for 8-bit images
  97. PLAYER_COLORED=16,
  98. //TODO: all loaded images will be player-colored
  99. PLAY_ONCE=32 //play animation only once and stop at last frame
  100. };
  101. protected:
  102. CAnimation * anim;
  103. size_t group, frame; //current frame
  104. size_t first, last; //animation range
  105. //TODO: replace with time delay(needed for battles)
  106. ui32 frameDelay; //delay in frames of each image
  107. ui32 value; //how many times current frame was showed
  108. ui8 flags; //Flags from EFlags enum
  109. //blit image with optional rotation, fitting into rect, etc
  110. void blitImage(size_t frame, size_t group, SDL_Surface * to);
  111. //For clipping in rect, offsets of picture coordinates
  112. int xOffset, yOffset;
  113. ui8 alpha;
  114. public:
  115. //called when next animation sequence is required
  116. std::function<void()> callback;
  117. //Set per-surface alpha, 0 = transparent, 255 = opaque
  118. void setAlpha(ui32 alphaValue);
  119. CShowableAnim(int x, int y, std::string name, ui8 flags = 0, ui32 Delay = 4, size_t Group = 0);
  120. ~CShowableAnim();
  121. //set animation to group or part of group
  122. bool set(size_t Group);
  123. bool set(size_t Group, size_t from, size_t to = -1);
  124. //set rotation flags
  125. void rotate(bool on, bool vertical = false);
  126. //move displayed part of picture (if picture is clipped to rect)
  127. void clipRect(int posX, int posY, int width, int height);
  128. //set frame to first, call callback
  129. virtual void reset();
  130. //show current frame and increase counter
  131. void show(SDL_Surface * to) override;
  132. void showAll(SDL_Surface * to) override;
  133. };
  134. /// Creature-dependend animations like attacking, moving,...
  135. class CCreatureAnim : public CShowableAnim
  136. {
  137. public:
  138. enum EHeroAnimType
  139. {
  140. HERO_HOLDING = 0,
  141. HERO_IDLE = 1,
  142. // idling movement that happens from time to time
  143. HERO_DEFEAT = 2,
  144. // played when army loses stack or on friendly fire
  145. HERO_VICTORY = 3,
  146. // when enemy stack killed or huge damage is dealt
  147. HERO_CAST_SPELL = 4 // spellcasting
  148. };
  149. enum EAnimType // list of creature animations, numbers were taken from def files
  150. {
  151. MOVING=0,
  152. MOUSEON=1,
  153. HOLDING=2,
  154. HITTED=3,
  155. DEFENCE=4,
  156. DEATH=5,
  157. //DEATH2=6, //unused?
  158. TURN_L=7,
  159. TURN_R=8,
  160. //same
  161. //TURN_L2=9, //identical to previous?
  162. //TURN_R2=10,
  163. ATTACK_UP=11,
  164. ATTACK_FRONT=12,
  165. ATTACK_DOWN=13,
  166. SHOOT_UP=14,
  167. SHOOT_FRONT=15,
  168. SHOOT_DOWN=16,
  169. CAST_UP=17,
  170. CAST_FRONT=18,
  171. CAST_DOWN=19,
  172. MOVE_START=20,
  173. MOVE_END=21,
  174. DEAD = 22 // new group, used to show dead stacks. If empty - last frame from "DEATH" will be copied here
  175. };
  176. private:
  177. //queue of animations waiting to be displayed
  178. std::queue<EAnimType> queue;
  179. //this function is used as callback if preview flag was set during construction
  180. void loopPreview(bool warMachine);
  181. public:
  182. //change anim to next if queue is not empty, call callback othervice
  183. void reset() override;
  184. //add sequence to the end of queue
  185. void addLast(EAnimType newType);
  186. void startPreview(bool warMachine);
  187. //clear queue and set animation to this sequence
  188. void clearAndSet(EAnimType type);
  189. CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags = USE_RLE, EAnimType = HOLDING);
  190. };