CAnimation.h 4.7 KB

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