CAnimation.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "CDefFile.h"
  13. #include "Graphics.h"
  14. #include "../../lib/filesystem/Filesystem.h"
  15. #include "../../lib/JsonNode.h"
  16. #include "../renderSDL/SDLImage.h"
  17. std::shared_ptr<IImage> CAnimation::getFromExtraDef(std::string filename)
  18. {
  19. size_t pos = filename.find(':');
  20. if (pos == -1)
  21. return nullptr;
  22. CAnimation anim(filename.substr(0, pos));
  23. pos++;
  24. size_t frame = atoi(filename.c_str()+pos);
  25. size_t group = 0;
  26. pos = filename.find(':', pos);
  27. if (pos != -1)
  28. {
  29. pos++;
  30. group = frame;
  31. frame = atoi(filename.c_str()+pos);
  32. }
  33. anim.load(frame ,group);
  34. auto ret = anim.images[group][frame];
  35. anim.images.clear();
  36. return ret;
  37. }
  38. bool CAnimation::loadFrame(size_t frame, size_t group)
  39. {
  40. if(size(group) <= frame)
  41. {
  42. printError(frame, group, "LoadFrame");
  43. return false;
  44. }
  45. auto image = getImage(frame, group, false);
  46. if(image)
  47. {
  48. return true;
  49. }
  50. //try to get image from def
  51. if(source[group][frame].getType() == JsonNode::JsonType::DATA_NULL)
  52. {
  53. if(defFile)
  54. {
  55. auto frameList = defFile->getEntries();
  56. if(vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present
  57. {
  58. images[group][frame] = std::make_shared<SDLImage>(defFile.get(), frame, group);
  59. return true;
  60. }
  61. }
  62. // still here? image is missing
  63. printError(frame, group, "LoadFrame");
  64. images[group][frame] = std::make_shared<SDLImage>("DEFAULT", EImageBlitMode::ALPHA);
  65. }
  66. else //load from separate file
  67. {
  68. auto img = getFromExtraDef(source[group][frame]["file"].String());
  69. if(!img)
  70. img = std::make_shared<SDLImage>(source[group][frame], EImageBlitMode::ALPHA);
  71. images[group][frame] = img;
  72. return true;
  73. }
  74. return false;
  75. }
  76. bool CAnimation::unloadFrame(size_t frame, size_t group)
  77. {
  78. auto image = getImage(frame, group, false);
  79. if(image)
  80. {
  81. images[group].erase(frame);
  82. if(images[group].empty())
  83. images.erase(group);
  84. return true;
  85. }
  86. return false;
  87. }
  88. void CAnimation::initFromJson(const JsonNode & config)
  89. {
  90. std::string basepath;
  91. basepath = config["basepath"].String();
  92. JsonNode base(JsonNode::JsonType::DATA_STRUCT);
  93. base["margins"] = config["margins"];
  94. base["width"] = config["width"];
  95. base["height"] = config["height"];
  96. for(const JsonNode & group : config["sequences"].Vector())
  97. {
  98. size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
  99. source[groupID].clear();
  100. for(const JsonNode & frame : group["frames"].Vector())
  101. {
  102. JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT);
  103. JsonUtils::inherit(toAdd, base);
  104. toAdd["file"].String() = basepath + frame.String();
  105. source[groupID].push_back(toAdd);
  106. }
  107. }
  108. for(const JsonNode & node : config["images"].Vector())
  109. {
  110. size_t group = node["group"].Integer();
  111. size_t frame = node["frame"].Integer();
  112. if (source[group].size() <= frame)
  113. source[group].resize(frame+1);
  114. JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT);
  115. JsonUtils::inherit(toAdd, base);
  116. toAdd["file"].String() = basepath + node["file"].String();
  117. source[group][frame] = toAdd;
  118. }
  119. }
  120. void CAnimation::exportBitmaps(const boost::filesystem::path& path) const
  121. {
  122. if(images.empty())
  123. {
  124. logGlobal->error("Nothing to export, animation is empty");
  125. return;
  126. }
  127. boost::filesystem::path actualPath = path / "SPRITES" / name;
  128. boost::filesystem::create_directories(actualPath);
  129. size_t counter = 0;
  130. for(const auto & groupPair : images)
  131. {
  132. size_t group = groupPair.first;
  133. for(const auto & imagePair : groupPair.second)
  134. {
  135. size_t frame = imagePair.first;
  136. const auto img = imagePair.second;
  137. boost::format fmt("%d_%d.bmp");
  138. fmt % group % frame;
  139. img->exportBitmap(actualPath / fmt.str());
  140. counter++;
  141. }
  142. }
  143. logGlobal->info("Exported %d frames to %s", counter, actualPath.string());
  144. }
  145. void CAnimation::init()
  146. {
  147. if(defFile)
  148. {
  149. const std::map<size_t, size_t> defEntries = defFile->getEntries();
  150. for (auto & defEntry : defEntries)
  151. source[defEntry.first].resize(defEntry.second);
  152. }
  153. ResourceID resID(std::string("SPRITES/") + name, EResType::TEXT);
  154. if (vstd::contains(graphics->imageLists, resID.getName()))
  155. initFromJson(graphics->imageLists[resID.getName()]);
  156. auto configList = CResourceHandler::get()->getResourcesWithName(resID);
  157. for(auto & loader : configList)
  158. {
  159. auto stream = loader->load(resID);
  160. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  161. stream->read(textData.get(), stream->getSize());
  162. const JsonNode config((char*)textData.get(), stream->getSize());
  163. initFromJson(config);
  164. }
  165. }
  166. void CAnimation::printError(size_t frame, size_t group, std::string type) const
  167. {
  168. logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name, group, frame);
  169. }
  170. CAnimation::CAnimation(std::string Name):
  171. name(Name),
  172. preloaded(false),
  173. defFile()
  174. {
  175. size_t dotPos = name.find_last_of('.');
  176. if ( dotPos!=-1 )
  177. name.erase(dotPos);
  178. std::transform(name.begin(), name.end(), name.begin(), toupper);
  179. ResourceID resource(std::string("SPRITES/") + name, EResType::ANIMATION);
  180. if(CResourceHandler::get()->existsResource(resource))
  181. defFile = std::make_shared<CDefFile>(name);
  182. init();
  183. if(source.empty())
  184. logAnim->error("Animation %s failed to load", Name);
  185. }
  186. CAnimation::CAnimation():
  187. name(""),
  188. preloaded(false),
  189. defFile()
  190. {
  191. init();
  192. }
  193. CAnimation::~CAnimation() = default;
  194. void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
  195. {
  196. if(!source.count(sourceGroup))
  197. {
  198. logAnim->error("Group %d missing in %s", sourceGroup, name);
  199. return;
  200. }
  201. if(source[sourceGroup].size() <= sourceFrame)
  202. {
  203. logAnim->error("Frame [%d %d] missing in %s", sourceGroup, sourceFrame, name);
  204. return;
  205. }
  206. //todo: clone actual loaded Image object
  207. JsonNode clone(source[sourceGroup][sourceFrame]);
  208. if(clone.getType() == JsonNode::JsonType::DATA_NULL)
  209. {
  210. std::string temp = name+":"+boost::lexical_cast<std::string>(sourceGroup)+":"+boost::lexical_cast<std::string>(sourceFrame);
  211. clone["file"].String() = temp;
  212. }
  213. source[targetGroup].push_back(clone);
  214. size_t index = source[targetGroup].size() - 1;
  215. if(preloaded)
  216. load(index, targetGroup);
  217. }
  218. void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
  219. {
  220. if (source[group].size() <= frame)
  221. source[group].resize(frame+1);
  222. source[group][frame]["file"].String() = filename;
  223. //FIXME: update image if already loaded
  224. }
  225. std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose) const
  226. {
  227. auto groupIter = images.find(group);
  228. if (groupIter != images.end())
  229. {
  230. auto imageIter = groupIter->second.find(frame);
  231. if (imageIter != groupIter->second.end())
  232. return imageIter->second;
  233. }
  234. if (verbose)
  235. printError(frame, group, "GetImage");
  236. return nullptr;
  237. }
  238. void CAnimation::load()
  239. {
  240. for (auto & elem : source)
  241. for (size_t image=0; image < elem.second.size(); image++)
  242. loadFrame(image, elem.first);
  243. }
  244. void CAnimation::unload()
  245. {
  246. for (auto & elem : source)
  247. for (size_t image=0; image < elem.second.size(); image++)
  248. unloadFrame(image, elem.first);
  249. }
  250. void CAnimation::preload()
  251. {
  252. if(!preloaded)
  253. {
  254. preloaded = true;
  255. load();
  256. }
  257. }
  258. void CAnimation::loadGroup(size_t group)
  259. {
  260. if (vstd::contains(source, group))
  261. for (size_t image=0; image < source[group].size(); image++)
  262. loadFrame(image, group);
  263. }
  264. void CAnimation::unloadGroup(size_t group)
  265. {
  266. if (vstd::contains(source, group))
  267. for (size_t image=0; image < source[group].size(); image++)
  268. unloadFrame(image, group);
  269. }
  270. void CAnimation::load(size_t frame, size_t group)
  271. {
  272. loadFrame(frame, group);
  273. }
  274. void CAnimation::unload(size_t frame, size_t group)
  275. {
  276. unloadFrame(frame, group);
  277. }
  278. size_t CAnimation::size(size_t group) const
  279. {
  280. auto iter = source.find(group);
  281. if (iter != source.end())
  282. return iter->second.size();
  283. return 0;
  284. }
  285. void CAnimation::horizontalFlip()
  286. {
  287. for(auto & group : images)
  288. for(auto & image : group.second)
  289. image.second->horizontalFlip();
  290. }
  291. void CAnimation::verticalFlip()
  292. {
  293. for(auto & group : images)
  294. for(auto & image : group.second)
  295. image.second->verticalFlip();
  296. }
  297. void CAnimation::playerColored(PlayerColor player)
  298. {
  299. for(auto & group : images)
  300. for(auto & image : group.second)
  301. image.second->playerColored(player);
  302. }
  303. void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
  304. {
  305. for(size_t frame = 0; frame < size(sourceGroup); ++frame)
  306. {
  307. duplicateImage(sourceGroup, frame, targetGroup);
  308. auto image = getImage(frame, targetGroup);
  309. image->verticalFlip();
  310. }
  311. }