2023-10-19 16:19:09 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CAnimation.h"
|
|
|
|
|
2024-06-02 20:33:51 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../render/IImage.h"
|
|
|
|
#include "../render/IRenderHandler.h"
|
2024-07-25 12:38:48 +02:00
|
|
|
#include "../render/IScreenHandler.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../../lib/json/JsonUtils.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-11-30 16:44:09 +02:00
|
|
|
bool CAnimation::loadFrame(size_t frame, size_t group, bool verbose)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(size(group) <= frame)
|
|
|
|
{
|
2024-11-30 16:44:09 +02:00
|
|
|
if(verbose)
|
|
|
|
printError(frame, group, "LoadFrame");
|
2023-10-19 16:19:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-04 18:24:23 +02:00
|
|
|
if(auto image = getImageImpl(frame, group, false))
|
2023-10-19 16:19:09 +02:00
|
|
|
return true;
|
|
|
|
|
2024-07-16 21:51:00 +02:00
|
|
|
std::shared_ptr<IImage> image = GH.renderHandler().loadImage(getImageLocator(frame, group), mode);
|
2024-06-02 20:33:51 +02:00
|
|
|
|
2024-06-05 22:16:12 +02:00
|
|
|
if(image)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-06-05 22:16:12 +02:00
|
|
|
images[group][frame] = image;
|
2024-07-20 20:28:47 +02:00
|
|
|
|
|
|
|
if (player.isValidPlayer())
|
|
|
|
image->playerColored(player);
|
2024-06-02 20:33:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-06-05 22:16:12 +02:00
|
|
|
else
|
2024-06-02 20:33:51 +02:00
|
|
|
{
|
2024-06-05 22:16:12 +02:00
|
|
|
// image is missing
|
|
|
|
printError(frame, group, "LoadFrame");
|
|
|
|
images[group][frame] = GH.renderHandler().loadImage(ImagePath::builtin("DEFAULT"), EImageBlitMode::OPAQUE);
|
|
|
|
return false;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAnimation::unloadFrame(size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
auto image = getImage(frame, group, false);
|
|
|
|
if(image)
|
|
|
|
{
|
|
|
|
images[group].erase(frame);
|
|
|
|
|
|
|
|
if(images[group].empty())
|
|
|
|
images.erase(group);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::exportBitmaps(const boost::filesystem::path& path) const
|
|
|
|
{
|
|
|
|
if(images.empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("Nothing to export, animation is empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::filesystem::path actualPath = path / "SPRITES" / name.getName();
|
|
|
|
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;
|
|
|
|
const auto img = imagePair.second;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-17 11:30:16 +02:00
|
|
|
CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <ImageLocator> > layout, EImageBlitMode mode):
|
2023-10-19 16:19:09 +02:00
|
|
|
name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
|
2024-06-08 09:35:13 +02:00
|
|
|
source(layout),
|
|
|
|
mode(mode)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
if(source.empty())
|
|
|
|
logAnim->error("Animation %s failed to load", Name.getOriginalName());
|
|
|
|
}
|
|
|
|
|
|
|
|
CAnimation::~CAnimation() = default;
|
|
|
|
|
|
|
|
void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
|
|
|
|
{
|
2024-07-16 21:51:00 +02:00
|
|
|
ImageLocator clone(getImageLocator(sourceFrame, sourceGroup));
|
2023-10-19 16:19:09 +02:00
|
|
|
source[targetGroup].push_back(clone);
|
|
|
|
}
|
|
|
|
|
2024-06-04 18:24:23 +02:00
|
|
|
std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
|
|
|
|
{
|
2024-11-30 16:44:09 +02:00
|
|
|
if (!loadFrame(frame, group, verbose))
|
2024-06-04 18:24:23 +02:00
|
|
|
return nullptr;
|
|
|
|
return getImageImpl(frame, group, verbose);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
auto groupIter = images.find(group);
|
|
|
|
if (groupIter != images.end())
|
|
|
|
{
|
|
|
|
auto imageIter = groupIter->second.find(frame);
|
|
|
|
if (imageIter != groupIter->second.end())
|
|
|
|
return imageIter->second;
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
printError(frame, group, "GetImage");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CAnimation::size(size_t group) const
|
|
|
|
{
|
|
|
|
auto iter = source.find(group);
|
|
|
|
if (iter != source.end())
|
|
|
|
return iter->second.size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::horizontalFlip()
|
|
|
|
{
|
2024-06-05 22:16:12 +02:00
|
|
|
for(auto & group : source)
|
|
|
|
for(size_t i = 0; i < group.second.size(); ++i)
|
|
|
|
horizontalFlip(i, group.first);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::verticalFlip()
|
|
|
|
{
|
2024-06-05 22:16:12 +02:00
|
|
|
for(auto & group : source)
|
|
|
|
for(size_t i = 0; i < group.second.size(); ++i)
|
|
|
|
verticalFlip(i, group.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::horizontalFlip(size_t frame, size_t group)
|
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
auto i1 = images.find(group);
|
|
|
|
if(i1 != images.end())
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
auto i2 = i1->second.find(frame);
|
|
|
|
|
|
|
|
if(i2 != i1->second.end())
|
|
|
|
i2->second = nullptr;
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 21:51:00 +02:00
|
|
|
auto locator = getImageLocator(frame, group);
|
2024-06-17 11:30:16 +02:00
|
|
|
locator.horizontalFlip = !locator.horizontalFlip;
|
2024-07-16 21:51:00 +02:00
|
|
|
source[group][frame] = locator;
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::verticalFlip(size_t frame, size_t group)
|
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
auto i1 = images.find(group);
|
|
|
|
if(i1 != images.end())
|
2024-06-05 22:16:12 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
auto i2 = i1->second.find(frame);
|
|
|
|
|
|
|
|
if(i2 != i1->second.end())
|
|
|
|
i2->second = nullptr;
|
2024-06-05 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 21:51:00 +02:00
|
|
|
auto locator = getImageLocator(frame, group);
|
2024-06-17 11:30:16 +02:00
|
|
|
locator.verticalFlip = !locator.verticalFlip;
|
2024-07-16 21:51:00 +02:00
|
|
|
source[group][frame] = locator;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-20 20:28:47 +02:00
|
|
|
void CAnimation::playerColored(PlayerColor targetPlayer)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-07-20 20:28:47 +02:00
|
|
|
player = targetPlayer;
|
2023-10-19 16:19:09 +02:00
|
|
|
for(auto & group : images)
|
|
|
|
for(auto & image : group.second)
|
|
|
|
image.second->playerColored(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
|
|
|
|
{
|
|
|
|
for(size_t frame = 0; frame < size(sourceGroup); ++frame)
|
|
|
|
{
|
|
|
|
duplicateImage(sourceGroup, frame, targetGroup);
|
2024-06-05 22:16:12 +02:00
|
|
|
verticalFlip(frame, targetGroup);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-16 21:51:00 +02:00
|
|
|
|
|
|
|
ImageLocator CAnimation::getImageLocator(size_t frame, size_t group) const
|
|
|
|
{
|
|
|
|
const ImageLocator & locator = source.at(group).at(frame);
|
|
|
|
|
|
|
|
if (!locator.empty())
|
|
|
|
return locator;
|
|
|
|
|
|
|
|
return ImageLocator(name, frame, group);
|
|
|
|
}
|