CAnimation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #pragma once
  2. #include "GUIBase.h"
  3. /*
  4. * CAnimation.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. struct SDL_Surface;
  13. class SDLImageLoader;
  14. class CompImageLoader;
  15. class JsonNode;
  16. /// Class for def loading, methods are based on CDefHandler
  17. /// After loading will store general info (palette and frame offsets) and pointer to file itself
  18. class CDefFile
  19. {
  20. private:
  21. struct SSpriteDef
  22. {
  23. ui32 size;
  24. ui32 format; /// format in which pixel data is stored
  25. ui32 fullWidth; /// full width and height of frame, including borders
  26. ui32 fullHeight;
  27. ui32 width; /// width and height of pixel data, borders excluded
  28. ui32 height;
  29. si32 leftMargin;
  30. si32 topMargin;
  31. };
  32. //offset[group][frame] - offset of frame data in file
  33. std::map<size_t, std::vector <size_t> > offset;
  34. ui8 * data;
  35. SDL_Color * palette;
  36. public:
  37. CDefFile(std::string Name);
  38. ~CDefFile();
  39. //load frame as SDL_Surface
  40. template<class ImageLoader>
  41. void loadFrame(size_t frame, size_t group, ImageLoader &loader) const;
  42. const std::map<size_t, size_t> getEntries() const;
  43. };
  44. /*
  45. * Base class for images, can be used for non-animation pictures as well
  46. */
  47. class IImage
  48. {
  49. int refCount;
  50. public:
  51. //draws image on surface "where" at position
  52. virtual void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=NULL, ui8 alpha=255) const=0;
  53. //decrease ref count, returns true if image can be deleted (refCount <= 0)
  54. bool decreaseRef();
  55. void increaseRef();
  56. //Change palette to specific player
  57. virtual void playerColored(int player)=0;
  58. virtual int width() const=0;
  59. virtual int height() const=0;
  60. IImage();
  61. virtual ~IImage() {};
  62. };
  63. /*
  64. * Wrapper around SDL_Surface
  65. */
  66. class SDLImage : public IImage
  67. {
  68. public:
  69. //Surface without empty borders
  70. SDL_Surface * surf;
  71. //size of left and top borders
  72. Point margins;
  73. //total size including borders
  74. Point fullSize;
  75. public:
  76. //Load image from def file
  77. SDLImage(CDefFile *data, size_t frame, size_t group=0, bool compressed=false);
  78. //Load from bitmap file
  79. SDLImage(std::string filename, bool compressed=false);
  80. //Create using existing surface, extraRef will increase refcount on SDL_Surface
  81. SDLImage(SDL_Surface * from, bool extraRef);
  82. ~SDLImage();
  83. void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=NULL, ui8 alpha=255) const;
  84. void playerColored(int player);
  85. int width() const;
  86. int height() const;
  87. friend class SDLImageLoader;
  88. };
  89. /*
  90. * RLE-compressed image data for 8-bit images with alpha-channel, currently far from finished
  91. * primary purpose is not high compression ratio but fast drawing.
  92. * Consist of repeatable segments with format similar to H3 def compression:
  93. * 1st byte:
  94. * if (byte == 0xff)
  95. * raw data, opaque and semi-transparent data always in separate blocks
  96. * else
  97. * RLE-compressed image data with this color
  98. * 2nd byte = size of segment
  99. * raw data (if any)
  100. */
  101. class CompImage : public IImage
  102. {
  103. //x,y - margins, w,h - sprite size
  104. Rect sprite;
  105. //total size including borders
  106. Point fullSize;
  107. //RLE-d data
  108. ui8 * surf;
  109. //array of offsets for each line
  110. ui32 * line;
  111. //palette
  112. SDL_Color *palette;
  113. //Used internally to blit one block of data
  114. template<int bpp, int dir>
  115. void BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha) const;
  116. void BlitBlockWithBpp(ui8 bpp, ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha, bool rotated) const;
  117. public:
  118. //Load image from def file
  119. CompImage(const CDefFile *data, size_t frame, size_t group=0);
  120. //TODO: load image from SDL_Surface
  121. CompImage(SDL_Surface * surf);
  122. ~CompImage();
  123. void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=NULL, ui8 alpha=255) const;
  124. void playerColored(int player);
  125. int width() const;
  126. int height() const;
  127. friend class CompImageLoader;
  128. };
  129. /// Class for handling animation
  130. class CAnimation
  131. {
  132. private:
  133. //source[group][position] - file with this frame, if string is empty - image located in def file
  134. std::map<size_t, std::vector <JsonNode> > source;
  135. //bitmap[group][position], store objects with loaded bitmaps
  136. std::map<size_t, std::map<size_t, IImage* > > images;
  137. //animation file name
  138. std::string name;
  139. //if true all frames will be stored in compressed (RLE) state
  140. const bool compressed;
  141. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  142. bool loadFrame(CDefFile * file, size_t frame, size_t group);
  143. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  144. bool unloadFrame(size_t frame, size_t group);
  145. //initialize animation from file
  146. void init(CDefFile * file);
  147. //try to open def file
  148. CDefFile * getFile() const;
  149. //to get rid of copy-pasting error message :]
  150. void printError(size_t frame, size_t group, std::string type) const;
  151. //not a very nice method to get image from another def file
  152. //TODO: remove after implementing resource manager
  153. IImage * getFromExtraDef(std::string filename);
  154. public:
  155. CAnimation(std::string Name, bool Compressed = false);
  156. CAnimation();
  157. ~CAnimation();
  158. //static method for debugging - print info about loaded animations in tlog1
  159. static void getAnimInfo();
  160. static std::set<CAnimation*> loadedAnims;
  161. //add custom surface to the selected position.
  162. void setCustom(std::string filename, size_t frame, size_t group=0);
  163. //get pointer to image from specific group, NULL if not found
  164. IImage * getImage(size_t frame, size_t group=0, bool verbose=true) const;
  165. //all available frames
  166. void load ();
  167. void unload();
  168. //all frames from group
  169. void loadGroup (size_t group);
  170. void unloadGroup(size_t group);
  171. //single image
  172. void load (size_t frame, size_t group=0);
  173. void unload(size_t frame, size_t group=0);
  174. //total count of frames in group (including not loaded)
  175. size_t size(size_t group=0) const;
  176. };
  177. /// Class for displaying one image from animation
  178. class CAnimImage: public CIntObject
  179. {
  180. private:
  181. CAnimation* anim;
  182. //displayed frame/group
  183. size_t frame;
  184. size_t group;
  185. int player;
  186. ui8 flags;
  187. void init();
  188. public:
  189. CAnimImage(std::string name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  190. CAnimImage(CAnimation* anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  191. ~CAnimImage();//d-tor
  192. //size of animation
  193. size_t size();
  194. //change displayed frame on this one
  195. void setFrame(size_t Frame, size_t Group=0);
  196. //makes image player-colored
  197. void playerColored(int player);
  198. void showAll(SDL_Surface *to);
  199. };
  200. /// Base class for displaying animation, used as superclass for different animations
  201. class CShowableAnim: public CIntObject
  202. {
  203. public:
  204. enum EFlags
  205. {
  206. BASE=1, //base frame will be blitted before current one
  207. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  208. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  209. USE_RLE=8, //RLE-d version, support full alpha-channel for 8-bit images
  210. PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
  211. };
  212. protected:
  213. CAnimation anim;
  214. size_t group, frame;//current frame
  215. size_t first, last; //animation range
  216. //TODO: replace with time delay(needed for battles)
  217. ui32 frameDelay;//delay in frames of each image
  218. ui32 value;//how many times current frame was showed
  219. ui8 flags;//Flags from EFlags enum
  220. //blit image with optional rotation, fitting into rect, etc
  221. void blitImage(size_t frame, size_t group, SDL_Surface *to);
  222. //For clipping in rect, offsets of picture coordinates
  223. int xOffset, yOffset;
  224. ui8 alpha;
  225. public:
  226. //called when next animation sequence is required
  227. boost::function<void()> callback;
  228. //Set per-surface alpha, 0 = transparent, 255 = opaque
  229. void setAlpha(ui32 alphaValue);
  230. CShowableAnim(int x, int y, std::string name, ui8 flags=0, ui32 Delay=4, size_t Group=0);
  231. ~CShowableAnim();
  232. //set animation to group or part of group
  233. bool set(size_t Group);
  234. bool set(size_t Group, size_t from, size_t to=-1);
  235. //set rotation flags
  236. void rotate(bool on, bool vertical=false);
  237. //move displayed part of picture (if picture is clipped to rect)
  238. void clipRect(int posX, int posY, int width, int height);
  239. //set frame to first, call callback
  240. virtual void reset();
  241. //show current frame and increase counter
  242. void show(SDL_Surface *to);
  243. void showAll(SDL_Surface *to);
  244. };
  245. /// Creature-dependend animations like attacking, moving,...
  246. class CCreatureAnim: public CShowableAnim
  247. {
  248. public:
  249. enum EAnimType // list of creature animations, numbers were taken from def files
  250. {
  251. WHOLE_ANIM=-1, //just for convenience
  252. MOVING=0, //will automatically add MOVE_START and MOVE_END to queue
  253. MOUSEON=1, //rename to IDLE
  254. HOLDING=2, //rename to STANDING
  255. HITTED=3,
  256. DEFENCE=4,
  257. DEATH=5,
  258. //DEATH2=6, //unused?
  259. TURN_L=7, //will automatically play second part of anim and rotate creature
  260. TURN_R=8, //same
  261. //TURN_L2=9, //identical to previous?
  262. //TURN_R2=10,
  263. ATTACK_UP=11,
  264. ATTACK_FRONT=12,
  265. ATTACK_DOWN=13,
  266. SHOOT_UP=14,
  267. SHOOT_FRONT=15,
  268. SHOOT_DOWN=16,
  269. CAST_UP=17,
  270. CAST_FRONT=18,
  271. CAST_DOWN=19,
  272. DHEX_ATTACK_UP=17,
  273. DHEX_ATTACK_FRONT=18,
  274. DHEX_ATTACK_DOWN=19,
  275. MOVE_START=20, //no need to use this two directly - MOVING will be enought
  276. MOVE_END=21
  277. //MOUSEON=22 //special group for border-only images - IDLE will be used as base
  278. //READY=23 //same but STANDING is base
  279. };
  280. private:
  281. //queue of animations waiting to be displayed
  282. std::queue<EAnimType> queue;
  283. //this funcction is used as callback if preview flag was set during construction
  284. void loopPreview();
  285. public:
  286. //change anim to next if queue is not empty, call callback othervice
  287. void reset();
  288. //add sequence to the end of queue
  289. void addLast(EAnimType newType);
  290. void startPreview();
  291. //clear queue and set animation to this sequence
  292. void clearAndSet(EAnimType type);
  293. CCreatureAnim(int x, int y, std::string name, Rect picPos,
  294. ui8 flags= USE_RLE, EAnimType = HOLDING );
  295. };