From 20de754141c109d40bfd0604e17f647317a89adf Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Fri, 25 Nov 2016 15:23:28 +0300 Subject: [PATCH] def2bmp CLI command converted to CAnimation --- client/CMT.cpp | 20 ++++------------- client/gui/CAnimation.cpp | 47 ++++++++++++++++++++++++++++++++++++++- client/gui/CAnimation.h | 4 ++++ 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/client/CMT.cpp b/client/CMT.cpp index c3f589a40..e62bc0136 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -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(i)+".bmp"); - SDL_SaveBMP(cde->ourImages[i].bitmap, filePath.string().c_str()); - } - } - else - logGlobal->errorStream() << "File not found!"; + std::unique_ptr anim = make_unique(URI); + anim->preload(); + anim->exportBitmaps(VCMIDirs::get().userCachePath() / "extracted"); } else if(cn == "extract") { diff --git a/client/gui/CAnimation.cpp b/client/gui/CAnimation.cpp index 2ea5f511f..91cde61f6 100644 --- a/client/gui/CAnimation.cpp +++ b/client/gui/CAnimation.cpp @@ -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 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 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 SDLImage::scaleFast(float scale) const return std::unique_ptr(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) diff --git a/client/gui/CAnimation.h b/client/gui/CAnimation.h index 5765189f5..0e71a51a8 100644 --- a/client/gui/CAnimation.h +++ b/client/gui/CAnimation.h @@ -32,6 +32,8 @@ public: virtual std::unique_ptr 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();