RenderHandler.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * RenderHandler.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 "RenderHandler.h"
  12. #include "SDLImage.h"
  13. #include "../render/CAnimation.h"
  14. #include "../render/CDefFile.h"
  15. #include "../../lib/json/JsonUtils.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include <vcmi/ArtifactService.h>
  18. #include <vcmi/CreatureService.h>
  19. #include <vcmi/Entity.h>
  20. #include <vcmi/FactionService.h>
  21. #include <vcmi/HeroTypeService.h>
  22. #include <vcmi/Services.h>
  23. #include <vcmi/SkillService.h>
  24. #include <vcmi/spells/Service.h>
  25. RenderHandler::ImageLocator::ImageLocator(const JsonNode & config)
  26. : image(ImagePath::fromJson(config["file"]))
  27. , animation(AnimationPath::fromJson(config["animation"]))
  28. , frame(config["frame"].Integer())
  29. , group(config["group"].Integer())
  30. , verticalFlip(config["verticalFlip"].Bool())
  31. , horizontalFlip(config["horizontalFlip"].Bool())
  32. {
  33. }
  34. RenderHandler::ImageLocator::ImageLocator(const ImagePath & path)
  35. : image(path)
  36. {
  37. }
  38. RenderHandler::ImageLocator::ImageLocator(const AnimationPath & path, int frame, int group)
  39. : animation(path)
  40. , frame(frame)
  41. , group(group)
  42. {
  43. }
  44. bool RenderHandler::ImageLocator::operator<(const ImageLocator & other) const
  45. {
  46. if(image != other.image)
  47. return image < other.image;
  48. if(animation != other.animation)
  49. return animation < other.animation;
  50. if(group != other.group)
  51. return group < other.group;
  52. if(frame != other.frame)
  53. return frame < other.frame;
  54. if(verticalFlip != other.verticalFlip)
  55. return verticalFlip < other.verticalFlip;
  56. return horizontalFlip < other.horizontalFlip;
  57. }
  58. std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
  59. {
  60. AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
  61. auto it = animationFiles.find(actualPath);
  62. if (it != animationFiles.end())
  63. return it->second;
  64. if (!CResourceHandler::get()->existsResource(actualPath))
  65. {
  66. animationFiles[actualPath] = nullptr;
  67. return nullptr;
  68. }
  69. auto result = std::make_shared<CDefFile>(actualPath);
  70. animationFiles[actualPath] = result;
  71. return result;
  72. }
  73. void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & config)
  74. {
  75. std::string basepath;
  76. basepath = config["basepath"].String();
  77. JsonNode base;
  78. base["margins"] = config["margins"];
  79. base["width"] = config["width"];
  80. base["height"] = config["height"];
  81. for(const JsonNode & group : config["sequences"].Vector())
  82. {
  83. size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
  84. source[groupID].clear();
  85. for(const JsonNode & frame : group["frames"].Vector())
  86. {
  87. JsonNode toAdd;
  88. JsonUtils::inherit(toAdd, base);
  89. toAdd["file"].String() = basepath + frame.String();
  90. source[groupID].push_back(toAdd);
  91. }
  92. }
  93. for(const JsonNode & node : config["images"].Vector())
  94. {
  95. size_t group = node["group"].Integer();
  96. size_t frame = node["frame"].Integer();
  97. if (source[group].size() <= frame)
  98. source[group].resize(frame+1);
  99. JsonNode toAdd;
  100. JsonUtils::inherit(toAdd, base);
  101. toAdd["file"].String() = basepath + node["file"].String();
  102. source[group][frame] = toAdd;
  103. }
  104. }
  105. RenderHandler::AnimationLayoutMap & RenderHandler::getAnimationLayout(const AnimationPath & path)
  106. {
  107. AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
  108. auto it = animationLayouts.find(actualPath);
  109. if (it != animationLayouts.end())
  110. return it->second;
  111. AnimationLayoutMap result;
  112. auto defFile = getAnimationFile(actualPath);
  113. if(defFile)
  114. {
  115. const std::map<size_t, size_t> defEntries = defFile->getEntries();
  116. for (const auto & defEntry : defEntries)
  117. result[defEntry.first].resize(defEntry.second);
  118. }
  119. auto jsonResource = actualPath.toType<EResType::JSON>();
  120. auto configList = CResourceHandler::get()->getResourcesWithName(jsonResource);
  121. for(auto & loader : configList)
  122. {
  123. auto stream = loader->load(jsonResource);
  124. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  125. stream->read(textData.get(), stream->getSize());
  126. const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), path.getOriginalName());
  127. initFromJson(result, config);
  128. }
  129. animationLayouts[actualPath] = result;
  130. return animationLayouts[actualPath];
  131. }
  132. std::shared_ptr<IConstImage> RenderHandler::loadImageFromSingleFile(const ImagePath & path)
  133. {
  134. auto result = std::make_shared<SDLImageConst>(path, EImageBlitMode::COLORKEY);
  135. imageFiles[ImageLocator(path)] = result;
  136. return result;
  137. }
  138. std::shared_ptr<IConstImage> RenderHandler::loadImageFromAnimationFileUncached(const AnimationPath & path, int frame, int group)
  139. {
  140. const auto & layout = getAnimationLayout(path);
  141. if (!layout.count(group))
  142. return loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
  143. if (frame >= layout.at(group).size())
  144. return loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
  145. const auto & config = layout.at(group).at(frame);
  146. if (config.isNull())
  147. {
  148. auto defFile = getAnimationFile(path);
  149. return std::make_shared<SDLImageConst>(defFile.get(), frame, group);
  150. }
  151. else
  152. {
  153. return loadImageImpl(ImageLocator(config));
  154. }
  155. }
  156. std::shared_ptr<IConstImage> RenderHandler::loadImageFromAnimationFile(const AnimationPath & path, int frame, int group)
  157. {
  158. auto result = loadImageFromAnimationFileUncached(path, frame, group);
  159. imageFiles[ImageLocator(path, frame, group)] = result;
  160. return result;
  161. }
  162. std::shared_ptr<IConstImage> RenderHandler::loadImageImpl(const ImageLocator & locator)
  163. {
  164. auto it = imageFiles.find(locator);
  165. if (it != imageFiles.end())
  166. return it->second;
  167. std::shared_ptr<IConstImage> result;
  168. if (!locator.image.empty())
  169. result = loadImageFromSingleFile(locator.image);
  170. else if (!locator.animation.empty())
  171. result = loadImageFromAnimationFile(locator.animation, locator.frame, locator.group);
  172. if (!result)
  173. result = loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
  174. if (locator.verticalFlip)
  175. result = result->verticalFlip();
  176. if (locator.horizontalFlip)
  177. result = result->horizontalFlip();
  178. imageFiles[locator] = result;
  179. return result;
  180. }
  181. std::shared_ptr<IImage> RenderHandler::loadImage(const JsonNode & config)
  182. {
  183. if (config.isString())
  184. return loadImageImpl(ImageLocator(ImagePath::fromJson(config)))->createImageReference();
  185. else
  186. return loadImageImpl(ImageLocator(config))->createImageReference();
  187. }
  188. std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group)
  189. {
  190. return loadImageImpl(ImageLocator(path, frame, group))->createImageReference();
  191. }
  192. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path)
  193. {
  194. return loadImage(path, EImageBlitMode::ALPHA);
  195. }
  196. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
  197. {
  198. return loadImageImpl(ImageLocator(path))->createImageReference();
  199. }
  200. std::shared_ptr<IImage> RenderHandler::createImage(SDL_Surface * source)
  201. {
  202. return std::make_shared<SDLImageConst>(source, EImageBlitMode::ALPHA)->createImageReference();
  203. }
  204. std::shared_ptr<CAnimation> RenderHandler::loadAnimation(const AnimationPath & path)
  205. {
  206. return std::make_shared<CAnimation>(path, getAnimationLayout(path));
  207. }
  208. std::shared_ptr<CAnimation> RenderHandler::createAnimation()
  209. {
  210. return std::make_shared<CAnimation>();
  211. }
  212. void RenderHandler::addImageListEntries(const EntityService * service)
  213. {
  214. service->forEachBase([this](const Entity * entity, bool & stop)
  215. {
  216. entity->registerIcons([this](size_t index, size_t group, const std::string & listName, const std::string & imageName)
  217. {
  218. if (imageName.empty())
  219. return;
  220. auto & layout = getAnimationLayout(AnimationPath::builtin("SPRITES/" + listName));
  221. JsonNode entry;
  222. entry["file"].String() = imageName;
  223. if (index >= layout[group].size())
  224. layout[group].resize(index + 1);
  225. layout[group][index] = entry;
  226. });
  227. });
  228. }
  229. void RenderHandler::onLibraryLoadingFinished(const Services * services)
  230. {
  231. addImageListEntries(services->creatures());
  232. addImageListEntries(services->heroTypes());
  233. addImageListEntries(services->artifacts());
  234. addImageListEntries(services->factions());
  235. addImageListEntries(services->spells());
  236. addImageListEntries(services->skills());
  237. }