Images.h 5.9 KB

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