RenderHandler.cpp 7.2 KB

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