CAnimation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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);
  32. else
  33. image = GH.renderHandler().loadImage(source[group][frame]);
  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):
  89. name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
  90. source(layout)
  91. {
  92. if(source.empty())
  93. logAnim->error("Animation %s failed to load", Name.getOriginalName());
  94. }
  95. CAnimation::CAnimation() = default;
  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. void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
  119. {
  120. if (source[group].size() <= frame)
  121. source[group].resize(frame+1);
  122. source[group][frame]["file"].String() = filename;
  123. //FIXME: update image if already loaded
  124. }
  125. std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
  126. {
  127. if (!loadFrame(frame, group))
  128. return nullptr;
  129. return getImageImpl(frame, group, verbose);
  130. }
  131. std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
  132. {
  133. auto groupIter = images.find(group);
  134. if (groupIter != images.end())
  135. {
  136. auto imageIter = groupIter->second.find(frame);
  137. if (imageIter != groupIter->second.end())
  138. return imageIter->second;
  139. }
  140. if (verbose)
  141. printError(frame, group, "GetImage");
  142. return nullptr;
  143. }
  144. size_t CAnimation::size(size_t group) const
  145. {
  146. auto iter = source.find(group);
  147. if (iter != source.end())
  148. return iter->second.size();
  149. return 0;
  150. }
  151. void CAnimation::horizontalFlip()
  152. {
  153. for(auto & group : source)
  154. for(size_t i = 0; i < group.second.size(); ++i)
  155. horizontalFlip(i, group.first);
  156. }
  157. void CAnimation::verticalFlip()
  158. {
  159. for(auto & group : source)
  160. for(size_t i = 0; i < group.second.size(); ++i)
  161. verticalFlip(i, group.first);
  162. }
  163. void CAnimation::horizontalFlip(size_t frame, size_t group)
  164. {
  165. try
  166. {
  167. images.at(group).at(frame) = nullptr;
  168. }
  169. catch (const std::out_of_range &)
  170. {
  171. // ignore - image not loaded
  172. }
  173. JsonNode & config = source.at(group).at(frame);
  174. if (config.isNull())
  175. {
  176. config["animation"].String() = name.getName();
  177. config["frame"].Integer() = frame;
  178. config["group"].Integer() = group;
  179. }
  180. config["horizontalFlip"].Bool() = !config["horizontalFlip"].Bool();
  181. }
  182. void CAnimation::verticalFlip(size_t frame, size_t group)
  183. {
  184. try
  185. {
  186. images.at(group).at(frame) = nullptr;
  187. }
  188. catch (const std::out_of_range &)
  189. {
  190. // ignore - image not loaded
  191. }
  192. JsonNode & config = source.at(group).at(frame);
  193. if (config.isNull())
  194. {
  195. config["animation"].String() = name.getName();
  196. config["frame"].Integer() = frame;
  197. config["group"].Integer() = group;
  198. }
  199. config["verticalFlip"].Bool() = !config["verticalFlip"].Bool();
  200. }
  201. void CAnimation::playerColored(PlayerColor player)
  202. {
  203. for(auto & group : images)
  204. for(auto & image : group.second)
  205. image.second->playerColored(player);
  206. }
  207. void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
  208. {
  209. for(size_t frame = 0; frame < size(sourceGroup); ++frame)
  210. {
  211. duplicateImage(sourceGroup, frame, targetGroup);
  212. verticalFlip(frame, targetGroup);
  213. }
  214. }