RenderHandler.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 "ScalableImage.h"
  14. #include "FontChain.h"
  15. #include "../GameEngine.h"
  16. #include "../render/AssetGenerator.h"
  17. #include "../render/CAnimation.h"
  18. #include "../render/CanvasImage.h"
  19. #include "../render/CDefFile.h"
  20. #include "../render/Colors.h"
  21. #include "../render/ColorFilter.h"
  22. #include "../render/IScreenHandler.h"
  23. #include "../../lib/CConfigHandler.h"
  24. #include "../../lib/CThreadHelper.h"
  25. #include "../../lib/ExceptionsCommon.h"
  26. #include "../../lib/VCMIDirs.h"
  27. #include "../../lib/constants/StringConstants.h"
  28. #include "../../lib/entities/building/CBuilding.h"
  29. #include "../../lib/entities/faction/CTown.h"
  30. #include "../../lib/entities/faction/CTownHandler.h"
  31. #include "../../lib/filesystem/Filesystem.h"
  32. #include "../../lib/json/JsonUtils.h"
  33. #include <vcmi/ArtifactService.h>
  34. #include <vcmi/CreatureService.h>
  35. #include <vcmi/Entity.h>
  36. #include <vcmi/FactionService.h>
  37. #include <vcmi/HeroTypeService.h>
  38. #include <vcmi/Services.h>
  39. #include <vcmi/SkillService.h>
  40. #include <vcmi/spells/Service.h>
  41. RenderHandler::RenderHandler()
  42. :assetGenerator(std::make_unique<AssetGenerator>())
  43. {
  44. }
  45. RenderHandler::~RenderHandler() = default;
  46. std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
  47. {
  48. AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
  49. auto it = animationFiles.find(actualPath);
  50. if (it != animationFiles.end())
  51. {
  52. auto locked = it->second.lock();
  53. if (locked)
  54. return locked;
  55. }
  56. if (!CResourceHandler::get()->existsResource(actualPath))
  57. return nullptr;
  58. auto result = std::make_shared<CDefFile>(actualPath);
  59. animationFiles[actualPath] = result;
  60. return result;
  61. }
  62. void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & config, EImageBlitMode mode) const
  63. {
  64. std::string basepath;
  65. basepath = config["basepath"].String();
  66. JsonNode base;
  67. base["margins"] = config["margins"];
  68. base["width"] = config["width"];
  69. base["height"] = config["height"];
  70. for(const JsonNode & group : config["sequences"].Vector())
  71. {
  72. size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
  73. source[groupID].clear();
  74. for(const JsonNode & frame : group["frames"].Vector())
  75. {
  76. JsonNode toAdd = frame;
  77. JsonUtils::inherit(toAdd, base);
  78. toAdd["file"].String() = basepath + frame.String();
  79. if(group["generateShadow"].isNumber())
  80. toAdd["generateShadow"].Integer() = group["generateShadow"].Integer();
  81. if(group["generateOverlay"].isNumber())
  82. toAdd["generateOverlay"].Integer() = group["generateOverlay"].Integer();
  83. source[groupID].emplace_back(toAdd, mode);
  84. }
  85. }
  86. for(const JsonNode & node : config["images"].Vector())
  87. {
  88. size_t group = node["group"].Integer();
  89. size_t frame = node["frame"].Integer();
  90. if (source[group].size() <= frame)
  91. source[group].resize(frame+1);
  92. JsonNode toAdd = node;
  93. JsonUtils::inherit(toAdd, base);
  94. if (toAdd.Struct().count("file"))
  95. toAdd["file"].String() = basepath + node["file"].String();
  96. if (toAdd.Struct().count("defFile"))
  97. toAdd["defFile"].String() = basepath + node["defFile"].String();
  98. source[group][frame] = ImageLocator(toAdd, mode);
  99. }
  100. }
  101. RenderHandler::AnimationLayoutMap & RenderHandler::getAnimationLayout(const AnimationPath & path, int scalingFactor, EImageBlitMode mode)
  102. {
  103. static constexpr std::array scaledSpritesPath = {
  104. "", // 0x
  105. "SPRITES/",
  106. "SPRITES2X/",
  107. "SPRITES3X/",
  108. "SPRITES4X/",
  109. };
  110. std::string pathString = path.getName();
  111. if (boost::starts_with(pathString, "SPRITES/"))
  112. pathString = pathString.substr(std::string("SPRITES/").length());
  113. AnimationPath actualPath = AnimationPath::builtin(scaledSpritesPath.at(scalingFactor) + pathString);
  114. auto it = animationLayouts.find(actualPath);
  115. if (it != animationLayouts.end() && (settings["video"]["useHdTextures"].Bool() || scalingFactor == 1))
  116. return it->second;
  117. AnimationLayoutMap result;
  118. auto defFile = getAnimationFile(actualPath);
  119. if(defFile)
  120. {
  121. const std::map<size_t, size_t> defEntries = defFile->getEntries();
  122. for (const auto & defEntry : defEntries)
  123. result[defEntry.first].resize(defEntry.second);
  124. }
  125. auto jsonResource = actualPath.toType<EResType::JSON>();
  126. auto configList = CResourceHandler::get()->getResourcesWithName(jsonResource);
  127. for(auto & loader : configList)
  128. {
  129. auto stream = loader->load(jsonResource);
  130. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  131. stream->read(textData.get(), stream->getSize());
  132. const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), path.getOriginalName());
  133. initFromJson(result, config, mode);
  134. }
  135. animationLayouts[actualPath] = result;
  136. return animationLayouts[actualPath];
  137. }
  138. int RenderHandler::getScalingFactor() const
  139. {
  140. return ENGINE->screenHandler().getScalingFactor();
  141. }
  142. ImageLocator RenderHandler::getLocatorForAnimationFrame(const AnimationPath & path, int frame, int group, int scaling, EImageBlitMode mode)
  143. {
  144. const auto & layout = getAnimationLayout(path, scaling, mode);
  145. if (!layout.count(group))
  146. return ImageLocator();
  147. if (frame >= layout.at(group).size())
  148. return ImageLocator();
  149. const auto & locator = layout.at(group).at(frame);
  150. if (locator.image || locator.defFile)
  151. return locator;
  152. return ImageLocator(path, frame, group, mode);
  153. }
  154. std::shared_ptr<ScalableImageShared> RenderHandler::loadImageImpl(const ImageLocator & locator)
  155. {
  156. auto it = imageFiles.find(locator);
  157. if (it != imageFiles.end())
  158. {
  159. auto locked = it->second.lock();
  160. if (locked)
  161. return locked;
  162. }
  163. auto sdlImage = loadImageFromFileUncached(locator);
  164. auto scaledImage = std::make_shared<ScalableImageShared>(locator, sdlImage);
  165. storeCachedImage(locator, scaledImage);
  166. return scaledImage;
  167. }
  168. std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
  169. {
  170. if(locator.image)
  171. {
  172. auto imagePath = *locator.image;
  173. auto imagePathSprites = imagePath.addPrefix("SPRITES/");
  174. auto imagePathData = imagePath.addPrefix("DATA/");
  175. if(CResourceHandler::get()->existsResource(imagePathSprites))
  176. return std::make_shared<SDLImageShared>(imagePathSprites);
  177. if(CResourceHandler::get()->existsResource(imagePathData))
  178. return std::make_shared<SDLImageShared>(imagePathData);
  179. if(CResourceHandler::get()->existsResource(imagePath))
  180. return std::make_shared<SDLImageShared>(imagePath);
  181. auto generated = assetGenerator->generateImage(imagePath);
  182. if (generated)
  183. return generated;
  184. logGlobal->error("Failed to load image %s", locator.image->getOriginalName());
  185. return std::make_shared<SDLImageShared>(ImagePath::builtin("DEFAULT"));
  186. }
  187. if(locator.defFile)
  188. {
  189. auto defFile = getAnimationFile(*locator.defFile);
  190. if(defFile->hasFrame(locator.defFrame, locator.defGroup))
  191. return std::make_shared<SDLImageShared>(defFile.get(), locator.defFrame, locator.defGroup);
  192. else
  193. {
  194. logGlobal->error("Frame %d in group %d not found in file: %s",
  195. locator.defFrame, locator.defGroup, locator.defFile->getName().c_str());
  196. return std::make_shared<SDLImageShared>(ImagePath::builtin("DEFAULT"));
  197. }
  198. }
  199. throw std::runtime_error("Invalid image locator received!");
  200. }
  201. void RenderHandler::storeCachedImage(const ImageLocator & locator, std::shared_ptr<ScalableImageShared> image)
  202. {
  203. imageFiles[locator] = image;
  204. }
  205. std::shared_ptr<SDLImageShared> RenderHandler::loadScaledImage(const ImageLocator & locator)
  206. {
  207. static constexpr std::array scaledDataPath = {
  208. "", // 0x
  209. "DATA/",
  210. "DATA2X/",
  211. "DATA3X/",
  212. "DATA4X/",
  213. };
  214. static constexpr std::array scaledSpritesPath = {
  215. "", // 0x
  216. "SPRITES/",
  217. "SPRITES2X/",
  218. "SPRITES3X/",
  219. "SPRITES4X/",
  220. };
  221. ImagePath pathToLoad;
  222. if(locator.defFile)
  223. {
  224. auto remappedLocator = getLocatorForAnimationFrame(*locator.defFile, locator.defFrame, locator.defGroup, locator.scalingFactor, locator.layer);
  225. // we expect that .def's are only used for 1x data, upscaled assets should use standalone images
  226. if (!remappedLocator.image)
  227. return nullptr;
  228. pathToLoad = *remappedLocator.image;
  229. }
  230. if(locator.image)
  231. pathToLoad = *locator.image;
  232. if (pathToLoad.empty())
  233. return nullptr;
  234. std::string imagePathString = pathToLoad.getName();
  235. bool generateShadow = locator.generateShadow && (*locator.generateShadow) != SharedImageLocator::ShadowMode::SHADOW_NONE;
  236. bool generateOverlay = locator.generateOverlay && (*locator.generateOverlay) != SharedImageLocator::OverlayMode::OVERLAY_NONE;
  237. bool isShadow = locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_SELECTION || locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_FLAG_COLOR;
  238. bool isOverlay = locator.layer == EImageBlitMode::ONLY_FLAG_COLOR || locator.layer == EImageBlitMode::ONLY_SELECTION;
  239. bool optimizeImage = !(isShadow && generateShadow) && !(isOverlay && generateOverlay); // images needs to expanded
  240. if(isOverlay && !generateOverlay)
  241. imagePathString += "-OVERLAY";
  242. if(isShadow && !generateShadow)
  243. imagePathString += "-SHADOW";
  244. if(locator.playerColored.isValidPlayer())
  245. imagePathString += "-" + boost::to_upper_copy(GameConstants::PLAYER_COLOR_NAMES[locator.playerColored.getNum()]);
  246. if(locator.playerColored == PlayerColor::NEUTRAL)
  247. imagePathString += "-NEUTRAL";
  248. auto imagePath = ImagePath::builtin(imagePathString);
  249. auto imagePathSprites = ImagePath::builtin(imagePathString).addPrefix(scaledSpritesPath.at(locator.scalingFactor));
  250. auto imagePathData = ImagePath::builtin(imagePathString).addPrefix(scaledDataPath.at(locator.scalingFactor));
  251. std::shared_ptr<SDLImageShared> img = nullptr;
  252. if(CResourceHandler::get()->existsResource(imagePathSprites) && (settings["video"]["useHdTextures"].Bool() || locator.scalingFactor == 1))
  253. img = std::make_shared<SDLImageShared>(imagePathSprites, optimizeImage);
  254. else if(CResourceHandler::get()->existsResource(imagePathData) && (settings["video"]["useHdTextures"].Bool() || locator.scalingFactor == 1))
  255. img = std::make_shared<SDLImageShared>(imagePathData, optimizeImage);
  256. else if(CResourceHandler::get()->existsResource(imagePath))
  257. img = std::make_shared<SDLImageShared>(imagePath, optimizeImage);
  258. else if(locator.scalingFactor == 1)
  259. img = std::dynamic_pointer_cast<SDLImageShared>(assetGenerator->generateImage(imagePath));
  260. if(img)
  261. {
  262. // TODO: Performance improvement - Run algorithm on optimized ("trimmed") images
  263. // Not implemented yet because different frame image sizes seems to cause wobbeling shadow -> needs a way around this
  264. if(isShadow && generateShadow)
  265. img = img->drawShadow((*locator.generateShadow) == SharedImageLocator::ShadowMode::SHADOW_SHEAR);
  266. if(isOverlay && generateOverlay && (*locator.generateOverlay) == SharedImageLocator::OverlayMode::OVERLAY_OUTLINE)
  267. img = img->drawOutline(Colors::WHITE, 1);
  268. if(locator.scalingFactor == 1)
  269. img->setAsyncUpscale(false); // no base image, needs to be done in sync
  270. }
  271. return img;
  272. }
  273. std::shared_ptr<IImage> RenderHandler::loadImage(const ImageLocator & locator)
  274. {
  275. ImageLocator adjustedLocator = locator;
  276. std::shared_ptr<ScalableImageInstance> result;
  277. if (adjustedLocator.scalingFactor == 0)
  278. {
  279. auto scaledLocator = adjustedLocator;
  280. scaledLocator.scalingFactor = getScalingFactor();
  281. result = loadImageImpl(scaledLocator)->createImageReference();
  282. }
  283. else
  284. result = loadImageImpl(adjustedLocator)->createImageReference();
  285. if (locator.horizontalFlip)
  286. result->horizontalFlip();
  287. if (locator.verticalFlip)
  288. result->verticalFlip();
  289. return result;
  290. }
  291. std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group, EImageBlitMode mode)
  292. {
  293. ImageLocator locator = getLocatorForAnimationFrame(path, frame, group, 1, mode);
  294. if (!locator.empty())
  295. return loadImage(locator);
  296. else
  297. {
  298. logGlobal->error("Failed to load non-existing image");
  299. return loadImage(ImageLocator(ImagePath::builtin("DEFAULT"), mode));
  300. }
  301. }
  302. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
  303. {
  304. auto name = path.getOriginalName();
  305. std::vector<std::string> splitted;
  306. boost::split(splitted, name, boost::is_any_of(":"));
  307. if(splitted.size() == 3)
  308. {
  309. ImageLocator locator = getLocatorForAnimationFrame(AnimationPath::builtin(splitted[0]), std::stoi(splitted[2]), std::stoi(splitted[1]), 1, mode);
  310. return loadImage(locator);
  311. }
  312. ImageLocator locator(path, mode);
  313. return loadImage(locator);
  314. }
  315. std::shared_ptr<CanvasImage> RenderHandler::createImage(const Point & size, CanvasScalingPolicy scalingPolicy)
  316. {
  317. return std::make_shared<CanvasImage>(size, scalingPolicy);
  318. }
  319. std::shared_ptr<CAnimation> RenderHandler::loadAnimation(const AnimationPath & path, EImageBlitMode mode)
  320. {
  321. return std::make_shared<CAnimation>(path, getAnimationLayout(path, 1, mode), mode);
  322. }
  323. void RenderHandler::addImageListEntries(const EntityService * service)
  324. {
  325. service->forEachBase([this](const Entity * entity, bool & stop)
  326. {
  327. entity->registerIcons([this](size_t index, size_t group, const std::string & listName, const std::string & imageName)
  328. {
  329. if (imageName.empty())
  330. return;
  331. auto & layout = getAnimationLayout(AnimationPath::builtin("SPRITES/" + listName), 1, EImageBlitMode::COLORKEY);
  332. JsonNode entry;
  333. entry["file"].String() = imageName;
  334. if (index >= layout[group].size())
  335. layout[group].resize(index + 1);
  336. layout[group][index] = ImageLocator(entry, EImageBlitMode::SIMPLE);
  337. });
  338. });
  339. }
  340. static void detectOverlappingBuildings(RenderHandler * renderHandler, const Faction * factionBase)
  341. {
  342. if (!factionBase->hasTown())
  343. return;
  344. auto faction = dynamic_cast<const CFaction*>(factionBase);
  345. for (const auto & left : faction->town->clientInfo.structures)
  346. {
  347. for (const auto & right : faction->town->clientInfo.structures)
  348. {
  349. if (left->identifier <= right->identifier)
  350. continue; // only a<->b comparison is needed, not a<->a or b<->a
  351. if (left->building && right->building && left->building->getBase() == right->building->getBase())
  352. {
  353. if (left->pos.z != right->pos.z)
  354. logMod->warn("Town %s: Upgrades of same building have different z-index: '%s' and '%s'", faction->getJsonKey(), left->identifier, right->identifier);
  355. continue; // upgrades of the same buildings are expected to overlap
  356. }
  357. if (left->pos.z != right->pos.z)
  358. continue; // buildings already have different z-index and have well-defined overlap logic
  359. auto leftImage = renderHandler->loadImage(left->defName, 0, 0, EImageBlitMode::COLORKEY);
  360. auto rightImage = renderHandler->loadImage(right->defName, 0, 0, EImageBlitMode::COLORKEY);
  361. Rect leftRect( left->pos.x, left->pos.y, leftImage->width(), leftImage->height());
  362. Rect rightRect( right->pos.x, right->pos.y, rightImage->width(), rightImage->height());
  363. Rect intersection = leftRect.intersect(rightRect);
  364. Point intersectionPosition;
  365. bool intersectionFound = false;
  366. for (int y = 0; y < intersection.h && !intersectionFound; ++y)
  367. {
  368. for (int x = 0; x < intersection.w && !intersectionFound; ++x)
  369. {
  370. Point leftPoint = Point(x,y) - leftRect.topLeft() + intersection.topLeft();
  371. Point rightPoint = Point(x,y) - rightRect.topLeft() + intersection.topLeft();
  372. if (!leftImage->isTransparent(leftPoint) && !rightImage->isTransparent(rightPoint))
  373. {
  374. intersectionFound = true;
  375. intersectionPosition = intersection.topLeft() + Point(x,y);
  376. }
  377. }
  378. }
  379. if (intersectionFound)
  380. logMod->warn("Town %s: Detected overlapping buildings '%s' and '%s' at (%d, %d) with same z-index!", faction->getJsonKey(), left->identifier, right->identifier, intersectionPosition.x, intersectionPosition.y);
  381. }
  382. }
  383. };
  384. void RenderHandler::onLibraryLoadingFinished(const Services * services)
  385. {
  386. assert(animationLayouts.empty());
  387. assetGenerator->initialize();
  388. animationLayouts = assetGenerator->generateAllAnimations();
  389. addImageListEntries(services->creatures());
  390. addImageListEntries(services->heroTypes());
  391. addImageListEntries(services->artifacts());
  392. addImageListEntries(services->factions());
  393. addImageListEntries(services->spells());
  394. addImageListEntries(services->skills());
  395. if (settings["mods"]["validation"].String() == "full")
  396. {
  397. services->factions()->forEach([this](const Faction * factionBase, bool & stop)
  398. {
  399. detectOverlappingBuildings(this, factionBase);
  400. });
  401. }
  402. }
  403. std::shared_ptr<const IFont> RenderHandler::loadFont(EFonts font)
  404. {
  405. if (fonts.count(font))
  406. return fonts.at(font);
  407. const int8_t index = static_cast<int8_t>(font);
  408. logGlobal->debug("Loading font %d", static_cast<int>(index));
  409. auto configList = CResourceHandler::get()->getResourcesWithName(JsonPath::builtin("config/fonts.json"));
  410. std::shared_ptr<FontChain> loadedFont = std::make_shared<FontChain>();
  411. std::string bitmapPath;
  412. for(auto & loader : configList)
  413. {
  414. auto stream = loader->load(JsonPath::builtin("config/fonts.json"));
  415. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  416. stream->read(textData.get(), stream->getSize());
  417. const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), "config/fonts.json");
  418. const JsonVector & bmpConf = config["bitmap"].Vector();
  419. const JsonNode & ttfConf = config["trueType"];
  420. bitmapPath = bmpConf[index].String();
  421. if (!ttfConf[bitmapPath].isNull())
  422. loadedFont->addTrueTypeFont(ttfConf[bitmapPath], !config["lowPriority"].Bool());
  423. }
  424. loadedFont->addBitmapFont(bitmapPath);
  425. fonts[font] = loadedFont;
  426. return loadedFont;
  427. }
  428. void RenderHandler::exportGeneratedAssets()
  429. {
  430. for (const auto & entry : assetGenerator->generateAllImages())
  431. entry.second->exportBitmap(VCMIDirs::get().userDataPath() / "Generated" / (entry.first.getOriginalName() + ".png"), nullptr);
  432. }