CAnimation.h 5.0 KB

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