CAnimation.cpp 6.4 KB

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