CAnimation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. CAnimation::~CAnimation() = default;
  98. void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
  99. {
  100. if(!source.count(sourceGroup))
  101. {
  102. logAnim->error("Group %d missing in %s", sourceGroup, name.getName());
  103. return;
  104. }
  105. if(source[sourceGroup].size() <= sourceFrame)
  106. {
  107. logAnim->error("Frame [%d %d] missing in %s", sourceGroup, sourceFrame, name.getName());
  108. return;
  109. }
  110. JsonNode clone(source[sourceGroup][sourceFrame]);
  111. if(clone.getType() == JsonNode::JsonType::DATA_NULL)
  112. {
  113. clone["animation"].String() = name.getName();
  114. clone["group"].Integer() = sourceGroup;
  115. clone["frame"].Integer() = sourceFrame;
  116. }
  117. source[targetGroup].push_back(clone);
  118. }
  119. void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
  120. {
  121. if (source[group].size() <= frame)
  122. source[group].resize(frame+1);
  123. source[group][frame]["file"].String() = filename;
  124. //FIXME: update image if already loaded
  125. }
  126. std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
  127. {
  128. if (!loadFrame(frame, group))
  129. return nullptr;
  130. return getImageImpl(frame, group, verbose);
  131. }
  132. std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
  133. {
  134. auto groupIter = images.find(group);
  135. if (groupIter != images.end())
  136. {
  137. auto imageIter = groupIter->second.find(frame);
  138. if (imageIter != groupIter->second.end())
  139. return imageIter->second;
  140. }
  141. if (verbose)
  142. printError(frame, group, "GetImage");
  143. return nullptr;
  144. }
  145. size_t CAnimation::size(size_t group) const
  146. {
  147. auto iter = source.find(group);
  148. if (iter != source.end())
  149. return iter->second.size();
  150. return 0;
  151. }
  152. void CAnimation::horizontalFlip()
  153. {
  154. for(auto & group : source)
  155. for(size_t i = 0; i < group.second.size(); ++i)
  156. horizontalFlip(i, group.first);
  157. }
  158. void CAnimation::verticalFlip()
  159. {
  160. for(auto & group : source)
  161. for(size_t i = 0; i < group.second.size(); ++i)
  162. verticalFlip(i, group.first);
  163. }
  164. void CAnimation::horizontalFlip(size_t frame, size_t group)
  165. {
  166. try
  167. {
  168. images.at(group).at(frame) = nullptr;
  169. }
  170. catch (const std::out_of_range &)
  171. {
  172. // ignore - image not loaded
  173. }
  174. JsonNode & config = source.at(group).at(frame);
  175. if (config.isNull())
  176. {
  177. config["animation"].String() = name.getName();
  178. config["frame"].Integer() = frame;
  179. config["group"].Integer() = group;
  180. }
  181. config["horizontalFlip"].Bool() = !config["horizontalFlip"].Bool();
  182. }
  183. void CAnimation::verticalFlip(size_t frame, size_t group)
  184. {
  185. try
  186. {
  187. images.at(group).at(frame) = nullptr;
  188. }
  189. catch (const std::out_of_range &)
  190. {
  191. // ignore - image not loaded
  192. }
  193. JsonNode & config = source.at(group).at(frame);
  194. if (config.isNull())
  195. {
  196. config["animation"].String() = name.getName();
  197. config["frame"].Integer() = frame;
  198. config["group"].Integer() = group;
  199. }
  200. config["verticalFlip"].Bool() = !config["verticalFlip"].Bool();
  201. }
  202. void CAnimation::playerColored(PlayerColor player)
  203. {
  204. for(auto & group : images)
  205. for(auto & image : group.second)
  206. image.second->playerColored(player);
  207. }
  208. void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
  209. {
  210. for(size_t frame = 0; frame < size(sourceGroup); ++frame)
  211. {
  212. duplicateImage(sourceGroup, frame, targetGroup);
  213. verticalFlip(frame, targetGroup);
  214. }
  215. }