CAnimation.h 10 KB

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