2
0

CAnimation.h 10 KB

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