CAnimation.cpp 5.1 KB

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