Animation.h 2.6 KB

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