Images.h 6.1 KB

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