CAnimation.cpp 5.0 KB

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