2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* Images.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
|
|
|
|
*
|
|
|
|
*/
|
2014-07-15 10:14:49 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../gui/CIntObject.h"
|
2022-12-08 19:41:02 +02:00
|
|
|
#include "../battle/BattleConstants.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-01-18 17:32:57 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2023-01-17 22:01:35 +02:00
|
|
|
class Rect;
|
2023-01-18 17:32:57 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
struct SDL_Surface;
|
2023-01-30 00:12:43 +02:00
|
|
|
struct SDL_Color;
|
2014-07-15 10:14:49 +03:00
|
|
|
class CAnimImage;
|
|
|
|
class CLabel;
|
|
|
|
class CAnimation;
|
2020-03-31 19:52:22 +02:00
|
|
|
class IImage;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
// Image class
|
|
|
|
class CPicture : public CIntObject
|
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> bg;
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// if set, only specified section of internal image will be rendered
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<Rect> srcRect;
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
/// If set to true, iamge will be redrawn on each frame
|
|
|
|
bool needRefresh;
|
|
|
|
|
|
|
|
/// If set to false, image will not be rendered
|
|
|
|
/// Deprecated, use CIntObject::disable()/enable() instead
|
2016-10-16 14:27:35 +02:00
|
|
|
bool visible;
|
2022-11-29 17:07:21 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> getSurface()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
return bg;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
/// wrap existing image
|
|
|
|
CPicture(std::shared_ptr<IImage> image, const Point & position);
|
2023-01-30 13:58:13 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
/// wrap section of an existing Image
|
|
|
|
CPicture(std::shared_ptr<IImage> image, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
/// Loads image from specified file name
|
|
|
|
CPicture(const std::string & bmpname);
|
|
|
|
CPicture(const std::string & bmpname, const Point & position);
|
|
|
|
CPicture(const std::string & bmpname, int x, int y);
|
|
|
|
|
|
|
|
/// set alpha value for whole surface. Note: may be messed up if surface is shared
|
|
|
|
/// 0=transparent, 255=opaque
|
2014-07-15 10:14:49 +03:00
|
|
|
void setAlpha(int value);
|
|
|
|
void scaleTo(Point size);
|
2023-01-30 13:58:13 +02:00
|
|
|
void colorize(PlayerColor player);
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void show(SDL_Surface * to) override;
|
|
|
|
void showAll(SDL_Surface * to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// area filled with specific texture
|
2022-12-17 06:19:16 +02:00
|
|
|
class CFilledTexture : public CIntObject
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-06 17:28:39 +02:00
|
|
|
protected:
|
2023-01-30 18:25:47 +02:00
|
|
|
std::shared_ptr<IImage> texture;
|
2023-04-26 14:44:10 +02:00
|
|
|
Rect imageArea;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
CFilledTexture(std::string imageName, Rect position);
|
2023-04-26 14:44:10 +02:00
|
|
|
CFilledTexture(std::shared_ptr<IImage> image, Rect position, Rect imageArea);
|
2023-01-30 18:25:47 +02:00
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void showAll(SDL_Surface *to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2023-05-06 17:28:39 +02:00
|
|
|
class FilledTexturePlayerColored : public CFilledTexture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FilledTexturePlayerColored(std::string imageName, Rect position);
|
|
|
|
|
|
|
|
void playerColored(PlayerColor player);
|
|
|
|
};
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
/// Class for displaying one image from animation
|
|
|
|
class CAnimImage: public CIntObject
|
|
|
|
{
|
|
|
|
private:
|
2016-10-16 11:58:18 +02:00
|
|
|
std::shared_ptr<CAnimation> anim;
|
2014-07-15 10:14:49 +03:00
|
|
|
//displayed frame/group
|
|
|
|
size_t frame;
|
|
|
|
size_t group;
|
|
|
|
ui8 flags;
|
2020-03-31 19:52:22 +02:00
|
|
|
const Point scaledSize;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// If set, then image is colored using player-specific palette
|
|
|
|
std::optional<PlayerColor> player;
|
|
|
|
|
2020-03-31 19:52:22 +02:00
|
|
|
bool isScaled() const;
|
|
|
|
void setSizeFromImage(const IImage &img);
|
2014-07-15 10:14:49 +03:00
|
|
|
void init();
|
|
|
|
public:
|
2016-10-16 06:51:07 +02:00
|
|
|
bool visible;
|
|
|
|
|
2016-10-16 11:58:18 +02:00
|
|
|
CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
|
|
|
|
CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
|
2020-03-31 19:52:22 +02:00
|
|
|
CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
|
2017-07-17 23:04:00 +02:00
|
|
|
~CAnimImage();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// size of animation
|
2014-07-15 10:14:49 +03:00
|
|
|
size_t size();
|
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// change displayed frame on this one
|
2014-07-15 10:14:49 +03:00
|
|
|
void setFrame(size_t Frame, size_t Group=0);
|
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// makes image player-colored to specific player
|
2014-07-15 10:14:49 +03:00
|
|
|
void playerColored(PlayerColor player);
|
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// returns true if image has player-colored effect applied
|
|
|
|
bool isPlayerColored() const;
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void showAll(SDL_Surface * to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class for displaying animation, used as superclass for different animations
|
|
|
|
class CShowableAnim: public CIntObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum EFlags
|
|
|
|
{
|
|
|
|
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
|
|
|
|
PLAY_ONCE=32 //play animation only once and stop at last frame
|
|
|
|
};
|
|
|
|
protected:
|
2018-07-25 00:36:48 +02:00
|
|
|
std::shared_ptr<CAnimation> anim;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
size_t group, frame;//current frame
|
|
|
|
|
|
|
|
size_t first, last; //animation range
|
|
|
|
|
2023-01-31 15:00:46 +02:00
|
|
|
/// total time on scren for each frame in animation
|
2023-01-27 12:22:48 +02:00
|
|
|
ui32 frameTimeTotal;
|
2023-01-31 15:00:46 +02:00
|
|
|
|
|
|
|
/// how long was current frame visible on screen
|
2023-01-27 12:22:48 +02:00
|
|
|
ui32 frameTimePassed;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
ui8 flags;//Flags from EFlags enum
|
|
|
|
|
|
|
|
//blit image with optional rotation, fitting into rect, etc
|
|
|
|
void blitImage(size_t frame, size_t group, SDL_Surface *to);
|
|
|
|
|
|
|
|
//For clipping in rect, offsets of picture coordinates
|
|
|
|
int xOffset, yOffset;
|
|
|
|
|
|
|
|
ui8 alpha;
|
|
|
|
|
|
|
|
public:
|
|
|
|
//called when next animation sequence is required
|
|
|
|
std::function<void()> callback;
|
|
|
|
|
|
|
|
//Set per-surface alpha, 0 = transparent, 255 = opaque
|
|
|
|
void setAlpha(ui32 alphaValue);
|
|
|
|
|
2023-01-27 12:22:48 +02:00
|
|
|
CShowableAnim(int x, int y, std::string name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
|
2014-07-15 10:14:49 +03: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);
|
|
|
|
|
|
|
|
//set rotation flags
|
|
|
|
void rotate(bool on, bool vertical=false);
|
|
|
|
|
|
|
|
//move displayed part of picture (if picture is clipped to rect)
|
|
|
|
void clipRect(int posX, int posY, int width, int height);
|
|
|
|
|
|
|
|
//set frame to first, call callback
|
|
|
|
virtual void reset();
|
|
|
|
|
2023-03-13 00:18:55 +02:00
|
|
|
//set animation duration
|
|
|
|
void setDuration(int durationMs);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
//show current frame and increase counter
|
2015-10-12 15:47:10 +02:00
|
|
|
void show(SDL_Surface * to) override;
|
|
|
|
void showAll(SDL_Surface * to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Creature-dependend animations like attacking, moving,...
|
|
|
|
class CCreatureAnim: public CShowableAnim
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
//queue of animations waiting to be displayed
|
2022-12-22 01:04:58 +02:00
|
|
|
std::queue<ECreatureAnimType> queue;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//this function is used as callback if preview flag was set during construction
|
|
|
|
void loopPreview(bool warMachine);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//change anim to next if queue is not empty, call callback othervice
|
2015-10-12 15:47:10 +02:00
|
|
|
void reset() override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//add sequence to the end of queue
|
2022-12-22 01:04:58 +02:00
|
|
|
void addLast(ECreatureAnimType newType);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void startPreview(bool warMachine);
|
|
|
|
|
|
|
|
//clear queue and set animation to this sequence
|
2022-12-22 01:04:58 +02:00
|
|
|
void clearAndSet(ECreatureAnimType type);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-12-22 01:04:58 +02:00
|
|
|
CCreatureAnim(int x, int y, std::string name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
};
|