CAnimation.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef __CANIMATION_H__
  2. #define __CANIMATION_H__
  3. #include <vector>
  4. #include <string>
  5. #include <set>
  6. #include "../global.h"
  7. /*
  8. * CAnimation.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. struct SDL_Surface;
  17. struct BMPPalette;
  18. //class for def loading, methods are based on CDefHandler
  19. //after loading will store general info (palette and frame offsets) and pointer to file itself
  20. class CDefFile
  21. {
  22. private:
  23. struct SSpriteDef
  24. {
  25. ui32 prSize;
  26. ui32 defType2;
  27. ui32 FullWidth;
  28. ui32 FullHeight;
  29. ui32 SpriteWidth;
  30. ui32 SpriteHeight;
  31. ui32 LeftMargin;
  32. ui32 TopMargin;
  33. };
  34. unsigned char * data;
  35. int datasize;
  36. BMPPalette * colors;
  37. //offset[group][frame] - offset of frame data in file
  38. std::vector< std::vector <size_t> > offset;
  39. //sorted list of offsets used to determine size
  40. std::set <size_t> offList;
  41. public:
  42. CDefFile(std::string Name);
  43. ~CDefFile();
  44. //true if file was opened correctly
  45. bool loaded() const;
  46. //get copy of palette to unpack compressed animation
  47. BMPPalette * getPalette();
  48. //true if frame is present in it
  49. bool haveFrame(size_t frame, size_t group) const;
  50. //get copy of binary data
  51. unsigned char * getFrame(size_t frame, size_t group) const;
  52. //load frame as SDL_Surface
  53. SDL_Surface * loadFrame(size_t frame, size_t group) const ;
  54. //static version of previous one for calling from compressed anim
  55. static SDL_Surface * loadFrame(const unsigned char * FDef, const BMPPalette * palette);
  56. };
  57. // Class for handling animation.
  58. class CAnimation
  59. {
  60. private:
  61. //internal structure to hold all data of specific frame
  62. struct AnimEntry
  63. {
  64. //surface for this entry
  65. SDL_Surface * surf;
  66. //bitfield, location of image data: 1 - def, 2 - file#9.*, 4 - file#9#2.*
  67. unsigned char source;
  68. //reference count, changed by loadFrame \ unloadFrame
  69. size_t refCount;
  70. //data for CompressedAnim
  71. unsigned char * data;
  72. //size of compressed data, unused for def files
  73. size_t dataSize;
  74. AnimEntry();
  75. };
  76. //animation file name
  77. std::string name;
  78. //if true all frames will be stored in compressed state
  79. const bool compressed;
  80. //palette from def file, used only for compressed anim
  81. BMPPalette * defPalette;
  82. //entries[group][position], store all info regarding frames
  83. std::vector< std::vector <AnimEntry> > entries;
  84. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  85. bool loadFrame(CDefFile * file, size_t frame, size_t group);
  86. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  87. bool unloadFrame(size_t frame, size_t group);
  88. //decompress entry data
  89. void decompress(AnimEntry &entry);
  90. //initialize animation from file
  91. void init(CDefFile * file);
  92. //try to open def file
  93. CDefFile * getFile() const;
  94. //to get rid of copy-pasting error message :]
  95. void printError(size_t frame, size_t group, std::string type) const;
  96. public:
  97. CAnimation(std::string Name, bool Compressed = false);
  98. CAnimation();
  99. ~CAnimation();
  100. //add custom surface to the end of specific group. If shared==true surface needs to be deleted
  101. //somewhere outside of CAnim as well (SDL_Surface::refcount will be increased)
  102. void add(SDL_Surface * surf, bool shared=false, size_t group=0);
  103. //removes all surfaces which have compressed data
  104. void purgeCompressed();
  105. //get pointer to surface, this function ignores groups (like ourImages in DefHandler)
  106. SDL_Surface * image (size_t frame);
  107. //get pointer to surface, from specific group
  108. SDL_Surface * image(size_t frame, size_t group);
  109. //removes all frames as well as their entries
  110. void clear();
  111. //all available frames
  112. void load ();
  113. void unload();
  114. //all frames from group
  115. void loadGroup (size_t group);
  116. void unloadGroup(size_t group);
  117. //single image
  118. void load (size_t frame, size_t group=0);
  119. void unload(size_t frame, size_t group=0);
  120. //list of frames (first = group ID, second = frame ID)
  121. void load (std::vector <std::pair <size_t, size_t> > frames);
  122. void unload(std::vector <std::pair <size_t, size_t> > frames);
  123. //helper to fix frame order on some buttons
  124. void fixButtonPos();
  125. //size of specific group, 0 if not present
  126. size_t groupSize(size_t group) const;
  127. //total count of frames in whole anim
  128. size_t size() const;
  129. };
  130. #endif // __CANIMATIONHANDLER_H__