CAnimation.cpp 5.6 KB

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