Images.h 6.3 KB

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