Images.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #pragma once
  2. #include "../gui/CIntObject.h"
  3. #include "../gui/SDL_Extensions.h"
  4. struct SDL_Surface;
  5. struct Rect;
  6. class CAnimImage;
  7. class CLabel;
  8. class CAnimation;
  9. /*
  10. * Images.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  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();//d-tor
  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, //base frame will be blitted before current one
  90. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  91. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  92. USE_RLE=8, //RLE-d version, support full alpha-channel for 8-bit images
  93. PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
  94. PLAY_ONCE=32 //play animation only once and stop at last frame
  95. };
  96. protected:
  97. CAnimation * anim;
  98. size_t group, frame;//current frame
  99. size_t first, last; //animation range
  100. //TODO: replace with time delay(needed for battles)
  101. ui32 frameDelay;//delay in frames of each image
  102. ui32 value;//how many times current frame was showed
  103. ui8 flags;//Flags from EFlags enum
  104. //blit image with optional rotation, fitting into rect, etc
  105. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  106. //For clipping in rect, offsets of picture coordinates
  107. int xOffset, yOffset;
  108. ui8 alpha;
  109. public:
  110. //called when next animation sequence is required
  111. std::function<void()> callback;
  112. //Set per-surface alpha, 0 = transparent, 255 = opaque
  113. void setAlpha(ui32 alphaValue);
  114. CShowableAnim(int x, int y, std::string name, ui8 flags=0, ui32 Delay=4, size_t Group=0);
  115. ~CShowableAnim();
  116. //set animation to group or part of group
  117. bool set(size_t Group);
  118. bool set(size_t Group, size_t from, size_t to=-1);
  119. //set rotation flags
  120. void rotate(bool on, bool vertical=false);
  121. //move displayed part of picture (if picture is clipped to rect)
  122. void clipRect(int posX, int posY, int width, int height);
  123. //set frame to first, call callback
  124. virtual void reset();
  125. //show current frame and increase counter
  126. void show(SDL_Surface * to) override;
  127. void showAll(SDL_Surface * to) override;
  128. };
  129. /// Creature-dependend animations like attacking, moving,...
  130. class CCreatureAnim: public CShowableAnim
  131. {
  132. public:
  133. enum EHeroAnimType
  134. {
  135. HERO_HOLDING = 0,
  136. HERO_IDLE = 1, // idling movement that happens from time to time
  137. HERO_DEFEAT = 2, // played when army loses stack or on friendly fire
  138. HERO_VICTORY = 3, // when enemy stack killed or huge damage is dealt
  139. HERO_CAST_SPELL = 4 // spellcasting
  140. };
  141. enum EAnimType // list of creature animations, numbers were taken from def files
  142. {
  143. MOVING=0,
  144. MOUSEON=1,
  145. HOLDING=2,
  146. HITTED=3,
  147. DEFENCE=4,
  148. DEATH=5,
  149. //DEATH2=6, //unused?
  150. TURN_L=7,
  151. TURN_R=8, //same
  152. //TURN_L2=9, //identical to previous?
  153. //TURN_R2=10,
  154. ATTACK_UP=11,
  155. ATTACK_FRONT=12,
  156. ATTACK_DOWN=13,
  157. SHOOT_UP=14,
  158. SHOOT_FRONT=15,
  159. SHOOT_DOWN=16,
  160. CAST_UP=17,
  161. CAST_FRONT=18,
  162. CAST_DOWN=19,
  163. MOVE_START=20,
  164. MOVE_END=21,
  165. DEAD = 22 // new group, used to show dead stacks. If empty - last frame from "DEATH" will be copied here
  166. };
  167. private:
  168. //queue of animations waiting to be displayed
  169. std::queue<EAnimType> queue;
  170. //this function is used as callback if preview flag was set during construction
  171. void loopPreview(bool warMachine);
  172. public:
  173. //change anim to next if queue is not empty, call callback othervice
  174. void reset() override;
  175. //add sequence to the end of queue
  176. void addLast(EAnimType newType);
  177. void startPreview(bool warMachine);
  178. //clear queue and set animation to this sequence
  179. void clearAndSet(EAnimType type);
  180. CCreatureAnim(int x, int y, std::string name, Rect picPos,
  181. ui8 flags= USE_RLE, EAnimType = HOLDING );
  182. };