CAnimation.h 5.2 KB

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