CAnimation.h 4.7 KB

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