Animation.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Animation.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. //code is copied from vcmiclient/CAnimation.h with minimal changes
  12. #include "../lib/GameConstants.h"
  13. #include <QRgb>
  14. #include <QImage>
  15. #include <QDir>
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class JsonNode;
  18. VCMI_LIB_NAMESPACE_END
  19. /*
  20. * Base class for images, can be used for non-animation pictures as well
  21. */
  22. class DefFile;
  23. /// Class for handling animation
  24. class Animation
  25. {
  26. private:
  27. //source[group][position] - file with this frame, if string is empty - image located in def file
  28. std::map<size_t, std::vector<JsonNode>> source;
  29. //bitmap[group][position], store objects with loaded bitmaps
  30. std::map<size_t, std::map<size_t, std::shared_ptr<QImage> > > images;
  31. //animation file name
  32. std::string name;
  33. bool preloaded;
  34. std::shared_ptr<DefFile> defFile;
  35. //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
  36. bool loadFrame(size_t frame, size_t group);
  37. //unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
  38. bool unloadFrame(size_t frame, size_t group);
  39. //initialize animation from file
  40. void initFromJson(const JsonNode & input);
  41. void init();
  42. //to get rid of copy-pasting error message :]
  43. void printError(size_t frame, size_t group, std::string type) const;
  44. //not a very nice method to get image from another def file
  45. //TODO: remove after implementing resource manager
  46. std::shared_ptr<QImage> getFromExtraDef(std::string filename);
  47. public:
  48. Animation(std::string Name);
  49. Animation();
  50. ~Animation();
  51. //duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
  52. //and loads it if animation is preloaded
  53. void duplicateImage(size_t sourceGroup, size_t sourceFrame, size_t targetGroup);
  54. // adjust the color of the animation, used in battle spell effects, e.g. Cloned objects
  55. std::shared_ptr<QImage> getImage(size_t frame, size_t group = 0, bool verbose = true) const;
  56. //all available frames
  57. void load();
  58. void unload();
  59. void preload();
  60. //all frames from group
  61. void loadGroup (size_t group);
  62. void unloadGroup(size_t group);
  63. //single image
  64. void load (size_t frame, size_t group = 0);
  65. void unload(size_t frame, size_t group = 0);
  66. void exportBitmaps(const QDir & path) const;
  67. //total count of frames in group (including not loaded)
  68. size_t size(size_t group = 0) const;
  69. void horizontalFlip();
  70. void verticalFlip();
  71. void playerColored(PlayerColor player);
  72. void createFlippedGroup(const size_t sourceGroup, const size_t targetGroup);
  73. };