1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

def2bmp CLI command converted to CAnimation

This commit is contained in:
AlexVinS 2016-11-25 15:23:28 +03:00
parent 3c9695abcc
commit 20de754141
3 changed files with 54 additions and 17 deletions

View File

@ -22,7 +22,6 @@
#include "../lib/CCreatureHandler.h"
#include "../lib/spells/CSpellHandler.h"
#include "CMusicHandler.h"
#include "CDefHandler.h"
#include "../lib/CGeneralTextHandler.h"
#include "Graphics.h"
#include "Client.h"
@ -43,6 +42,7 @@
#include "../lib/CondSh.h"
#include "../lib/StringConstants.h"
#include "../lib/CPlayerState.h"
#include "gui/CAnimation.h"
#ifdef VCMI_WINDOWS
#include "SDL_syswm.h"
@ -764,21 +764,9 @@ void processCommand(const std::string &message)
{
std::string URI;
readed >> URI;
if (CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + URI)))
{
CDefEssential * cde = CDefHandler::giveDefEss(URI);
const bfs::path outPath = VCMIDirs::get().userCachePath() / "extracted" / URI;
bfs::create_directories(outPath);
for (size_t i = 0; i < cde->ourImages.size(); ++i)
{
const bfs::path filePath = outPath / (boost::lexical_cast<std::string>(i)+".bmp");
SDL_SaveBMP(cde->ourImages[i].bitmap, filePath.string().c_str());
}
}
else
logGlobal->errorStream() << "File not found!";
std::unique_ptr<CAnimation> anim = make_unique<CAnimation>(URI);
anim->preload();
anim->exportBitmaps(VCMIDirs::get().userCachePath() / "extracted");
}
else if(cn == "extract")
{

View File

@ -90,7 +90,7 @@ public:
void draw(SDL_Surface * where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const override;
void draw(SDL_Surface * where, SDL_Rect * dest, SDL_Rect * src, ui8 alpha=255) const override;
std::unique_ptr<IImage> scaleFast(float scale) const override;
void exportBitmap(const boost::filesystem::path & path) const override;
void playerColored(PlayerColor player) override;
void setFlagColor(PlayerColor player) override;
int width() const override;
@ -147,6 +147,8 @@ public:
std::unique_ptr<IImage> scaleFast(float scale) const override;
void exportBitmap(const boost::filesystem::path & path) const override;
void playerColored(PlayerColor player) override;
void setFlagColor(PlayerColor player) override;
int width() const override;
@ -838,6 +840,11 @@ std::unique_ptr<IImage> SDLImage::scaleFast(float scale) const
return std::unique_ptr<IImage>(ret);
}
void SDLImage::exportBitmap(const boost::filesystem::path& path) const
{
SDL_SaveBMP(surf, path.string().c_str());
}
void SDLImage::playerColored(PlayerColor player)
{
graphics->blueToPlayersAdv(surf, player);
@ -1162,6 +1169,11 @@ void CompImage::shiftPalette(int from, int howMany)
logAnim->error("CompImage::shiftPalette is not implemented");
}
void CompImage::exportBitmap(const boost::filesystem::path& path) const
{
logAnim->error("CompImage::exportBitmap is not implemented");
}
/*************************************************************************
* CAnimation for animations handling, can load part of file if needed *
@ -1289,6 +1301,39 @@ void CAnimation::initFromJson(const JsonNode & config)
}
}
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;
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 IImage * 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::init(CDefFile * file)
{
if (file)

View File

@ -32,6 +32,8 @@ public:
virtual std::unique_ptr<IImage> scaleFast(float scale) const = 0;
virtual void exportBitmap(const boost::filesystem::path & path) const = 0;
//decrease ref count, returns true if image can be deleted (refCount <= 0)
bool decreaseRef();
void increaseRef();
@ -109,6 +111,8 @@ public:
//get pointer to image from specific group, nullptr if not found
IImage * getImage(size_t frame, size_t group=0, bool verbose=true) const;
void exportBitmaps(const boost::filesystem::path & path) const;
//all available frames
void load ();
void unload();