CAnimation.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. virtual void verticalFlip() = 0;
  38. IImage();
  39. virtual ~IImage() {};
  40. };
  41. /// Class for handling animation
  42. class CAnimation
  43. {
  44. private:
  45. //source[group][position] - file with this frame, if string is empty - image located in def file
  46. std::map<size_t, std::vector <JsonNode> > source;
  47. //bitmap[group][position], store objects with loaded bitmaps
  48. std::map<size_t, std::map<size_t, IImage* > > images;
  49. //animation file name
  50. std::string name;
  51. //if true all frames will be stored in compressed (RLE) state
  52. const bool compressed;
  53. bool preloaded;
  54. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  55. bool loadFrame(CDefFile * file, size_t frame, size_t group);
  56. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  57. bool unloadFrame(size_t frame, size_t group);
  58. //initialize animation from file
  59. void initFromJson(const JsonNode & input);
  60. void init(CDefFile * file);
  61. //try to open def file
  62. CDefFile * getFile() const;
  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. IImage * getFromExtraDef(std::string filename);
  68. public:
  69. CAnimation(std::string Name, bool Compressed = false);
  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. //get pointer to image from specific group, nullptr if not found
  78. IImage * getImage(size_t frame, size_t group=0, bool verbose=true) 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. };
  92. const float DEFAULT_DELTA = 0.05f;
  93. class CFadeAnimation
  94. {
  95. public:
  96. enum class EMode
  97. {
  98. NONE, IN, OUT
  99. };
  100. private:
  101. float delta;
  102. SDL_Surface * fadingSurface;
  103. bool fading;
  104. float fadingCounter;
  105. bool shouldFreeSurface;
  106. float initialCounter() const;
  107. bool isFinished() const;
  108. public:
  109. EMode fadingMode;
  110. CFadeAnimation();
  111. ~CFadeAnimation();
  112. void init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd = false, float animDelta = DEFAULT_DELTA);
  113. void update();
  114. void draw(SDL_Surface * targetSurface, const SDL_Rect * sourceRect, SDL_Rect * destRect);
  115. bool isFading() const { return fading; }
  116. };