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-05-22 13:28:13 +02:00
|
|
|
#include "../render/CAnimation.h"
|
|
|
|
#include "../render/CDefFile.h"
|
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
#include "../../lib/json/JsonUtils.h"
|
|
|
|
#include "../../lib/filesystem/Filesystem.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-06-05 22:16:12 +02:00
|
|
|
RenderHandler::ImageLocator::ImageLocator(const JsonNode & config)
|
|
|
|
: image(ImagePath::fromJson(config["file"]))
|
|
|
|
, animation(AnimationPath::fromJson(config["animation"]))
|
|
|
|
, frame(config["frame"].Integer())
|
|
|
|
, group(config["group"].Integer())
|
|
|
|
, verticalFlip(config["verticalFlip"].Bool())
|
|
|
|
, horizontalFlip(config["horizontalFlip"].Bool())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderHandler::ImageLocator::ImageLocator(const ImagePath & path)
|
|
|
|
: image(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderHandler::ImageLocator::ImageLocator(const AnimationPath & path, int frame, int group)
|
|
|
|
: animation(path)
|
|
|
|
, frame(frame)
|
|
|
|
, group(group)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RenderHandler::ImageLocator::operator<(const ImageLocator & other) const
|
|
|
|
{
|
|
|
|
if(image != other.image)
|
|
|
|
return image < other.image;
|
|
|
|
if(animation != other.animation)
|
|
|
|
return animation < other.animation;
|
|
|
|
if(group != other.group)
|
|
|
|
return group < other.group;
|
|
|
|
if(frame != other.frame)
|
|
|
|
return frame < other.frame;
|
|
|
|
if(verticalFlip != other.verticalFlip)
|
|
|
|
return verticalFlip < other.verticalFlip;
|
|
|
|
return horizontalFlip < other.horizontalFlip;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
JsonNode toAdd;
|
|
|
|
JsonUtils::inherit(toAdd, base);
|
|
|
|
toAdd["file"].String() = basepath + frame.String();
|
|
|
|
source[groupID].push_back(toAdd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
JsonNode toAdd;
|
|
|
|
JsonUtils::inherit(toAdd, base);
|
|
|
|
toAdd["file"].String() = basepath + node["file"].String();
|
|
|
|
source[group][frame] = toAdd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-05 22:16:12 +02:00
|
|
|
std::shared_ptr<IConstImage> RenderHandler::loadImageFromSingleFile(const ImagePath & path)
|
2024-05-22 13:28:13 +02:00
|
|
|
{
|
2024-06-08 09:35:13 +02:00
|
|
|
auto result = std::make_shared<SDLImageConst>(path);
|
2024-06-05 22:16:12 +02:00
|
|
|
imageFiles[ImageLocator(path)] = result;
|
|
|
|
return result;
|
|
|
|
}
|
2024-05-22 13:28:13 +02:00
|
|
|
|
2024-06-05 22:16:12 +02:00
|
|
|
std::shared_ptr<IConstImage> RenderHandler::loadImageFromAnimationFileUncached(const AnimationPath & path, int frame, int group)
|
|
|
|
{
|
2024-06-05 16:31:37 +02:00
|
|
|
const auto & layout = getAnimationLayout(path);
|
|
|
|
if (!layout.count(group))
|
2024-06-05 22:16:12 +02:00
|
|
|
return loadImageFromSingleFile(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-06-05 22:16:12 +02:00
|
|
|
return loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
|
2024-06-05 16:31:37 +02:00
|
|
|
|
|
|
|
const auto & config = layout.at(group).at(frame);
|
|
|
|
if (config.isNull())
|
|
|
|
{
|
|
|
|
auto defFile = getAnimationFile(path);
|
2024-06-05 22:16:12 +02:00
|
|
|
return std::make_shared<SDLImageConst>(defFile.get(), frame, group);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadImageImpl(ImageLocator(config));
|
2024-06-05 16:31:37 +02:00
|
|
|
}
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IConstImage> RenderHandler::loadImageFromAnimationFile(const AnimationPath & path, int frame, int group)
|
|
|
|
{
|
|
|
|
auto result = loadImageFromAnimationFileUncached(path, frame, group);
|
|
|
|
imageFiles[ImageLocator(path, frame, group)] = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IConstImage> RenderHandler::loadImageImpl(const ImageLocator & locator)
|
|
|
|
{
|
|
|
|
auto it = imageFiles.find(locator);
|
|
|
|
if (it != imageFiles.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
std::shared_ptr<IConstImage> result;
|
|
|
|
|
|
|
|
if (!locator.image.empty())
|
|
|
|
result = loadImageFromSingleFile(locator.image);
|
|
|
|
else if (!locator.animation.empty())
|
|
|
|
result = loadImageFromAnimationFile(locator.animation, locator.frame, locator.group);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
result = loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
imageFiles[locator] = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-08 09:35:13 +02:00
|
|
|
std::shared_ptr<IImage> RenderHandler::loadImage(const JsonNode & config, EImageBlitMode mode)
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
|
|
|
if (config.isString())
|
2024-06-08 09:35:13 +02:00
|
|
|
return loadImageImpl(ImageLocator(ImagePath::fromJson(config)))->createImageReference(mode);
|
2024-06-05 22:16:12 +02:00
|
|
|
else
|
2024-06-08 09:35:13 +02:00
|
|
|
return loadImageImpl(ImageLocator(config))->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-06-08 09:35:13 +02:00
|
|
|
return loadImageImpl(ImageLocator(path, frame, group))->createImageReference(mode);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
|
|
|
|
{
|
2024-06-08 09:35:13 +02:00
|
|
|
return loadImageImpl(ImageLocator(path))->createImageReference(mode);
|
2023-09-04 17:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IImage> RenderHandler::createImage(SDL_Surface * source)
|
|
|
|
{
|
2024-06-08 09:35:13 +02:00
|
|
|
return std::make_shared<SDLImageConst>(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);
|
|
|
|
|
|
|
|
layout[group][index] = entry;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderHandler::onLibraryLoadingFinished(const Services * services)
|
|
|
|
{
|
|
|
|
addImageListEntries(services->creatures());
|
|
|
|
addImageListEntries(services->heroTypes());
|
|
|
|
addImageListEntries(services->artifacts());
|
|
|
|
addImageListEntries(services->factions());
|
|
|
|
addImageListEntries(services->spells());
|
|
|
|
addImageListEntries(services->skills());
|
|
|
|
}
|