2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CAnimation.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
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "CAnimation.h"
|
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "CDefFile.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "Graphics.h"
|
2023-01-30 00:12:43 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
|
|
|
#include "../../lib/JsonNode.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../renderSDL/SDLImage.h"
|
2023-01-30 19:55:32 +02:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<IImage> CAnimation::getFromExtraDef(std::string filename)
|
2011-02-20 11:24:53 +02:00
|
|
|
{
|
|
|
|
size_t pos = filename.find(':');
|
|
|
|
if (pos == -1)
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2023-08-23 14:07:50 +02:00
|
|
|
CAnimation anim(AnimationPath::builtinTODO(filename.substr(0, pos)));
|
2011-02-20 11:24:53 +02:00
|
|
|
pos++;
|
|
|
|
size_t frame = atoi(filename.c_str()+pos);
|
|
|
|
size_t group = 0;
|
|
|
|
pos = filename.find(':', pos);
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
2016-11-07 23:19:53 +02:00
|
|
|
pos++;
|
2011-02-20 11:24:53 +02:00
|
|
|
group = frame;
|
|
|
|
frame = atoi(filename.c_str()+pos);
|
|
|
|
}
|
|
|
|
anim.load(frame ,group);
|
2018-03-30 13:02:04 +02:00
|
|
|
auto ret = anim.images[group][frame];
|
2011-02-20 11:24:53 +02:00
|
|
|
anim.images.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
bool CAnimation::loadFrame(size_t frame, size_t group)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
if(size(group) <= frame)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
printError(frame, group, "LoadFrame");
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2018-03-30 13:02:04 +02:00
|
|
|
auto image = getImage(frame, group, false);
|
2017-09-08 13:25:12 +02:00
|
|
|
if(image)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//try to get image from def
|
2017-11-26 23:18:18 +02:00
|
|
|
if(source[group][frame].getType() == JsonNode::JsonType::DATA_NULL)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
if(defFile)
|
2013-04-22 22:51:22 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
auto frameList = defFile->getEntries();
|
2013-04-23 12:16:20 +03:00
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
if(vstd::contains(frameList, group) && frameList.at(group) > frame) // frame is present
|
2013-04-23 12:16:20 +03:00
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
images[group][frame] = std::make_shared<SDLImage>(defFile.get(), frame, group);
|
2013-04-23 12:16:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-22 22:51:22 +03:00
|
|
|
}
|
2013-04-23 12:16:20 +03:00
|
|
|
// still here? image is missing
|
2013-04-22 22:51:22 +03:00
|
|
|
|
2013-04-23 12:16:20 +03:00
|
|
|
printError(frame, group, "LoadFrame");
|
2023-02-20 18:37:33 +02:00
|
|
|
images[group][frame] = std::make_shared<SDLImage>("DEFAULT", EImageBlitMode::ALPHA);
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
else //load from separate file
|
|
|
|
{
|
2018-03-30 13:02:04 +02:00
|
|
|
auto img = getFromExtraDef(source[group][frame]["file"].String());
|
2017-09-08 13:25:12 +02:00
|
|
|
if(!img)
|
2023-02-20 18:37:33 +02:00
|
|
|
img = std::make_shared<SDLImage>(source[group][frame], EImageBlitMode::ALPHA);
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
images[group][frame] = img;
|
2011-02-06 19:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAnimation::unloadFrame(size_t frame, size_t group)
|
|
|
|
{
|
2018-03-30 13:02:04 +02:00
|
|
|
auto image = getImage(frame, group, false);
|
2018-04-07 13:34:11 +02:00
|
|
|
if(image)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
images[group].erase(frame);
|
|
|
|
|
|
|
|
if(images[group].empty())
|
2011-02-06 19:26:27 +02:00
|
|
|
images.erase(group);
|
|
|
|
return true;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
return false;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 22:51:22 +03:00
|
|
|
void CAnimation::initFromJson(const JsonNode & config)
|
|
|
|
{
|
|
|
|
std::string basepath;
|
|
|
|
basepath = config["basepath"].String();
|
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode base(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-08 13:25:12 +02:00
|
|
|
base["margins"] = config["margins"];
|
|
|
|
base["width"] = config["width"];
|
|
|
|
base["height"] = config["height"];
|
|
|
|
|
|
|
|
for(const JsonNode & group : config["sequences"].Vector())
|
2013-04-22 22:51:22 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
|
2013-04-22 22:51:22 +03:00
|
|
|
source[groupID].clear();
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
for(const JsonNode & frame : group["frames"].Vector())
|
2013-04-22 22:51:22 +03:00
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-08 13:25:12 +02:00
|
|
|
JsonUtils::inherit(toAdd, base);
|
2017-11-03 17:39:43 +02:00
|
|
|
toAdd["file"].String() = basepath + frame.String();
|
2017-09-08 13:25:12 +02:00
|
|
|
source[groupID].push_back(toAdd);
|
2013-04-22 22:51:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
for(const JsonNode & node : config["images"].Vector())
|
2013-04-22 22:51:22 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
size_t group = node["group"].Integer();
|
|
|
|
size_t frame = node["frame"].Integer();
|
2013-04-22 22:51:22 +03:00
|
|
|
|
|
|
|
if (source[group].size() <= frame)
|
|
|
|
source[group].resize(frame+1);
|
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode toAdd(JsonNode::JsonType::DATA_STRUCT);
|
2017-09-08 13:25:12 +02:00
|
|
|
JsonUtils::inherit(toAdd, base);
|
|
|
|
toAdd["file"].String() = basepath + node["file"].String();
|
|
|
|
source[group][frame] = toAdd;
|
2013-04-22 22:51:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 14:23:28 +02:00
|
|
|
void CAnimation::exportBitmaps(const boost::filesystem::path& path) const
|
|
|
|
{
|
|
|
|
if(images.empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("Nothing to export, animation is empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
boost::filesystem::path actualPath = path / "SPRITES" / name.getName();
|
2016-11-25 14:23:28 +02:00
|
|
|
boost::filesystem::create_directories(actualPath);
|
|
|
|
|
|
|
|
size_t counter = 0;
|
|
|
|
|
|
|
|
for(const auto & groupPair : images)
|
|
|
|
{
|
|
|
|
size_t group = groupPair.first;
|
|
|
|
|
|
|
|
for(const auto & imagePair : groupPair.second)
|
|
|
|
{
|
|
|
|
size_t frame = imagePair.first;
|
2018-03-30 13:02:04 +02:00
|
|
|
const auto img = imagePair.second;
|
2016-11-25 14:23:28 +02:00
|
|
|
|
|
|
|
boost::format fmt("%d_%d.bmp");
|
|
|
|
fmt % group % frame;
|
|
|
|
|
|
|
|
img->exportBitmap(actualPath / fmt.str());
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logGlobal->info("Exported %d frames to %s", counter, actualPath.string());
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:25:12 +02:00
|
|
|
void CAnimation::init()
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
if(defFile)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
const std::map<size_t, size_t> defEntries = defFile->getEntries();
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
for (auto & defEntry : defEntries)
|
|
|
|
source[defEntry.first].resize(defEntry.second);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
if (vstd::contains(graphics->imageLists, name.getName()))
|
|
|
|
initFromJson(graphics->imageLists[name.getName()]);
|
2013-04-22 22:51:22 +03:00
|
|
|
|
2023-09-04 12:03:15 +02:00
|
|
|
auto jsonResource = name.toType<EResType::JSON>();
|
2023-08-23 14:07:50 +02:00
|
|
|
auto configList = CResourceHandler::get()->getResourcesWithName(jsonResource);
|
2012-08-06 10:34:37 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
for(auto & loader : configList)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
auto stream = loader->load(jsonResource);
|
2012-08-06 10:34:37 +03:00
|
|
|
std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
|
|
|
|
stream->read(textData.get(), stream->getSize());
|
|
|
|
|
|
|
|
const JsonNode config((char*)textData.get(), stream->getSize());
|
2011-07-22 19:22:22 +03:00
|
|
|
|
2013-04-22 22:51:22 +03:00
|
|
|
initFromJson(config);
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
void CAnimation::printError(size_t frame, size_t group, std::string type) const
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name.getOriginalName(), group, frame);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CAnimation::CAnimation(const AnimationPath & Name):
|
|
|
|
name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
|
2017-09-08 13:25:12 +02:00
|
|
|
preloaded(false),
|
2018-07-25 00:36:48 +02:00
|
|
|
defFile()
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(name))
|
2018-07-25 00:36:48 +02:00
|
|
|
defFile = std::make_shared<CDefFile>(name);
|
2017-09-08 13:25:12 +02:00
|
|
|
|
|
|
|
init();
|
2017-09-04 17:41:22 +02:00
|
|
|
|
|
|
|
if(source.empty())
|
2023-08-23 14:07:50 +02:00
|
|
|
logAnim->error("Animation %s failed to load", Name.getOriginalName());
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CAnimation::CAnimation():
|
|
|
|
name(""),
|
2017-09-08 13:25:12 +02:00
|
|
|
preloaded(false),
|
2018-07-25 00:36:48 +02:00
|
|
|
defFile()
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
init();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
CAnimation::~CAnimation() = default;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
|
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
if(!source.count(sourceGroup))
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
logAnim->error("Group %d missing in %s", sourceGroup, name.getName());
|
2018-07-25 00:36:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(source[sourceGroup].size() <= sourceFrame)
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
logAnim->error("Frame [%d %d] missing in %s", sourceGroup, sourceFrame, name.getName());
|
2018-07-25 00:36:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
//todo: clone actual loaded Image object
|
|
|
|
JsonNode clone(source[sourceGroup][sourceFrame]);
|
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
if(clone.getType() == JsonNode::JsonType::DATA_NULL)
|
2016-11-07 23:19:53 +02:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
std::string temp = name.getName()+":"+std::to_string(sourceGroup)+":"+std::to_string(sourceFrame);
|
2020-10-06 01:27:04 +02:00
|
|
|
clone["file"].String() = temp;
|
2016-11-07 23:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
source[targetGroup].push_back(clone);
|
|
|
|
|
|
|
|
size_t index = source[targetGroup].size() - 1;
|
|
|
|
|
|
|
|
if(preloaded)
|
|
|
|
load(index, targetGroup);
|
|
|
|
}
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (source[group].size() <= frame)
|
|
|
|
source[group].resize(frame+1);
|
2011-07-22 19:22:22 +03:00
|
|
|
source[group][frame]["file"].String() = filename;
|
2011-02-20 11:24:53 +02:00
|
|
|
//FIXME: update image if already loaded
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose) const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto groupIter = images.find(group);
|
2011-02-06 19:26:27 +02:00
|
|
|
if (groupIter != images.end())
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto imageIter = groupIter->second.find(frame);
|
2011-02-06 19:26:27 +02:00
|
|
|
if (imageIter != groupIter->second.end())
|
|
|
|
return imageIter->second;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
if (verbose)
|
|
|
|
printError(frame, group, "GetImage");
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::load()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : source)
|
|
|
|
for (size_t image=0; image < elem.second.size(); image++)
|
2017-09-08 13:25:12 +02:00
|
|
|
loadFrame(image, elem.first);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unload()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : source)
|
|
|
|
for (size_t image=0; image < elem.second.size(); image++)
|
|
|
|
unloadFrame(image, elem.first);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:31:31 +02:00
|
|
|
void CAnimation::preload()
|
|
|
|
{
|
2016-11-07 23:19:53 +02:00
|
|
|
if(!preloaded)
|
|
|
|
{
|
|
|
|
preloaded = true;
|
|
|
|
load();
|
|
|
|
}
|
2016-10-18 09:31:31 +02:00
|
|
|
}
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
void CAnimation::loadGroup(size_t group)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (vstd::contains(source, group))
|
|
|
|
for (size_t image=0; image < source[group].size(); image++)
|
2017-09-08 13:25:12 +02:00
|
|
|
loadFrame(image, group);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unloadGroup(size_t group)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (vstd::contains(source, group))
|
|
|
|
for (size_t image=0; image < source[group].size(); image++)
|
|
|
|
unloadFrame(image, group);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::load(size_t frame, size_t group)
|
|
|
|
{
|
2017-09-08 13:25:12 +02:00
|
|
|
loadFrame(frame, group);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unload(size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
unloadFrame(frame, group);
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
size_t CAnimation::size(size_t group) const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto iter = source.find(group);
|
2011-02-06 19:26:27 +02:00
|
|
|
if (iter != source.end())
|
|
|
|
return iter->second.size();
|
2010-10-18 18:08:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-05 15:44:27 +02:00
|
|
|
void CAnimation::horizontalFlip()
|
|
|
|
{
|
|
|
|
for(auto & group : images)
|
|
|
|
for(auto & image : group.second)
|
|
|
|
image.second->horizontalFlip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::verticalFlip()
|
|
|
|
{
|
|
|
|
for(auto & group : images)
|
|
|
|
for(auto & image : group.second)
|
|
|
|
image.second->verticalFlip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::playerColored(PlayerColor player)
|
|
|
|
{
|
|
|
|
for(auto & group : images)
|
|
|
|
for(auto & image : group.second)
|
|
|
|
image.second->playerColored(player);
|
|
|
|
}
|
|
|
|
|
2017-09-05 19:04:17 +02:00
|
|
|
void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
|
|
|
|
{
|
|
|
|
for(size_t frame = 0; frame < size(sourceGroup); ++frame)
|
|
|
|
{
|
|
|
|
duplicateImage(sourceGroup, frame, targetGroup);
|
|
|
|
|
2018-03-30 13:02:04 +02:00
|
|
|
auto image = getImage(frame, targetGroup);
|
2017-09-05 19:04:17 +02:00
|
|
|
image->verticalFlip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|