2011-12-14 00:23:17 +03:00
|
|
|
#pragma once
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
#include "../FunctionList.h"
|
|
|
|
//#include "../CDefHandler.h"
|
2011-12-17 21:59:59 +03:00
|
|
|
#include "../CAnimation.h"
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CCreatureAnimation.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
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
|
|
|
|
2011-12-17 21:59:59 +03:00
|
|
|
class CIntObject;
|
2013-07-06 19:10:20 +03:00
|
|
|
class CCreatureAnimation;
|
|
|
|
|
|
|
|
/// Namespace for some common controls of animations
|
|
|
|
namespace AnimationControls
|
|
|
|
{
|
|
|
|
/// get SDL_Color for creature selection borders
|
|
|
|
SDL_Color getBlueBorder();
|
|
|
|
SDL_Color getGoldBorder();
|
|
|
|
SDL_Color getNoBorder();
|
|
|
|
|
|
|
|
/// creates animation object with preset speed control
|
|
|
|
CCreatureAnimation * getAnimation(const CCreature * creature);
|
|
|
|
|
|
|
|
/// returns animation speed of specific group, taking in mind game setting
|
|
|
|
float getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t groupID);
|
|
|
|
|
|
|
|
/// returns how far projectile should move each frame
|
|
|
|
/// TODO: make it time-based
|
|
|
|
float getProjectileSpeed();
|
|
|
|
|
|
|
|
/// returns duration of full movement animation, in seconds. Needed to move animation on screen
|
|
|
|
float getMovementDuration(const CCreature * creature);
|
|
|
|
|
|
|
|
/// Returns distance on which flying creatures should during one animation loop
|
|
|
|
float getFlightDistance(const CCreature * creature);
|
|
|
|
}
|
2010-08-17 17:58:13 +03:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Class which manages animations of creatures/units inside battles
|
2013-07-06 19:10:20 +03:00
|
|
|
/// TODO: split into constant image container and class that does *control* of animation
|
2009-04-16 14:14:13 +03:00
|
|
|
class CCreatureAnimation : public CIntObject
|
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
public:
|
|
|
|
typedef boost::function<float(CCreatureAnimation *, size_t)> TSpeedController;
|
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
private:
|
2013-07-06 19:10:20 +03:00
|
|
|
std::string defName;
|
|
|
|
|
|
|
|
int fullWidth, fullHeight;
|
|
|
|
|
|
|
|
// palette, as read from def file
|
|
|
|
std::array<SDL_Color, 256> palette;
|
|
|
|
|
|
|
|
//key = id of group (note that some groups may be missing)
|
|
|
|
//value = offset of pixel data for each frame, vector size = number of frames in group
|
|
|
|
std::map<int, std::vector<unsigned int>> dataOffsets;
|
|
|
|
|
|
|
|
//animation raw data
|
|
|
|
//TODO: use vector instead?
|
|
|
|
unique_ptr<ui8[]> pixelData;
|
|
|
|
size_t pixelDataSize;
|
|
|
|
|
|
|
|
// speed of animation, measure in frames per second
|
|
|
|
float speed;
|
|
|
|
|
|
|
|
// currently displayed frame. Float to allow H3-style animations where frames
|
|
|
|
// don't display for integer number of frames
|
|
|
|
float currentFrame;
|
|
|
|
// cumulative, real-time duration of animation. Used for effects like selection border
|
|
|
|
float elapsedTime;
|
|
|
|
CCreatureAnim::EAnimType type; //type of animation being displayed
|
|
|
|
|
|
|
|
// border color, disabled if alpha = 0
|
|
|
|
SDL_Color border;
|
|
|
|
|
|
|
|
TSpeedController speedController;
|
|
|
|
|
|
|
|
bool once; // animation will be played once and the reset to idling
|
|
|
|
|
|
|
|
ui8 * getPixelAddr(SDL_Surface * dest, int ftcpX, int ftcpY) const;
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2010-08-12 08:22:48 +03:00
|
|
|
template<int bpp>
|
2013-07-06 19:10:20 +03:00
|
|
|
void putPixelAt(SDL_Surface * dest, int X, int Y, size_t index, const std::array<SDL_Color, 8> & special, SDL_Rect * destRect = nullptr) const;
|
|
|
|
|
|
|
|
template<int bpp>
|
|
|
|
void putPixel( ui8 * dest, const SDL_Color & color, size_t index, const std::array<SDL_Color, 8> & special) const;
|
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
template<int bpp>
|
2013-07-06 19:10:20 +03:00
|
|
|
void nextFrameT(SDL_Surface * dest, int x, int y, bool rotate, SDL_Rect * destRect = nullptr);
|
|
|
|
|
|
|
|
void endAnimation();
|
2011-03-22 15:19:07 +02:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
/// creates 8 special colors for current frame
|
|
|
|
std::array<SDL_Color, 8> genSpecialPalette();
|
2011-03-22 15:19:07 +02:00
|
|
|
public:
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
// function(s) that will be called when animation ends, after reset to 1st frame
|
|
|
|
// NOTE that these function will be fired only once
|
|
|
|
CFunctionList<void()> onAnimationReset;
|
|
|
|
|
|
|
|
int getWidth() const;
|
|
|
|
int getHeight() const;
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
/// name - path to .def file, relative to SPRITES/ directory
|
|
|
|
/// controller - function that will return for how long *each* frame
|
|
|
|
/// in specified group of animation should be played, measured in seconds
|
|
|
|
CCreatureAnimation(std::string name, TSpeedController speedController); //c-tor
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void setType(CCreatureAnim::EAnimType type); //sets type of animation and cleares framecount
|
|
|
|
CCreatureAnim::EAnimType getType() const; //returns type of animation
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void nextFrame(SDL_Surface * dest, int x, int y, bool rotate, SDL_Rect * destRect = nullptr);
|
|
|
|
|
|
|
|
// should be called every frame, return true when animation was reset to beginning
|
|
|
|
bool incrementFrame(float timePassed);
|
|
|
|
void setBorderColor(SDL_Color palette);
|
|
|
|
|
|
|
|
float getCurrentFrame() const; // Gets the current frame ID relative to frame group.
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
int framesInGroup(CCreatureAnim::EAnimType group) const; //retirns number of fromes in given group
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
void pause();
|
|
|
|
void play();
|
|
|
|
|
|
|
|
//helpers. TODO: move them somewhere else
|
|
|
|
bool isDead() const;
|
|
|
|
bool isIdle() const;
|
2009-04-16 14:14:13 +03:00
|
|
|
};
|