mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-26 03:52:01 +02:00
Replaced raw json nodes with ImageLocator class
This commit is contained in:
parent
2f68beead1
commit
e4bed98674
@ -88,6 +88,7 @@ set(client_SRCS
|
||||
render/Colors.cpp
|
||||
render/Graphics.cpp
|
||||
render/IFont.cpp
|
||||
render/ImageLocator.cpp
|
||||
|
||||
renderSDL/CBitmapFont.cpp
|
||||
renderSDL/CBitmapHanFont.cpp
|
||||
@ -287,6 +288,7 @@ set(client_HEADERS
|
||||
render/IFont.h
|
||||
render/IImage.h
|
||||
render/IImageLoader.h
|
||||
render/ImageLocator.h
|
||||
render/IRenderHandler.h
|
||||
render/IScreenHandler.h
|
||||
|
||||
|
@ -26,17 +26,9 @@ bool CAnimation::loadFrame(size_t frame, size_t group)
|
||||
}
|
||||
|
||||
if(auto image = getImageImpl(frame, group, false))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<IImage> image;
|
||||
|
||||
//try to get image from def
|
||||
if(source[group][frame].isNull())
|
||||
image = GH.renderHandler().loadImage(name, frame, group, mode);
|
||||
else
|
||||
image = GH.renderHandler().loadImage(source[group][frame], mode);
|
||||
std::shared_ptr<IImage> image = GH.renderHandler().loadImage(source[group][frame], mode);
|
||||
|
||||
if(image)
|
||||
{
|
||||
@ -104,7 +96,7 @@ void CAnimation::printError(size_t frame, size_t group, std::string type) const
|
||||
logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name.getOriginalName(), group, frame);
|
||||
}
|
||||
|
||||
CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout, EImageBlitMode mode):
|
||||
CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <ImageLocator> > layout, EImageBlitMode mode):
|
||||
name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
|
||||
source(layout),
|
||||
mode(mode)
|
||||
@ -129,15 +121,7 @@ void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFra
|
||||
return;
|
||||
}
|
||||
|
||||
JsonNode clone(source[sourceGroup][sourceFrame]);
|
||||
|
||||
if(clone.getType() == JsonNode::JsonType::DATA_NULL)
|
||||
{
|
||||
clone["animation"].String() = name.getName();
|
||||
clone["group"].Integer() = sourceGroup;
|
||||
clone["frame"].Integer() = sourceFrame;
|
||||
}
|
||||
|
||||
ImageLocator clone(source[sourceGroup][sourceFrame]);
|
||||
source[targetGroup].push_back(clone);
|
||||
}
|
||||
|
||||
@ -195,15 +179,8 @@ void CAnimation::horizontalFlip(size_t frame, size_t group)
|
||||
// ignore - image not loaded
|
||||
}
|
||||
|
||||
JsonNode & config = source.at(group).at(frame);
|
||||
if (config.isNull())
|
||||
{
|
||||
config["animation"].String() = name.getName();
|
||||
config["frame"].Integer() = frame;
|
||||
config["group"].Integer() = group;
|
||||
}
|
||||
|
||||
config["horizontalFlip"].Bool() = !config["horizontalFlip"].Bool();
|
||||
ImageLocator & locator = source.at(group).at(frame);
|
||||
locator.horizontalFlip = !locator.horizontalFlip;
|
||||
}
|
||||
|
||||
void CAnimation::verticalFlip(size_t frame, size_t group)
|
||||
@ -217,15 +194,8 @@ void CAnimation::verticalFlip(size_t frame, size_t group)
|
||||
// ignore - image not loaded
|
||||
}
|
||||
|
||||
JsonNode & config = source.at(group).at(frame);
|
||||
if (config.isNull())
|
||||
{
|
||||
config["animation"].String() = name.getName();
|
||||
config["frame"].Integer() = frame;
|
||||
config["group"].Integer() = group;
|
||||
}
|
||||
|
||||
config["verticalFlip"].Bool() = !config["verticalFlip"].Bool();
|
||||
ImageLocator & locator = source.at(group).at(frame);
|
||||
locator.verticalFlip = !locator.verticalFlip;
|
||||
}
|
||||
|
||||
void CAnimation::playerColored(PlayerColor player)
|
||||
|
@ -10,6 +10,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "IImage.h"
|
||||
#include "ImageLocator.h"
|
||||
|
||||
#include "../../lib/GameConstants.h"
|
||||
#include "../../lib/filesystem/ResourcePath.h"
|
||||
@ -25,8 +26,8 @@ class RenderHandler;
|
||||
class CAnimation
|
||||
{
|
||||
private:
|
||||
//source[group][position] - file with this frame, if string is empty - image located in def file
|
||||
std::map<size_t, std::vector <JsonNode> > source;
|
||||
//source[group][position] - location of this frame
|
||||
std::map<size_t, std::vector <ImageLocator> > source;
|
||||
|
||||
//bitmap[group][position], store objects with loaded bitmaps
|
||||
std::map<size_t, std::map<size_t, std::shared_ptr<IImage> > > images;
|
||||
@ -36,6 +37,9 @@ private:
|
||||
|
||||
EImageBlitMode mode;
|
||||
|
||||
// current player color, if any
|
||||
PlayerColor player = PlayerColor::CANNOT_DETERMINE;
|
||||
|
||||
//loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
|
||||
bool loadFrame(size_t frame, size_t group);
|
||||
|
||||
@ -47,7 +51,7 @@ private:
|
||||
|
||||
std::shared_ptr<IImage> getImageImpl(size_t frame, size_t group=0, bool verbose=true);
|
||||
public:
|
||||
CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout, EImageBlitMode mode);
|
||||
CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <ImageLocator> > layout, EImageBlitMode mode);
|
||||
~CAnimation();
|
||||
|
||||
//duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
|
||||
|
@ -9,7 +9,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/filesystem/ResourcePath.h"
|
||||
#include "ImageLocator.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
class Services;
|
||||
@ -30,7 +30,7 @@ public:
|
||||
virtual void onLibraryLoadingFinished(const Services * services) = 0;
|
||||
|
||||
/// Loads image using given path
|
||||
virtual std::shared_ptr<IImage> loadImage(const JsonNode & config, EImageBlitMode mode) = 0;
|
||||
virtual std::shared_ptr<IImage> loadImage(const ImageLocator & locator, EImageBlitMode mode) = 0;
|
||||
virtual std::shared_ptr<IImage> loadImage(const ImagePath & path, EImageBlitMode mode) = 0;
|
||||
virtual std::shared_ptr<IImage> loadImage(const AnimationPath & path, int frame, int group, EImageBlitMode mode) = 0;
|
||||
|
||||
|
51
client/render/ImageLocator.cpp
Normal file
51
client/render/ImageLocator.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* ImageLocator.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 "ImageLocator.h"
|
||||
|
||||
#include "../../lib/json/JsonNode.h"
|
||||
|
||||
|
||||
ImageLocator::ImageLocator(const JsonNode & config)
|
||||
: image(ImagePath::fromJson(config["file"]))
|
||||
, defFile(AnimationPath::fromJson(config["defFile"]))
|
||||
, defFrame(config["defFrame"].Integer())
|
||||
, defGroup(config["defGroup"].Integer())
|
||||
, verticalFlip(config["verticalFlip"].Bool())
|
||||
, horizontalFlip(config["horizontalFlip"].Bool())
|
||||
{
|
||||
}
|
||||
|
||||
ImageLocator::ImageLocator(const ImagePath & path)
|
||||
: image(path)
|
||||
{
|
||||
}
|
||||
|
||||
ImageLocator::ImageLocator(const AnimationPath & path, int frame, int group)
|
||||
: defFile(path)
|
||||
, defFrame(frame)
|
||||
, defGroup(group)
|
||||
{
|
||||
}
|
||||
|
||||
bool ImageLocator::operator<(const ImageLocator & other) const
|
||||
{
|
||||
if(image != other.image)
|
||||
return image < other.image;
|
||||
if(defFile != other.defFile)
|
||||
return defFile < other.defFile;
|
||||
if(defGroup != other.defGroup)
|
||||
return defGroup < other.defGroup;
|
||||
if(defFrame != other.defFrame)
|
||||
return defFrame < other.defFrame;
|
||||
if(verticalFlip != other.verticalFlip)
|
||||
return verticalFlip < other.verticalFlip;
|
||||
return horizontalFlip < other.horizontalFlip;
|
||||
}
|
29
client/render/ImageLocator.h
Normal file
29
client/render/ImageLocator.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* ImageLocator.h, 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
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "../../lib/filesystem/ResourcePath.h"
|
||||
|
||||
struct ImageLocator
|
||||
{
|
||||
std::optional<ImagePath> image;
|
||||
std::optional<AnimationPath> defFile;
|
||||
int defFrame = -1;
|
||||
int defGroup = -1;
|
||||
|
||||
bool verticalFlip = false;
|
||||
bool horizontalFlip = false;
|
||||
|
||||
ImageLocator() = default;
|
||||
ImageLocator(const JsonNode & config);
|
||||
ImageLocator(const ImagePath & path);
|
||||
ImageLocator(const AnimationPath & path, int frame, int group);
|
||||
bool operator < (const ImageLocator & other) const;
|
||||
};
|
@ -27,43 +27,6 @@
|
||||
#include <vcmi/SkillService.h>
|
||||
#include <vcmi/spells/Service.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
|
||||
{
|
||||
AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
|
||||
@ -179,14 +142,14 @@ std::shared_ptr<IConstImage> RenderHandler::loadImageFromAnimationFileUncached(c
|
||||
return loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
|
||||
|
||||
const auto & config = layout.at(group).at(frame);
|
||||
if (config.isNull())
|
||||
if (config.image)
|
||||
{
|
||||
auto defFile = getAnimationFile(path);
|
||||
return std::make_shared<SDLImageConst>(defFile.get(), frame, group);
|
||||
return loadImageImpl(ImageLocator(config));
|
||||
}
|
||||
else
|
||||
{
|
||||
return loadImageImpl(ImageLocator(config));
|
||||
auto defFile = getAnimationFile(path);
|
||||
return std::make_shared<SDLImageConst>(defFile.get(), frame, group);
|
||||
}
|
||||
}
|
||||
|
||||
@ -205,10 +168,10 @@ std::shared_ptr<IConstImage> RenderHandler::loadImageImpl(const ImageLocator & l
|
||||
|
||||
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 (!locator.image)
|
||||
result = loadImageFromSingleFile(*locator.image);
|
||||
else if (locator.defFile)
|
||||
result = loadImageFromAnimationFile(*locator.defFile, locator.defFrame, locator.defGroup);
|
||||
|
||||
if (!result)
|
||||
result = loadImageFromSingleFile(ImagePath::builtin("DEFAULT"));
|
||||
@ -223,17 +186,14 @@ std::shared_ptr<IConstImage> RenderHandler::loadImageImpl(const ImageLocator & l
|
||||
return result;
|
||||
}
|
||||
|
||||
std::shared_ptr<IImage> RenderHandler::loadImage(const JsonNode & config, EImageBlitMode mode)
|
||||
std::shared_ptr<IImage> RenderHandler::loadImage(const ImageLocator & locator, EImageBlitMode mode)
|
||||
{
|
||||
if (config.isString())
|
||||
return loadImageImpl(ImageLocator(ImagePath::fromJson(config)))->createImageReference(mode);
|
||||
else
|
||||
return loadImageImpl(ImageLocator(config))->createImageReference(mode);
|
||||
return loadImageImpl(locator)->createImageReference(mode);
|
||||
}
|
||||
|
||||
std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group, EImageBlitMode mode)
|
||||
{
|
||||
return loadImageImpl(ImageLocator(path, frame, group))->createImageReference(mode);
|
||||
return loadImageFromAnimationFile(path, frame, group)->createImageReference(mode);
|
||||
}
|
||||
|
||||
std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
|
||||
|
@ -20,23 +20,7 @@ class IConstImage;
|
||||
|
||||
class RenderHandler : public IRenderHandler
|
||||
{
|
||||
using AnimationLayoutMap = std::map<size_t, std::vector<JsonNode>>;
|
||||
|
||||
struct ImageLocator
|
||||
{
|
||||
ImagePath image;
|
||||
AnimationPath animation;
|
||||
int frame = -1;
|
||||
int group = -1;
|
||||
|
||||
bool verticalFlip = false;
|
||||
bool horizontalFlip = false;
|
||||
|
||||
ImageLocator(const JsonNode & config);
|
||||
ImageLocator(const ImagePath & path);
|
||||
ImageLocator(const AnimationPath & path, int frame, int group);
|
||||
bool operator < (const ImageLocator & other) const;
|
||||
};
|
||||
using AnimationLayoutMap = std::map<size_t, std::vector<ImageLocator>>;
|
||||
|
||||
std::map<AnimationPath, std::shared_ptr<CDefFile>> animationFiles;
|
||||
std::map<AnimationPath, AnimationLayoutMap> animationLayouts;
|
||||
@ -58,7 +42,7 @@ public:
|
||||
// IRenderHandler implementation
|
||||
void onLibraryLoadingFinished(const Services * services) override;
|
||||
|
||||
std::shared_ptr<IImage> loadImage(const JsonNode & config, EImageBlitMode mode) override;
|
||||
std::shared_ptr<IImage> loadImage(const ImageLocator & locator, EImageBlitMode mode) override;
|
||||
std::shared_ptr<IImage> loadImage(const ImagePath & path, EImageBlitMode mode) override;
|
||||
std::shared_ptr<IImage> loadImage(const AnimationPath & path, int frame, int group, EImageBlitMode mode) override;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user