CAnimation.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * CAnimation.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/vcmi_endian.h"
  12. #include "Geometries.h"
  13. #include "../../lib/GameConstants.h"
  14. namespace bfs = boost::filesystem;
  15. #ifdef IN
  16. #undef IN
  17. #endif
  18. #ifdef OUT
  19. #undef OUT
  20. #endif
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class JsonNode;
  23. VCMI_LIB_NAMESPACE_END
  24. struct SDL_Surface;
  25. class CDefFile;
  26. class ColorShifter;
  27. /*
  28. * Base class for images, can be used for non-animation pictures as well
  29. */
  30. class IImage
  31. {
  32. public:
  33. using BorderPallete = std::array<SDL_Color, 3>;
  34. //draws image on surface "where" at position
  35. virtual void draw(SDL_Surface * where, int posX = 0, int posY = 0, Rect * src = nullptr, ui8 alpha = 255) const=0;
  36. virtual void draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src, ui8 alpha = 255) const = 0;
  37. virtual std::shared_ptr<IImage> scaleFast(float scale) const = 0;
  38. virtual void exportBitmap(const bfs::path & path) const = 0;
  39. //Change palette to specific player
  40. virtual void playerColored(PlayerColor player)=0;
  41. //set special color for flag
  42. virtual void setFlagColor(PlayerColor player)=0;
  43. virtual int width() const=0;
  44. virtual int height() const=0;
  45. //only indexed bitmaps, 16 colors maximum
  46. virtual void shiftPalette(int from, int howMany) = 0;
  47. virtual void adjustPalette(const ColorShifter * shifter) = 0;
  48. virtual void resetPalette() = 0;
  49. //only indexed bitmaps, colors 5,6,7 must be special
  50. virtual void setBorderPallete(const BorderPallete & borderPallete) = 0;
  51. virtual void horizontalFlip() = 0;
  52. virtual void verticalFlip() = 0;
  53. IImage();
  54. virtual ~IImage();
  55. };
  56. /// Class for handling animation
  57. class CAnimation
  58. {
  59. private:
  60. //source[group][position] - file with this frame, if string is empty - image located in def file
  61. std::map<size_t, std::vector <JsonNode> > source;
  62. //bitmap[group][position], store objects with loaded bitmaps
  63. std::map<size_t, std::map<size_t, std::shared_ptr<IImage> > > images;
  64. //animation file name
  65. std::string name;
  66. bool preloaded;
  67. std::shared_ptr<CDefFile> defFile;
  68. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  69. bool loadFrame(size_t frame, size_t group);
  70. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  71. bool unloadFrame(size_t frame, size_t group);
  72. //initialize animation from file
  73. void initFromJson(const JsonNode & input);
  74. void init();
  75. //to get rid of copy-pasting error message :]
  76. void printError(size_t frame, size_t group, std::string type) const;
  77. //not a very nice method to get image from another def file
  78. //TODO: remove after implementing resource manager
  79. std::shared_ptr<IImage> getFromExtraDef(std::string filename);
  80. public:
  81. CAnimation(std::string Name);
  82. CAnimation();
  83. ~CAnimation();
  84. //duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
  85. //and loads it if animation is preloaded
  86. void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup);
  87. // adjust the color of the animation, used in battle spell effects, e.g. Cloned objects
  88. void shiftColor(const ColorShifter * shifter);
  89. //add custom surface to the selected position.
  90. void setCustom(std::string filename, size_t frame, size_t group=0);
  91. std::shared_ptr<IImage> getImage(size_t frame, size_t group=0, bool verbose=true) const;
  92. void exportBitmaps(const bfs::path & path, bool prependResourceName = false) const;
  93. //all available frames
  94. void load ();
  95. void unload();
  96. void preload();
  97. //all frames from group
  98. void loadGroup (size_t group);
  99. void unloadGroup(size_t group);
  100. //single image
  101. void load (size_t frame, size_t group=0);
  102. void unload(size_t frame, size_t group=0);
  103. //total count of frames in group (including not loaded)
  104. size_t size(size_t group=0) const;
  105. void horizontalFlip();
  106. void verticalFlip();
  107. void playerColored(PlayerColor player);
  108. void createFlippedGroup(const size_t sourceGroup, const size_t targetGroup);
  109. };
  110. const float DEFAULT_DELTA = 0.05f;
  111. class CFadeAnimation
  112. {
  113. public:
  114. enum class EMode
  115. {
  116. NONE, IN, OUT
  117. };
  118. private:
  119. float delta;
  120. SDL_Surface * fadingSurface;
  121. bool fading;
  122. float fadingCounter;
  123. bool shouldFreeSurface;
  124. float initialCounter() const;
  125. bool isFinished() const;
  126. public:
  127. EMode fadingMode;
  128. CFadeAnimation();
  129. ~CFadeAnimation();
  130. void init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd = false, float animDelta = DEFAULT_DELTA);
  131. void update();
  132. void draw(SDL_Surface * targetSurface, const SDL_Rect * sourceRect, SDL_Rect * destRect);
  133. bool isFading() const { return fading; }
  134. };