CAnimation.h 4.9 KB

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