CAnimation.cpp 5.4 KB

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