2023-09-04 17:01:44 +02:00
|
|
|
/*
|
|
|
|
* RenderHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "RenderHandler.h"
|
|
|
|
|
|
|
|
#include "SDLImage.h"
|
2024-07-21 23:11:02 +02:00
|
|
|
#include "ImageScaled.h"
|
2023-09-04 17:01:44 +02:00
|
|
|
|
2024-08-03 22:14:51 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
|
2024-05-22 13:28:13 +02:00
|
|
|
#include "../render/CAnimation.h"
|
|
|
|
#include "../render/CDefFile.h"
|
2024-07-25 12:38:48 +02:00
|
|
|
#include "../render/Colors.h"
|
|
|
|
#include "../render/ColorFilter.h"
|
2024-08-03 22:14:51 +02:00
|
|
|
#include "../render/IScreenHandler.h"
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
#include "../../lib/json/JsonUtils.h"
|
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2024-09-13 14:26:31 +02:00
|
|
|
#include "../../lib/VCMIDirs.h"
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
#include <vcmi/ArtifactService.h>
|
|
|
|
#include <vcmi/CreatureService.h>
|
|
|
|
#include <vcmi/Entity.h>
|
|
|
|
#include <vcmi/FactionService.h>
|
|
|
|
#include <vcmi/HeroTypeService.h>
|
|
|
|
#include <vcmi/Services.h>
|
|
|
|
#include <vcmi/SkillService.h>
|
|
|
|
#include <vcmi/spells/Service.h>
|
|
|
|
|
2024-05-22 13:28:13 +02:00
|
|
|
std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
|
|
|
|
{
|
2024-06-02 20:33:51 +02:00
|
|
|
AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
|
|
|
|
|
|
|
|
auto it = animationFiles.find(actualPath);
|
2024-05-22 13:28:13 +02:00
|
|
|
|
|
|
|
if (it != animationFiles.end())
|
|
|
|
return it->second;
|
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
if (!CResourceHandler::get()->existsResource(actualPath))
|
|
|
|
{
|
|
|
|
animationFiles[actualPath] = nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = std::make_shared<CDefFile>(actualPath);
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
animationFiles[actualPath] = result;
|
2024-05-22 13:28:13 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & config)
|
|
|
|
{
|
|
|
|
std::string basepath;
|
|
|
|
basepath = config["basepath"].String();
|
|
|
|
|
|
|
|
JsonNode base;
|
|
|
|
base["margins"] = config["margins"];
|
|
|
|
base["width"] = config["width"];
|
|
|
|
base["height"] = config["height"];
|
|
|
|
|
|
|
|
for(const JsonNode & group : config["sequences"].Vector())
|
|
|
|
{
|
|
|
|
size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
|
|
|
|
source[groupID].clear();
|
|
|
|
|
|
|
|
for(const JsonNode & frame : group["frames"].Vector())
|
|
|
|
{
|
2024-07-16 22:14:49 +02:00
|
|
|
JsonNode toAdd = frame;
|
2024-06-02 20:33:51 +02:00
|
|
|
JsonUtils::inherit(toAdd, base);
|
|
|
|
toAdd["file"].String() = basepath + frame.String();
|
2024-07-16 22:14:49 +02:00
|
|
|
source[groupID].emplace_back(toAdd);
|
2024-06-02 20:33:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const JsonNode & node : config["images"].Vector())
|
|
|
|
{
|
|
|
|
size_t group = node["group"].Integer();
|
|
|
|
size_t frame = node["frame"].Integer();
|
|
|
|
|
|
|
|
if (source[group].size() <= frame)
|
|
|
|
source[group].resize(frame+1);
|
|
|
|
|
2024-07-16 22:14:49 +02:00
|
|
|
JsonNode toAdd = node;
|
2024-06-02 20:33:51 +02:00
|
|
|
JsonUtils::inherit(toAdd, base);
|
|
|
|
toAdd["file"].String() = basepath + node["file"].String();
|
2024-07-16 21:51:00 +02:00
|
|
|
source[group][frame] = ImageLocator(toAdd);
|
2024-06-02 20:33:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
RenderHandler::AnimationLayoutMap & RenderHandler::getAnimationLayout(const AnimationPath & path)
|
2024-05-22 13:28:13 +02:00
|
|
|
{
|
2024-06-05 16:31:37 +02:00
|
|
|
AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
|
|
|
|
|
|
|
|
auto it = animationLayouts.find(actualPath);
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
if (it != animationLayouts.end())
|
2024-05-22 13:28:13 +02:00
|
|
|
return it->second;
|
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
AnimationLayoutMap result;
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
auto defFile = getAnimationFile(actualPath);
|
2024-06-02 20:33:51 +02:00
|
|
|
if(defFile)
|
|
|
|
{
|
|
|
|
const std::map<size_t, size_t> defEntries = defFile->getEntries();
|
|
|
|
|
2024-06-05 22:16:12 +02:00
|
|
|
for (const auto & defEntry : defEntries)
|
2024-06-02 20:33:51 +02:00
|
|
|
result[defEntry.first].resize(defEntry.second);
|
|
|
|
}
|
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
auto jsonResource = actualPath.toType<EResType::JSON>();
|
|
|
|
auto configList = CResourceHandler::get()->getResourcesWithName(jsonResource);
|
2024-06-02 20:33:51 +02:00
|
|
|
|
|
|
|
for(auto & loader : configList)
|
|
|
|
{
|
2024-06-05 16:31:37 +02:00
|
|
|
auto stream = loader->load(jsonResource);
|
2024-06-02 20:33:51 +02:00
|
|
|
std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
|
|
|
|
stream->read(textData.get(), stream->getSize());
|
|
|
|
|
|
|
|
const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), path.getOriginalName());
|
|
|
|
|
|
|
|
initFromJson(result, config);
|
|
|
|
}
|
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
animationLayouts[actualPath] = result;
|
|
|
|
return animationLayouts[actualPath];
|
2024-05-22 13:28:13 +02:00
|
|
|
}
|
|
|
|
|
2024-07-21 23:11:02 +02:00
|
|
|
int RenderHandler::getScalingFactor() const
|
|
|
|
{
|
2024-08-03 22:14:51 +02:00
|
|
|
return GH.screenHandler().getScalingFactor();
|
2024-07-21 23:11:02 +02:00
|
|
|
}
|
|
|
|
|
2024-07-25 12:38:48 +02:00
|
|
|
ImageLocator RenderHandler::getLocatorForAnimationFrame(const AnimationPath & path, int frame, int group)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-06-05 16:31:37 +02:00
|
|
|
const auto & layout = getAnimationLayout(path);
|
|
|
|
if (!layout.count(group))
|
2024-07-25 12:38:48 +02:00
|
|
|
return ImageLocator(ImagePath::builtin("DEFAULT"));
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
if (frame >= layout.at(group).size())
|
2024-07-25 12:38:48 +02:00
|
|
|
return ImageLocator(ImagePath::builtin("DEFAULT"));
|
2024-06-05 16:31:37 +02:00
|
|
|
|
2024-07-16 21:51:00 +02:00
|
|
|
const auto & locator = layout.at(group).at(frame);
|
2024-07-25 12:38:48 +02:00
|
|
|
if (locator.image || locator.defFile)
|
|
|
|
return locator;
|
|
|
|
|
|
|
|
return ImageLocator(path, frame, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ISharedImage> RenderHandler::loadImageImpl(const ImageLocator & locator)
|
|
|
|
{
|
|
|
|
auto it = imageFiles.find(locator);
|
|
|
|
if (it != imageFiles.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
// TODO: order should be different:
|
|
|
|
// 1) try to find correctly scaled image
|
|
|
|
// 2) if fails -> try to find correctly transformed
|
|
|
|
// 3) if also fails -> try to find image from correct file
|
|
|
|
// 4) load missing part of the sequence
|
|
|
|
// TODO: check whether (load -> transform -> scale) or (load -> scale -> transform) order should be used for proper loading of pre-scaled data
|
|
|
|
auto imageFromFile = loadImageFromFile(locator.copyFile());
|
|
|
|
auto transformedImage = transformImage(locator.copyFileTransform(), imageFromFile);
|
|
|
|
auto scaledImage = scaleImage(locator.copyFileTransformScale(), transformedImage);
|
|
|
|
|
|
|
|
return scaledImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
|
|
|
|
{
|
2024-07-16 21:51:00 +02:00
|
|
|
if (locator.image)
|
2024-06-05 16:31:37 +02:00
|
|
|
{
|
2024-07-25 12:38:48 +02:00
|
|
|
// TODO: create EmptySharedImage class that will be instantiated if image does not exists or fails to load
|
|
|
|
return std::make_shared<SDLImageShared>(*locator.image);
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
2024-07-25 12:38:48 +02:00
|
|
|
|
|
|
|
if (locator.defFile)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-07-25 12:38:48 +02:00
|
|
|
auto defFile = getAnimationFile(*locator.defFile);
|
|
|
|
return std::make_shared<SDLImageShared>(defFile.get(), locator.defFrame, locator.defGroup);
|
2024-06-05 16:31:37 +02:00
|
|
|
}
|
2024-07-25 12:38:48 +02:00
|
|
|
|
|
|
|
throw std::runtime_error("Invalid image locator received!");
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-13 14:26:31 +02:00
|
|
|
void RenderHandler::storeCachedImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
|
|
|
|
{
|
|
|
|
imageFiles[locator] = image;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "imageCache" / (locator.toString() + ".png");
|
|
|
|
boost::filesystem::path outDir = outPath;
|
|
|
|
outDir.remove_filename();
|
|
|
|
boost::filesystem::create_directories(outDir);
|
|
|
|
image->exportBitmap(outPath , nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-07-25 12:38:48 +02:00
|
|
|
std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFile(const ImageLocator & locator)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-07-25 12:38:48 +02:00
|
|
|
if (imageFiles.count(locator))
|
|
|
|
return imageFiles.at(locator);
|
|
|
|
|
|
|
|
auto result = loadImageFromFileUncached(locator);
|
2024-09-13 14:26:31 +02:00
|
|
|
storeCachedImage(locator, result);
|
2024-06-05 22:16:12 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-25 12:38:48 +02:00
|
|
|
std::shared_ptr<ISharedImage> RenderHandler::transformImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-07-25 12:38:48 +02:00
|
|
|
if (imageFiles.count(locator))
|
|
|
|
return imageFiles.at(locator);
|
2024-06-05 22:16:12 +02:00
|
|
|
|
2024-07-25 12:38:48 +02:00
|
|
|
auto result = image;
|
2024-06-05 22:16:12 +02:00
|
|
|
|
|
|
|
if (locator.verticalFlip)
|
|
|
|
result = result->verticalFlip();
|
2024-06-05 16:31:37 +02:00
|
|
|
|
2024-06-05 22:16:12 +02:00
|
|
|
if (locator.horizontalFlip)
|
|
|
|
result = result->horizontalFlip();
|
|
|
|
|
2024-09-13 14:26:31 +02:00
|
|
|
storeCachedImage(locator, result);
|
2024-06-05 22:16:12 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-25 12:38:48 +02:00
|
|
|
std::shared_ptr<ISharedImage> RenderHandler::scaleImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
|
|
|
|
{
|
|
|
|
if (imageFiles.count(locator))
|
|
|
|
return imageFiles.at(locator);
|
|
|
|
|
2024-08-27 21:44:11 +02:00
|
|
|
auto handle = image->createImageReference(locator.layer == EImageLayer::ALL ? EImageBlitMode::OPAQUE : EImageBlitMode::ALPHA);
|
2024-07-25 12:38:48 +02:00
|
|
|
|
|
|
|
assert(locator.scalingFactor != 1); // should be filtered-out before
|
|
|
|
|
2024-08-27 21:44:11 +02:00
|
|
|
handle->setOverlayEnabled(locator.layer == EImageLayer::ALL || locator.layer == EImageLayer::OVERLAY);
|
|
|
|
handle->setBodyEnabled(locator.layer == EImageLayer::ALL || locator.layer == EImageLayer::BODY);
|
|
|
|
handle->setShadowEnabled(locator.layer == EImageLayer::ALL || locator.layer == EImageLayer::SHADOW);
|
|
|
|
if (locator.layer == EImageLayer::ALL && locator.playerColored != PlayerColor::CANNOT_DETERMINE)
|
2024-07-25 12:38:48 +02:00
|
|
|
handle->playerColored(locator.playerColored);
|
|
|
|
|
2024-07-25 20:22:27 +02:00
|
|
|
handle->scaleInteger(locator.scalingFactor);
|
2024-07-25 12:38:48 +02:00
|
|
|
|
|
|
|
// TODO: try to optimize image size (possibly even before scaling?) - trim image borders if they are completely transparent
|
|
|
|
auto result = handle->getSharedImage();
|
2024-09-13 14:26:31 +02:00
|
|
|
storeCachedImage(locator, result);
|
2024-07-25 12:38:48 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-17 11:30:16 +02:00
|
|
|
std::shared_ptr<IImage> RenderHandler::loadImage(const ImageLocator & locator, EImageBlitMode mode)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-09-12 16:26:05 +02:00
|
|
|
if (locator.scalingFactor == 0 && getScalingFactor() != 1 )
|
|
|
|
{
|
|
|
|
auto unscaledLocator = locator;
|
|
|
|
auto scaledLocator = locator;
|
|
|
|
|
|
|
|
unscaledLocator.scalingFactor = 1;
|
|
|
|
scaledLocator.scalingFactor = getScalingFactor();
|
|
|
|
auto unscaledImage = loadImageImpl(unscaledLocator);
|
|
|
|
|
|
|
|
return std::make_shared<ImageScaled>(scaledLocator, unscaledImage, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locator.scalingFactor == 0)
|
|
|
|
{
|
|
|
|
auto scaledLocator = locator;
|
|
|
|
scaledLocator.scalingFactor = getScalingFactor();
|
|
|
|
|
|
|
|
return loadImageImpl(scaledLocator)->createImageReference(mode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadImageImpl(locator)->createImageReference(mode);
|
|
|
|
}
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 09:35:13 +02:00
|
|
|
std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group, EImageBlitMode mode)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-09-12 16:26:05 +02:00
|
|
|
ImageLocator locator = getLocatorForAnimationFrame(path, frame, group);
|
2024-07-25 12:38:48 +02:00
|
|
|
return loadImage(locator, mode);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
|
|
|
|
{
|
2024-09-12 16:26:05 +02:00
|
|
|
ImageLocator locator(path);
|
|
|
|
return loadImage(locator, mode);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IImage> RenderHandler::createImage(SDL_Surface * source)
|
|
|
|
{
|
2024-09-12 16:26:05 +02:00
|
|
|
return std::make_shared<SDLImageShared>(source)->createImageReference(EImageBlitMode::ALPHA);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-08 09:35:13 +02:00
|
|
|
std::shared_ptr<CAnimation> RenderHandler::loadAnimation(const AnimationPath & path, EImageBlitMode mode)
|
2023-09-04 17:01:44 +02:00
|
|
|
{
|
2024-06-08 09:35:13 +02:00
|
|
|
return std::make_shared<CAnimation>(path, getAnimationLayout(path), mode);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-05 16:31:37 +02:00
|
|
|
void RenderHandler::addImageListEntries(const EntityService * service)
|
|
|
|
{
|
|
|
|
service->forEachBase([this](const Entity * entity, bool & stop)
|
|
|
|
{
|
|
|
|
entity->registerIcons([this](size_t index, size_t group, const std::string & listName, const std::string & imageName)
|
|
|
|
{
|
|
|
|
if (imageName.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & layout = getAnimationLayout(AnimationPath::builtin("SPRITES/" + listName));
|
|
|
|
|
|
|
|
JsonNode entry;
|
|
|
|
entry["file"].String() = imageName;
|
|
|
|
|
|
|
|
if (index >= layout[group].size())
|
|
|
|
layout[group].resize(index + 1);
|
|
|
|
|
2024-07-16 21:51:00 +02:00
|
|
|
layout[group][index] = ImageLocator(entry);
|
2024-06-05 16:31:37 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderHandler::onLibraryLoadingFinished(const Services * services)
|
|
|
|
{
|
|
|
|
addImageListEntries(services->creatures());
|
|
|
|
addImageListEntries(services->heroTypes());
|
|
|
|
addImageListEntries(services->artifacts());
|
|
|
|
addImageListEntries(services->factions());
|
|
|
|
addImageListEntries(services->spells());
|
|
|
|
addImageListEntries(services->skills());
|
|
|
|
}
|