CAnimation.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include "../../lib/vcmi_endian.h"
  3. #include "gui/Geometries.h"
  4. #include "../../lib/GameConstants.h"
  5. /*
  6. * CAnimation.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  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. //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 SDL_Surface * scaleFast(float scale) const = 0;
  28. //decrease ref count, returns true if image can be deleted (refCount <= 0)
  29. bool decreaseRef();
  30. void increaseRef();
  31. //Change palette to specific player
  32. virtual void playerColored(PlayerColor player)=0;
  33. //set special color for flag
  34. virtual void setFlagColor(PlayerColor player)=0;
  35. virtual int width() const=0;
  36. virtual int height() const=0;
  37. //only indexed bitmaps, 16 colors maximum
  38. virtual void shiftPalette(int from, int howMany) = 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, IImage* > > images;
  52. //animation file name
  53. std::string name;
  54. //if true all frames will be stored in compressed (RLE) state
  55. const bool compressed;
  56. bool preloaded;
  57. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  58. bool loadFrame(CDefFile * file, size_t frame, size_t group);
  59. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  60. bool unloadFrame(size_t frame, size_t group);
  61. //initialize animation from file
  62. void initFromJson(const JsonNode & input);
  63. void init(CDefFile * file);
  64. //try to open def file
  65. CDefFile * getFile() const;
  66. //to get rid of copy-pasting error message :]
  67. void printError(size_t frame, size_t group, std::string type) const;
  68. //not a very nice method to get image from another def file
  69. //TODO: remove after implementing resource manager
  70. IImage * getFromExtraDef(std::string filename);
  71. public:
  72. CAnimation(std::string Name, bool Compressed = false);
  73. CAnimation();
  74. ~CAnimation();
  75. //duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
  76. //and loads it if animation is preloaded
  77. void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup);
  78. //add custom surface to the selected position.
  79. void setCustom(std::string filename, size_t frame, size_t group=0);
  80. //get pointer to image from specific group, nullptr if not found
  81. IImage * getImage(size_t frame, size_t group=0, bool verbose=true) const;
  82. //all available frames
  83. void load ();
  84. void unload();
  85. void preload();
  86. //all frames from group
  87. void loadGroup (size_t group);
  88. void unloadGroup(size_t group);
  89. //single image
  90. void load (size_t frame, size_t group=0);
  91. void unload(size_t frame, size_t group=0);
  92. //total count of frames in group (including not loaded)
  93. size_t size(size_t group=0) const;
  94. };
  95. const float DEFAULT_DELTA = 0.05f;
  96. class CFadeAnimation
  97. {
  98. public:
  99. enum class EMode
  100. {
  101. NONE, IN, OUT
  102. };
  103. private:
  104. float delta;
  105. SDL_Surface * fadingSurface;
  106. bool fading;
  107. float fadingCounter;
  108. bool shouldFreeSurface;
  109. float initialCounter() const;
  110. bool isFinished() const;
  111. public:
  112. EMode fadingMode;
  113. CFadeAnimation();
  114. ~CFadeAnimation();
  115. void init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd = false, float animDelta = DEFAULT_DELTA);
  116. void update();
  117. void draw(SDL_Surface * targetSurface, const SDL_Rect * sourceRect, SDL_Rect * destRect);
  118. bool isFading() const { return fading; }
  119. };