CAnimation.h 2.7 KB

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