CAnimation.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * CAnimation.cpp, 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. #include "StdInc.h"
  11. #include "CAnimation.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/IImage.h"
  14. #include "../render/IRenderHandler.h"
  15. #include "../render/IScreenHandler.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/json/JsonUtils.h"
  18. bool CAnimation::loadFrame(size_t frame, size_t group)
  19. {
  20. if(size(group) <= frame)
  21. {
  22. printError(frame, group, "LoadFrame");
  23. return false;
  24. }
  25. if(auto image = getImageImpl(frame, group, false))
  26. return true;
  27. std::shared_ptr<IImage> image = GH.renderHandler().loadImage(getImageLocator(frame, group), mode);
  28. if(image)
  29. {
  30. images[group][frame] = image;
  31. if (player.isValidPlayer())
  32. image->playerColored(player);
  33. return true;
  34. }
  35. else
  36. {
  37. // image is missing
  38. printError(frame, group, "LoadFrame");
  39. images[group][frame] = GH.renderHandler().loadImage(ImagePath::builtin("DEFAULT"), EImageBlitMode::OPAQUE);
  40. return false;
  41. }
  42. }
  43. bool CAnimation::unloadFrame(size_t frame, size_t group)
  44. {
  45. auto image = getImage(frame, group, false);
  46. if(image)
  47. {
  48. images[group].erase(frame);
  49. if(images[group].empty())
  50. images.erase(group);
  51. return true;
  52. }
  53. return false;
  54. }
  55. void CAnimation::exportBitmaps(const boost::filesystem::path& path) const
  56. {
  57. if(images.empty())
  58. {
  59. logGlobal->error("Nothing to export, animation is empty");
  60. return;
  61. }
  62. boost::filesystem::path actualPath = path / "SPRITES" / name.getName();
  63. boost::filesystem::create_directories(actualPath);
  64. size_t counter = 0;
  65. for(const auto & groupPair : images)
  66. {
  67. size_t group = groupPair.first;
  68. for(const auto & imagePair : groupPair.second)
  69. {
  70. size_t frame = imagePair.first;
  71. const auto img = imagePair.second;
  72. boost::format fmt("%d_%d.bmp");
  73. fmt % group % frame;
  74. img->exportBitmap(actualPath / fmt.str());
  75. counter++;
  76. }
  77. }
  78. logGlobal->info("Exported %d frames to %s", counter, actualPath.string());
  79. }
  80. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  81. {
  82. logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name.getOriginalName(), group, frame);
  83. }
  84. CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <ImageLocator> > layout, EImageBlitMode mode):
  85. name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
  86. source(layout),
  87. mode(mode)
  88. {
  89. if(source.empty())
  90. logAnim->error("Animation %s failed to load", Name.getOriginalName());
  91. }
  92. CAnimation::~CAnimation() = default;
  93. void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
  94. {
  95. ImageLocator clone(getImageLocator(sourceFrame, sourceGroup));
  96. source[targetGroup].push_back(clone);
  97. }
  98. std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
  99. {
  100. if (!loadFrame(frame, group))
  101. return nullptr;
  102. return getImageImpl(frame, group, verbose);
  103. }
  104. std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
  105. {
  106. auto groupIter = images.find(group);
  107. if (groupIter != images.end())
  108. {
  109. auto imageIter = groupIter->second.find(frame);
  110. if (imageIter != groupIter->second.end())
  111. return imageIter->second;
  112. }
  113. if (verbose)
  114. printError(frame, group, "GetImage");
  115. return nullptr;
  116. }
  117. size_t CAnimation::size(size_t group) const
  118. {
  119. auto iter = source.find(group);
  120. if (iter != source.end())
  121. return iter->second.size();
  122. return 0;
  123. }
  124. void CAnimation::horizontalFlip()
  125. {
  126. for(auto & group : source)
  127. for(size_t i = 0; i < group.second.size(); ++i)
  128. horizontalFlip(i, group.first);
  129. }
  130. void CAnimation::verticalFlip()
  131. {
  132. for(auto & group : source)
  133. for(size_t i = 0; i < group.second.size(); ++i)
  134. verticalFlip(i, group.first);
  135. }
  136. void CAnimation::horizontalFlip(size_t frame, size_t group)
  137. {
  138. auto i1 = images.find(group);
  139. if(i1 != images.end())
  140. {
  141. auto i2 = i1->second.find(frame);
  142. if(i2 != i1->second.end())
  143. i2->second = nullptr;
  144. }
  145. auto locator = getImageLocator(frame, group);
  146. locator.horizontalFlip = !locator.horizontalFlip;
  147. source[group][frame] = locator;
  148. }
  149. void CAnimation::verticalFlip(size_t frame, size_t group)
  150. {
  151. auto i1 = images.find(group);
  152. if(i1 != images.end())
  153. {
  154. auto i2 = i1->second.find(frame);
  155. if(i2 != i1->second.end())
  156. i2->second = nullptr;
  157. }
  158. auto locator = getImageLocator(frame, group);
  159. locator.verticalFlip = !locator.verticalFlip;
  160. source[group][frame] = locator;
  161. }
  162. void CAnimation::playerColored(PlayerColor targetPlayer)
  163. {
  164. player = targetPlayer;
  165. for(auto & group : images)
  166. for(auto & image : group.second)
  167. image.second->playerColored(player);
  168. }
  169. void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
  170. {
  171. for(size_t frame = 0; frame < size(sourceGroup); ++frame)
  172. {
  173. duplicateImage(sourceGroup, frame, targetGroup);
  174. verticalFlip(frame, targetGroup);
  175. }
  176. }
  177. ImageLocator CAnimation::getImageLocator(size_t frame, size_t group) const
  178. {
  179. const ImageLocator & locator = source.at(group).at(frame);
  180. if (!locator.empty())
  181. return locator;
  182. return ImageLocator(name, frame, group);
  183. }