2010-10-18 18:08:59 +03:00
|
|
|
/*
|
|
|
|
* CAnimation.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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../lib/vcmi_endian.h"
|
|
|
|
#include "Geometries.h"
|
|
|
|
#include "../../lib/GameConstants.h"
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2020-05-05 14:05:15 +02:00
|
|
|
#ifdef IN
|
|
|
|
#undef IN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OUT
|
|
|
|
#undef OUT
|
|
|
|
#endif
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
class JsonNode;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
struct SDL_Surface;
|
2016-11-07 23:19:53 +02:00
|
|
|
class CDefFile;
|
2020-01-25 11:21:26 +02:00
|
|
|
class ColorShifter;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
/*
|
|
|
|
* Base class for images, can be used for non-animation pictures as well
|
|
|
|
*/
|
|
|
|
class IImage
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
public:
|
2022-12-14 12:04:37 +02:00
|
|
|
using SpecialPalette = std::array<SDL_Color, 7>;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//draws image on surface "where" at position
|
2022-11-27 22:50:18 +02:00
|
|
|
virtual void draw(SDL_Surface * where, int posX = 0, int posY = 0, const Rect * src = nullptr, ui8 alpha = 255) const=0;
|
|
|
|
virtual void draw(SDL_Surface * where, const SDL_Rect * dest, const SDL_Rect * src, ui8 alpha = 255) const = 0;
|
2016-11-07 23:19:53 +02:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
virtual std::shared_ptr<IImage> scaleFast(float scale) const = 0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2016-11-25 14:23:28 +02:00
|
|
|
virtual void exportBitmap(const boost::filesystem::path & path) const = 0;
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//Change palette to specific player
|
2013-03-03 20:06:03 +03:00
|
|
|
virtual void playerColored(PlayerColor player)=0;
|
2016-11-04 16:05:56 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
//set special color for flag
|
|
|
|
virtual void setFlagColor(PlayerColor player)=0;
|
2016-11-04 16:05:56 +02:00
|
|
|
|
2022-11-25 11:46:47 +02:00
|
|
|
//test transparency of specific pixel
|
|
|
|
virtual bool isTransparent(const Point & coords) const = 0;
|
|
|
|
|
|
|
|
virtual Point dimensions() const = 0;
|
2022-12-11 22:09:57 +02:00
|
|
|
int width() const;
|
|
|
|
int height() const;
|
2016-11-07 23:15:46 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
//only indexed bitmaps, 16 colors maximum
|
|
|
|
virtual void shiftPalette(int from, int howMany) = 0;
|
2020-01-26 04:49:53 +02:00
|
|
|
virtual void adjustPalette(const ColorShifter * shifter) = 0;
|
2020-01-25 11:21:26 +02:00
|
|
|
virtual void resetPalette() = 0;
|
2016-11-07 23:15:46 +02:00
|
|
|
|
2022-12-14 12:04:37 +02:00
|
|
|
//only indexed bitmaps with 7 special colors
|
|
|
|
virtual void setSpecialPallete(const SpecialPalette & SpecialPalette) = 0;
|
2017-09-05 17:59:26 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
virtual void horizontalFlip() = 0;
|
|
|
|
virtual void verticalFlip() = 0;
|
2016-11-07 23:15:46 +02:00
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
IImage();
|
2018-04-07 13:34:11 +02:00
|
|
|
virtual ~IImage();
|
2022-11-25 00:26:14 +02:00
|
|
|
|
|
|
|
/// loads image from specified file. Returns 0-sized images on failure
|
|
|
|
static std::shared_ptr<IImage> createFromFile( const std::string & path );
|
2011-02-06 19:26:27 +02:00
|
|
|
};
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
/// Class for handling animation
|
2011-02-06 19:26:27 +02:00
|
|
|
class CAnimation
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
//source[group][position] - file with this frame, if string is empty - image located in def file
|
2011-07-21 21:29:22 +03:00
|
|
|
std::map<size_t, std::vector <JsonNode> > source;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
//bitmap[group][position], store objects with loaded bitmaps
|
2018-04-07 13:34:11 +02:00
|
|
|
std::map<size_t, std::map<size_t, std::shared_ptr<IImage> > > images;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-01-01 22:26:39 +02:00
|
|
|
//animation file name
|
|
|
|
std::string name;
|
|
|
|
|
2016-10-18 09:31:31 +02:00
|
|
|
bool preloaded;
|
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
std::shared_ptr<CDefFile> defFile;
|
2017-09-08 13:25:12 +02:00
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
//loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
|
2017-09-08 13:25:12 +02:00
|
|
|
bool loadFrame(size_t frame, size_t group);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
//unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
|
|
|
|
bool unloadFrame(size_t frame, size_t group);
|
|
|
|
|
|
|
|
//initialize animation from file
|
2013-04-22 22:51:22 +03:00
|
|
|
void initFromJson(const JsonNode & input);
|
2017-09-08 13:25:12 +02:00
|
|
|
void init();
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
//to get rid of copy-pasting error message :]
|
|
|
|
void printError(size_t frame, size_t group, std::string type) const;
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
//not a very nice method to get image from another def file
|
|
|
|
//TODO: remove after implementing resource manager
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<IImage> getFromExtraDef(std::string filename);
|
2011-02-20 11:24:53 +02:00
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
public:
|
2018-07-25 00:36:48 +02:00
|
|
|
CAnimation(std::string Name);
|
2010-10-18 18:08:59 +03:00
|
|
|
CAnimation();
|
|
|
|
~CAnimation();
|
|
|
|
|
2016-11-07 23:19:53 +02:00
|
|
|
//duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
|
|
|
|
//and loads it if animation is preloaded
|
|
|
|
void duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup);
|
|
|
|
|
2020-01-25 11:21:26 +02:00
|
|
|
// adjust the color of the animation, used in battle spell effects, e.g. Cloned objects
|
2020-01-26 04:49:53 +02:00
|
|
|
void shiftColor(const ColorShifter * shifter);
|
2020-01-25 11:21:26 +02:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//add custom surface to the selected position.
|
2011-02-20 11:24:53 +02:00
|
|
|
void setCustom(std::string filename, size_t frame, size_t group=0);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<IImage> getImage(size_t frame, size_t group=0, bool verbose=true) const;
|
2010-10-25 22:37:21 +03:00
|
|
|
|
2016-11-25 14:23:28 +02:00
|
|
|
void exportBitmaps(const boost::filesystem::path & path) const;
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
//all available frames
|
|
|
|
void load ();
|
|
|
|
void unload();
|
2016-10-18 09:31:31 +02:00
|
|
|
void preload();
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
//all frames from group
|
|
|
|
void loadGroup (size_t group);
|
|
|
|
void unloadGroup(size_t group);
|
|
|
|
|
|
|
|
//single image
|
|
|
|
void load (size_t frame, size_t group=0);
|
|
|
|
void unload(size_t frame, size_t group=0);
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//total count of frames in group (including not loaded)
|
|
|
|
size_t size(size_t group=0) const;
|
2017-09-05 15:44:27 +02:00
|
|
|
|
|
|
|
void horizontalFlip();
|
|
|
|
void verticalFlip();
|
|
|
|
void playerColored(PlayerColor player);
|
2017-09-05 19:04:17 +02:00
|
|
|
|
2017-09-05 19:45:29 +02:00
|
|
|
void createFlippedGroup(const size_t sourceGroup, const size_t targetGroup);
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
2015-01-31 00:37:28 +02:00
|
|
|
|
2015-02-18 16:31:55 +02:00
|
|
|
const float DEFAULT_DELTA = 0.05f;
|
|
|
|
|
2015-01-31 00:37:28 +02:00
|
|
|
class CFadeAnimation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class EMode
|
|
|
|
{
|
|
|
|
NONE, IN, OUT
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
float delta;
|
|
|
|
SDL_Surface * fadingSurface;
|
|
|
|
bool fading;
|
|
|
|
float fadingCounter;
|
|
|
|
bool shouldFreeSurface;
|
2016-01-29 16:16:14 +02:00
|
|
|
|
2015-01-31 00:37:28 +02:00
|
|
|
float initialCounter() const;
|
|
|
|
bool isFinished() const;
|
|
|
|
public:
|
|
|
|
EMode fadingMode;
|
|
|
|
|
|
|
|
CFadeAnimation();
|
|
|
|
~CFadeAnimation();
|
2015-02-02 18:42:42 +02:00
|
|
|
void init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd = false, float animDelta = DEFAULT_DELTA);
|
2015-01-31 00:37:28 +02:00
|
|
|
void update();
|
|
|
|
void draw(SDL_Surface * targetSurface, const SDL_Rect * sourceRect, SDL_Rect * destRect);
|
|
|
|
bool isFading() const { return fading; }
|
|
|
|
};
|