2011-12-14 00:23:17 +03:00
|
|
|
#pragma once
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2013-02-05 22:56:28 +03:00
|
|
|
#include "../lib/vcmi_endian.h"
|
2013-04-07 14:52:07 +03:00
|
|
|
#include "gui/CIntObject.h"
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct SDL_Surface;
|
2011-02-06 19:26:27 +02:00
|
|
|
class SDLImageLoader;
|
|
|
|
class CompImageLoader;
|
2011-07-21 21:29:22 +03:00
|
|
|
class JsonNode;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Class for def loading, methods are based on CDefHandler
|
|
|
|
/// After loading will store general info (palette and frame offsets) and pointer to file itself
|
2010-10-18 18:08:59 +03:00
|
|
|
class CDefFile
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
struct SSpriteDef
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
ui32 size;
|
|
|
|
ui32 format; /// format in which pixel data is stored
|
|
|
|
ui32 fullWidth; /// full width and height of frame, including borders
|
|
|
|
ui32 fullHeight;
|
|
|
|
ui32 width; /// width and height of pixel data, borders excluded
|
|
|
|
ui32 height;
|
|
|
|
si32 leftMargin;
|
|
|
|
si32 topMargin;
|
2013-02-05 22:56:28 +03:00
|
|
|
} PACKED_STRUCT;
|
2010-10-18 18:08:59 +03:00
|
|
|
//offset[group][frame] - offset of frame data in file
|
2011-02-06 19:26:27 +02:00
|
|
|
std::map<size_t, std::vector <size_t> > offset;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 * data;
|
2011-02-06 19:26:27 +02:00
|
|
|
SDL_Color * palette;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
CDefFile(std::string Name);
|
|
|
|
~CDefFile();
|
|
|
|
|
|
|
|
//load frame as SDL_Surface
|
2011-02-06 19:26:27 +02:00
|
|
|
template<class ImageLoader>
|
|
|
|
void loadFrame(size_t frame, size_t group, ImageLoader &loader) const;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
const std::map<size_t, size_t> getEntries() const;
|
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
|
|
|
int refCount;
|
|
|
|
public:
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//draws image on surface "where" at position
|
2013-06-26 14:18:27 +03:00
|
|
|
virtual void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const=0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//decrease ref count, returns true if image can be deleted (refCount <= 0)
|
|
|
|
bool decreaseRef();
|
|
|
|
void increaseRef();
|
2010-10-18 18:08:59 +03:00
|
|
|
|
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;
|
2011-02-06 19:26:27 +02:00
|
|
|
virtual int width() const=0;
|
|
|
|
virtual int height() const=0;
|
|
|
|
IImage();
|
|
|
|
virtual ~IImage() {};
|
|
|
|
};
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
/*
|
|
|
|
* Wrapper around SDL_Surface
|
|
|
|
*/
|
|
|
|
class SDLImage : public IImage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//Surface without empty borders
|
|
|
|
SDL_Surface * surf;
|
|
|
|
//size of left and top borders
|
2011-12-22 16:05:19 +03:00
|
|
|
Point margins;
|
2011-02-06 19:26:27 +02:00
|
|
|
//total size including borders
|
2011-12-22 16:05:19 +03:00
|
|
|
Point fullSize;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
public:
|
|
|
|
//Load image from def file
|
|
|
|
SDLImage(CDefFile *data, size_t frame, size_t group=0, bool compressed=false);
|
|
|
|
//Load from bitmap file
|
|
|
|
SDLImage(std::string filename, bool compressed=false);
|
|
|
|
//Create using existing surface, extraRef will increase refcount on SDL_Surface
|
|
|
|
SDLImage(SDL_Surface * from, bool extraRef);
|
|
|
|
~SDLImage();
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const;
|
2013-03-03 20:06:03 +03:00
|
|
|
void playerColored(PlayerColor player);
|
2011-02-06 19:26:27 +02:00
|
|
|
int width() const;
|
|
|
|
int height() const;
|
|
|
|
|
|
|
|
friend class SDLImageLoader;
|
|
|
|
};
|
2011-01-01 22:26:39 +02:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
/*
|
|
|
|
* RLE-compressed image data for 8-bit images with alpha-channel, currently far from finished
|
|
|
|
* primary purpose is not high compression ratio but fast drawing.
|
|
|
|
* Consist of repeatable segments with format similar to H3 def compression:
|
|
|
|
* 1st byte:
|
|
|
|
* if (byte == 0xff)
|
|
|
|
* raw data, opaque and semi-transparent data always in separate blocks
|
|
|
|
* else
|
|
|
|
* RLE-compressed image data with this color
|
|
|
|
* 2nd byte = size of segment
|
|
|
|
* raw data (if any)
|
|
|
|
*/
|
|
|
|
class CompImage : public IImage
|
|
|
|
{
|
|
|
|
//x,y - margins, w,h - sprite size
|
2011-12-22 16:05:19 +03:00
|
|
|
Rect sprite;
|
2011-02-06 19:26:27 +02:00
|
|
|
//total size including borders
|
2011-12-22 16:05:19 +03:00
|
|
|
Point fullSize;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
//RLE-d data
|
|
|
|
ui8 * surf;
|
|
|
|
//array of offsets for each line
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 * line;
|
2011-02-06 19:26:27 +02:00
|
|
|
//palette
|
|
|
|
SDL_Color *palette;
|
|
|
|
|
|
|
|
//Used internally to blit one block of data
|
2011-02-20 11:24:53 +02:00
|
|
|
template<int bpp, int dir>
|
2011-03-22 15:19:07 +02:00
|
|
|
void BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha) const;
|
|
|
|
void BlitBlockWithBpp(ui8 bpp, ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha, bool rotated) const;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
public:
|
|
|
|
//Load image from def file
|
|
|
|
CompImage(const CDefFile *data, size_t frame, size_t group=0);
|
|
|
|
//TODO: load image from SDL_Surface
|
|
|
|
CompImage(SDL_Surface * surf);
|
|
|
|
~CompImage();
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
void draw(SDL_Surface *where, int posX=0, int posY=0, Rect *src=nullptr, ui8 alpha=255) const;
|
2013-03-03 20:06:03 +03:00
|
|
|
void playerColored(PlayerColor player);
|
2011-02-06 19:26:27 +02:00
|
|
|
int width() const;
|
|
|
|
int height() const;
|
|
|
|
|
|
|
|
friend class CompImageLoader;
|
|
|
|
};
|
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
|
|
|
|
std::map<size_t, std::map<size_t, IImage* > > images;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-01-01 22:26:39 +02:00
|
|
|
//animation file name
|
|
|
|
std::string name;
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//if true all frames will be stored in compressed (RLE) state
|
2011-01-01 22:26:39 +02:00
|
|
|
const bool compressed;
|
|
|
|
|
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
|
|
|
|
bool loadFrame(CDefFile * file, size_t frame, size_t group);
|
|
|
|
|
|
|
|
//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);
|
2010-10-18 18:08:59 +03:00
|
|
|
void init(CDefFile * file);
|
|
|
|
|
|
|
|
//try to open def file
|
|
|
|
CDefFile * getFile() const;
|
|
|
|
|
|
|
|
//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
|
|
|
|
IImage * getFromExtraDef(std::string filename);
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
CAnimation(std::string Name, bool Compressed = false);
|
|
|
|
CAnimation();
|
|
|
|
~CAnimation();
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
//static method for debugging - print info about loaded animations
|
2011-03-22 15:19:07 +02:00
|
|
|
static void getAnimInfo();
|
|
|
|
static std::set<CAnimation*> loadedAnims;
|
|
|
|
|
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
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
//get pointer to image from specific group, nullptr if not found
|
2011-02-06 19:26:27 +02:00
|
|
|
IImage * getImage(size_t frame, size_t group=0, bool verbose=true) const;
|
2010-10-25 22:37:21 +03:00
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
//all available frames
|
|
|
|
void load ();
|
|
|
|
void unload();
|
|
|
|
|
|
|
|
//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;
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
|
|
|
|
/// Class for displaying one image from animation
|
2010-11-15 17:15:00 +02:00
|
|
|
class CAnimImage: public CIntObject
|
|
|
|
{
|
|
|
|
private:
|
2011-02-20 11:24:53 +02:00
|
|
|
CAnimation* anim;
|
2011-02-06 19:26:27 +02:00
|
|
|
//displayed frame/group
|
|
|
|
size_t frame;
|
2010-11-15 17:15:00 +02:00
|
|
|
size_t group;
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor player;
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 flags;
|
2011-02-20 11:24:53 +02:00
|
|
|
|
|
|
|
void init();
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
public:
|
2011-12-14 00:23:17 +03:00
|
|
|
CAnimImage(std::string name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
|
|
|
|
CAnimImage(CAnimation* anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
|
2010-11-15 17:15:00 +02:00
|
|
|
~CAnimImage();//d-tor
|
|
|
|
|
2011-05-28 13:05:25 +03:00
|
|
|
//size of animation
|
|
|
|
size_t size();
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
//change displayed frame on this one
|
2011-04-06 23:30:59 +03:00
|
|
|
void setFrame(size_t Frame, size_t Group=0);
|
2011-02-20 11:24:53 +02:00
|
|
|
|
|
|
|
//makes image player-colored
|
2013-03-03 20:06:03 +03:00
|
|
|
void playerColored(PlayerColor player);
|
2011-02-20 11:24:53 +02:00
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void showAll(SDL_Surface * to);
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Base class for displaying animation, used as superclass for different animations
|
2010-11-15 17:15:00 +02:00
|
|
|
class CShowableAnim: public CIntObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum EFlags
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
BASE=1, //base frame will be blitted before current one
|
|
|
|
HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
|
|
|
|
VERTICAL_FLIP=4, //TODO: will be displayed rotated
|
|
|
|
USE_RLE=8, //RLE-d version, support full alpha-channel for 8-bit images
|
2011-02-20 11:24:53 +02:00
|
|
|
PLAYER_COLORED=16, //TODO: all loaded images will be player-colored
|
2012-06-13 16:04:06 +03:00
|
|
|
PLAY_ONCE=32 //play animation only once and stop at last frame
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
|
|
|
protected:
|
|
|
|
CAnimation anim;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
size_t group, frame;//current frame
|
|
|
|
|
|
|
|
size_t first, last; //animation range
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//TODO: replace with time delay(needed for battles)
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 frameDelay;//delay in frames of each image
|
|
|
|
ui32 value;//how many times current frame was showed
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 flags;//Flags from EFlags enum
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
//blit image with optional rotation, fitting into rect, etc
|
2011-02-06 19:26:27 +02:00
|
|
|
void blitImage(size_t frame, size_t group, SDL_Surface *to);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
//For clipping in rect, offsets of picture coordinates
|
|
|
|
int xOffset, yOffset;
|
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
ui8 alpha;
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
public:
|
|
|
|
//called when next animation sequence is required
|
2013-06-26 14:18:27 +03:00
|
|
|
std::function<void()> callback;
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
//Set per-surface alpha, 0 = transparent, 255 = opaque
|
2011-12-14 00:23:17 +03:00
|
|
|
void setAlpha(ui32 alphaValue);
|
2011-03-22 15:19:07 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
CShowableAnim(int x, int y, std::string name, ui8 flags=0, ui32 Delay=4, size_t Group=0);
|
2010-11-15 17:15:00 +02:00
|
|
|
~CShowableAnim();
|
|
|
|
|
|
|
|
//set animation to group or part of group
|
|
|
|
bool set(size_t Group);
|
|
|
|
bool set(size_t Group, size_t from, size_t to=-1);
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//set rotation flags
|
|
|
|
void rotate(bool on, bool vertical=false);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
//move displayed part of picture (if picture is clipped to rect)
|
2011-02-06 19:26:27 +02:00
|
|
|
void clipRect(int posX, int posY, int width, int height);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
//set frame to first, call callback
|
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
//show current frame and increase counter
|
2011-12-22 16:05:19 +03:00
|
|
|
void show(SDL_Surface * to);
|
|
|
|
void showAll(SDL_Surface * to);
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
/// Creature-dependend animations like attacking, moving,...
|
2010-11-15 17:15:00 +02:00
|
|
|
class CCreatureAnim: public CShowableAnim
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum EAnimType // list of creature animations, numbers were taken from def files
|
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
MOVING=0,
|
|
|
|
MOUSEON=1,
|
|
|
|
HOLDING=2,
|
2011-02-06 19:26:27 +02:00
|
|
|
HITTED=3,
|
|
|
|
DEFENCE=4,
|
|
|
|
DEATH=5,
|
|
|
|
//DEATH2=6, //unused?
|
2013-07-06 19:10:20 +03:00
|
|
|
TURN_L=7,
|
2011-02-06 19:26:27 +02:00
|
|
|
TURN_R=8, //same
|
|
|
|
//TURN_L2=9, //identical to previous?
|
|
|
|
//TURN_R2=10,
|
|
|
|
ATTACK_UP=11,
|
|
|
|
ATTACK_FRONT=12,
|
|
|
|
ATTACK_DOWN=13,
|
|
|
|
SHOOT_UP=14,
|
|
|
|
SHOOT_FRONT=15,
|
|
|
|
SHOOT_DOWN=16,
|
|
|
|
CAST_UP=17,
|
|
|
|
CAST_FRONT=18,
|
|
|
|
CAST_DOWN=19,
|
2013-07-06 19:10:20 +03:00
|
|
|
MOVE_START=20,
|
|
|
|
MOVE_END=21,
|
|
|
|
DEAD = 22 // new group, used to show dead stacks. If empty - last frame from "DEATH" will be copied here
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2011-02-06 19:26:27 +02:00
|
|
|
//queue of animations waiting to be displayed
|
2010-11-15 17:15:00 +02:00
|
|
|
std::queue<EAnimType> queue;
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
//this function is used as callback if preview flag was set during construction
|
|
|
|
void loopPreview(bool warMachine);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
//change anim to next if queue is not empty, call callback othervice
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
//add sequence to the end of queue
|
|
|
|
void addLast(EAnimType newType);
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
void startPreview(bool warMachine);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
//clear queue and set animation to this sequence
|
|
|
|
void clearAndSet(EAnimType type);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
CCreatureAnim(int x, int y, std::string name, Rect picPos,
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 flags= USE_RLE, EAnimType = HOLDING );
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
};
|