CAnimation.h 10 KB

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