CAnimation.h 4.4 KB

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