2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* Images.cpp, 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
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Images.h"
|
|
|
|
|
|
|
|
#include "MiscWidgets.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
2024-09-12 18:11:03 +02:00
|
|
|
#include "../render/AssetGenerator.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/IImage.h"
|
2023-09-04 17:01:44 +02:00
|
|
|
#include "../render/IRenderHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/CAnimation.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
2023-05-06 17:28:39 +02:00
|
|
|
#include "../render/ColorFilter.h"
|
2024-11-17 22:40:38 +02:00
|
|
|
#include "../render/Colors.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "../battle/BattleInterface.h"
|
|
|
|
#include "../battle/BattleInterfaceClasses.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../../lib/CRandomGenerator.h"
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
|
|
|
|
: bg(image)
|
2023-01-30 13:58:13 +02:00
|
|
|
, needRefresh(false)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-30 13:58:13 +02:00
|
|
|
pos += position;
|
2023-01-30 17:18:59 +02:00
|
|
|
pos.w = bg->width();
|
|
|
|
pos.h = bg->height();
|
2024-08-15 00:21:02 +02:00
|
|
|
|
|
|
|
addUsedEvents(SHOW_POPUP);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CPicture::CPicture( const ImagePath &bmpname, int x, int y )
|
2023-01-30 13:58:13 +02:00
|
|
|
: CPicture(bmpname, Point(x,y))
|
|
|
|
{}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CPicture::CPicture( const ImagePath & bmpname )
|
2023-01-30 13:58:13 +02:00
|
|
|
: CPicture(bmpname, Point(0,0))
|
|
|
|
{}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CPicture::CPicture( const ImagePath & bmpname, const Point & position )
|
2024-06-08 09:35:13 +02:00
|
|
|
: bg(GH.renderHandler().loadImage(bmpname, EImageBlitMode::COLORKEY))
|
2023-01-30 13:58:13 +02:00
|
|
|
, needRefresh(false)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-30 13:58:13 +02:00
|
|
|
pos.x += position.x;
|
|
|
|
pos.y += position.y;
|
|
|
|
|
|
|
|
assert(bg);
|
2014-07-15 10:14:49 +03:00
|
|
|
if(bg)
|
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
pos.w = bg->width();
|
|
|
|
pos.h = bg->height();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = pos.h = 0;
|
|
|
|
}
|
2024-08-15 00:21:02 +02:00
|
|
|
|
|
|
|
addUsedEvents(SHOW_POPUP);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-06-08 09:35:13 +02:00
|
|
|
CPicture::CPicture(const ImagePath & bmpname, const Rect &SrcRect, int x, int y)
|
|
|
|
: CPicture(bmpname, Point(x,y))
|
|
|
|
{
|
|
|
|
srcRect = SrcRect;
|
|
|
|
pos.w = srcRect->w;
|
|
|
|
pos.h = srcRect->h;
|
2024-08-15 00:21:02 +02:00
|
|
|
|
|
|
|
addUsedEvents(SHOW_POPUP);
|
2024-06-08 09:35:13 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
|
|
|
|
: CPicture(image, Point(x,y))
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-30 13:58:13 +02:00
|
|
|
srcRect = SrcRect;
|
2014-07-15 10:14:49 +03:00
|
|
|
pos.w = srcRect->w;
|
|
|
|
pos.h = srcRect->h;
|
2024-08-15 00:21:02 +02:00
|
|
|
|
|
|
|
addUsedEvents(SHOW_POPUP);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CPicture::show(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-11-23 17:19:09 +02:00
|
|
|
if (needRefresh)
|
2014-07-15 10:14:49 +03:00
|
|
|
showAll(to);
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CPicture::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-11-23 17:19:09 +02:00
|
|
|
if(bg)
|
2023-06-02 15:42:18 +02:00
|
|
|
{
|
|
|
|
if (srcRect.has_value())
|
|
|
|
to.draw(bg, pos.topLeft(), *srcRect);
|
|
|
|
else
|
|
|
|
to.draw(bg, pos.topLeft());
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-28 19:34:18 +02:00
|
|
|
void CPicture::setAlpha(uint8_t value)
|
2016-10-16 06:51:07 +02:00
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
bg->setAlpha(value);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::scaleTo(Point size)
|
|
|
|
{
|
2024-07-25 20:22:27 +02:00
|
|
|
bg->scaleTo(size);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
pos.w = bg->width();
|
|
|
|
pos.h = bg->height();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:50:50 +02:00
|
|
|
void CPicture::setPlayerColor(PlayerColor player)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
bg->playerColored(player);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-08-15 23:09:04 +02:00
|
|
|
void CPicture::addRClickCallback(const std::function<void()> & callback)
|
2024-08-15 00:21:02 +02:00
|
|
|
{
|
|
|
|
rCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::showPopupWindow(const Point & cursorPosition)
|
|
|
|
{
|
|
|
|
if(rCallback)
|
|
|
|
rCallback();
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:43:56 +02:00
|
|
|
CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position)
|
|
|
|
: CIntObject(0, position.topLeft())
|
2024-06-08 09:35:13 +02:00
|
|
|
, texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
|
2023-04-26 14:44:10 +02:00
|
|
|
{
|
|
|
|
pos.w = position.w;
|
|
|
|
pos.h = position.h;
|
|
|
|
imageArea = Rect(Point(), texture->dimensions());
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:43:56 +02:00
|
|
|
CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea)
|
2023-04-26 14:44:10 +02:00
|
|
|
: CIntObject(0, position.topLeft())
|
2024-06-08 09:35:13 +02:00
|
|
|
, texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
|
2023-04-26 14:44:10 +02:00
|
|
|
, imageArea(imageArea)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
pos.w = position.w;
|
|
|
|
pos.h = position.h;
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CFilledTexture::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-06-02 15:42:18 +02:00
|
|
|
CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
|
2023-01-30 18:25:47 +02:00
|
|
|
|
2023-04-26 17:12:18 +02:00
|
|
|
for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
|
2023-01-30 18:25:47 +02:00
|
|
|
{
|
2023-04-26 17:12:18 +02:00
|
|
|
for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
|
2023-06-02 15:42:18 +02:00
|
|
|
to.draw(texture, Point(x,y), imageArea);
|
2023-01-30 18:25:47 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2023-05-06 17:28:39 +02:00
|
|
|
|
2024-06-05 18:30:53 +02:00
|
|
|
void FilledTexturePlayerIndexed::setPlayerColor(PlayerColor player)
|
2023-05-06 17:28:39 +02:00
|
|
|
{
|
2024-06-05 18:30:53 +02:00
|
|
|
texture->playerColored(player);
|
2023-05-06 17:28:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 18:11:03 +02:00
|
|
|
FilledTexturePlayerColored::FilledTexturePlayerColored(Rect position)
|
|
|
|
:CFilledTexture(ImagePath::builtin("DiBoxBck"), position)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:50:50 +02:00
|
|
|
void FilledTexturePlayerColored::setPlayerColor(PlayerColor player)
|
2023-05-06 17:28:39 +02:00
|
|
|
{
|
2024-09-12 18:11:03 +02:00
|
|
|
AssetGenerator::createPlayerColoredBackground(player);
|
|
|
|
|
|
|
|
ImagePath imagePath = ImagePath::builtin("DialogBoxBackground_" + player.toString() + ".bmp");
|
2023-05-06 17:28:39 +02:00
|
|
|
|
2024-09-12 18:11:03 +02:00
|
|
|
texture = GH.renderHandler().loadImage(imagePath, EImageBlitMode::COLORKEY);
|
2023-05-06 17:28:39 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
|
2014-07-15 10:14:49 +03:00
|
|
|
frame(Frame),
|
|
|
|
group(Group),
|
|
|
|
flags(Flags)
|
|
|
|
{
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
2024-11-17 19:54:55 +02:00
|
|
|
anim = GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY);
|
2014-07-15 10:14:49 +03:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:43:56 +02:00
|
|
|
CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
|
2024-11-17 19:54:55 +02:00
|
|
|
anim(GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY)),
|
2020-03-31 19:52:22 +02:00
|
|
|
frame(Frame),
|
|
|
|
group(Group),
|
|
|
|
flags(Flags),
|
|
|
|
scaledSize(targetPos.w, targetPos.h)
|
|
|
|
{
|
|
|
|
pos.x += targetPos.x;
|
|
|
|
pos.y += targetPos.y;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
size_t CAnimImage::size()
|
|
|
|
{
|
|
|
|
return anim->size(group);
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:52:22 +02:00
|
|
|
bool CAnimImage::isScaled() const
|
|
|
|
{
|
|
|
|
return (scaledSize.x != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimImage::setSizeFromImage(const IImage &img)
|
|
|
|
{
|
|
|
|
if (isScaled())
|
|
|
|
{
|
|
|
|
// At the time of writing this, IImage had no method to scale to different aspect ratio
|
|
|
|
// Therefore, have to ignore the target height and preserve original aspect ratio
|
|
|
|
pos.w = scaledSize.x;
|
|
|
|
pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = img.width();
|
|
|
|
pos.h = img.height();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void CAnimImage::init()
|
|
|
|
{
|
2016-10-16 06:51:07 +02:00
|
|
|
visible = true;
|
2018-03-30 13:02:04 +02:00
|
|
|
auto img = anim->getImage(frame, group);
|
2014-07-15 10:14:49 +03:00
|
|
|
if (img)
|
2020-03-31 19:52:22 +02:00
|
|
|
setSizeFromImage(*img);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CAnimImage::~CAnimImage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CAnimImage::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2016-10-16 06:51:07 +02:00
|
|
|
if(!visible)
|
|
|
|
return;
|
|
|
|
|
2021-02-11 21:06:47 +02:00
|
|
|
std::vector<size_t> frames = {frame};
|
|
|
|
|
|
|
|
if((flags & CShowableAnim::BASE) && frame != 0)
|
|
|
|
{
|
|
|
|
frames.insert(frames.begin(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto targetFrame : frames)
|
|
|
|
{
|
|
|
|
if(auto img = anim->getImage(targetFrame, group))
|
2020-03-31 19:52:22 +02:00
|
|
|
{
|
2021-02-11 21:06:47 +02:00
|
|
|
if(isScaled())
|
2024-07-25 20:22:27 +02:00
|
|
|
img->scaleTo(scaledSize);
|
2024-06-04 13:46:45 +02:00
|
|
|
|
|
|
|
to.draw(img, pos.topLeft());
|
2020-03-31 19:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:22:26 +02:00
|
|
|
void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
|
|
|
|
{
|
|
|
|
this->frame = frame;
|
2024-06-08 09:35:13 +02:00
|
|
|
anim = GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
|
2023-10-19 20:22:26 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimImage::setScale(Point scale)
|
|
|
|
{
|
|
|
|
scaledSize = scale;
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void CAnimImage::setFrame(size_t Frame, size_t Group)
|
|
|
|
{
|
|
|
|
if (frame == Frame && group==Group)
|
|
|
|
return;
|
|
|
|
if (anim->size(Group) > Frame)
|
|
|
|
{
|
|
|
|
frame = Frame;
|
|
|
|
group = Group;
|
2018-03-30 13:29:55 +02:00
|
|
|
if(auto img = anim->getImage(frame, group))
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-03 18:05:07 +02:00
|
|
|
if (player.has_value())
|
|
|
|
img->playerColored(*player);
|
2020-03-31 19:52:22 +02:00
|
|
|
setSizeFromImage(*img);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-08-11 13:38:10 +02:00
|
|
|
logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:50:50 +02:00
|
|
|
void CAnimImage::setPlayerColor(PlayerColor currPlayer)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
player = currPlayer;
|
2023-05-03 18:05:07 +02:00
|
|
|
anim->getImage(frame, group)->playerColored(*player);
|
2014-07-15 10:14:49 +03:00
|
|
|
if (flags & CShowableAnim::BASE)
|
2023-05-03 18:05:07 +02:00
|
|
|
anim->getImage(0, group)->playerColored(*player);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAnimImage::isPlayerColored() const
|
|
|
|
{
|
|
|
|
return player.has_value();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
|
2024-11-17 19:54:55 +02:00
|
|
|
anim(GH.renderHandler().loadAnimation(name, (Flags & CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY)),
|
2014-07-15 10:14:49 +03:00
|
|
|
group(Group),
|
|
|
|
frame(0),
|
|
|
|
first(0),
|
2023-01-27 12:22:48 +02:00
|
|
|
frameTimeTotal(frameTime),
|
|
|
|
frameTimePassed(0),
|
2014-07-15 10:14:49 +03:00
|
|
|
flags(Flags),
|
|
|
|
xOffset(0),
|
|
|
|
yOffset(0),
|
2023-01-08 23:44:47 +02:00
|
|
|
alpha(alpha)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
last = anim->size(group);
|
|
|
|
|
2024-05-14 11:44:40 +02:00
|
|
|
auto image = anim->getImage(0, group);
|
|
|
|
if (!image)
|
|
|
|
throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());
|
|
|
|
|
|
|
|
pos.w = image->width();
|
|
|
|
pos.h = image->height();
|
2014-07-15 10:14:49 +03:00
|
|
|
pos.x+= x;
|
|
|
|
pos.y+= y;
|
2023-05-13 16:24:18 +02:00
|
|
|
|
|
|
|
addUsedEvents(TIME);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::setAlpha(ui32 alphaValue)
|
|
|
|
{
|
|
|
|
alpha = std::min<ui32>(alphaValue, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CShowableAnim::set(size_t Group, size_t from, size_t to)
|
|
|
|
{
|
|
|
|
size_t max = anim->size(Group);
|
|
|
|
|
|
|
|
if (to < max)
|
|
|
|
max = to;
|
|
|
|
|
|
|
|
if (max < from || max == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
group = Group;
|
|
|
|
frame = first = from;
|
|
|
|
last = max;
|
2023-01-27 12:22:48 +02:00
|
|
|
frameTimePassed = 0;
|
2014-07-15 10:14:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CShowableAnim::set(size_t Group)
|
|
|
|
{
|
|
|
|
if (anim->size(Group)== 0)
|
|
|
|
return false;
|
|
|
|
if (group != Group)
|
|
|
|
{
|
|
|
|
first = 0;
|
|
|
|
group = Group;
|
|
|
|
last = anim->size(Group);
|
|
|
|
}
|
2023-01-27 12:22:48 +02:00
|
|
|
frame = 0;
|
|
|
|
frameTimePassed = 0;
|
2014-07-15 10:14:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::reset()
|
|
|
|
{
|
|
|
|
frame = first;
|
|
|
|
|
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::clipRect(int posX, int posY, int width, int height)
|
|
|
|
{
|
|
|
|
xOffset = posX;
|
|
|
|
yOffset = posY;
|
|
|
|
pos.w = width;
|
|
|
|
pos.h = height;
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CShowableAnim::show(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
2023-05-13 16:24:18 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
void CShowableAnim::tick(uint32_t msPassed)
|
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
if ((flags & PLAY_ONCE) && frame + 1 == last)
|
|
|
|
return;
|
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
frameTimePassed += msPassed;
|
2023-01-27 12:22:48 +02:00
|
|
|
|
|
|
|
if(frameTimePassed >= frameTimeTotal)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-01-27 12:22:48 +02:00
|
|
|
frameTimePassed -= frameTimeTotal;
|
2014-07-15 10:14:49 +03:00
|
|
|
if ( ++frame >= last)
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CShowableAnim::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if ( flags & BASE )// && frame != first)
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
Rect src( xOffset, yOffset, pos.w, pos.h);
|
2018-03-30 13:02:04 +02:00
|
|
|
auto img = anim->getImage(frame, group);
|
2018-07-25 00:36:48 +02:00
|
|
|
if(img)
|
2023-01-11 20:57:42 +02:00
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
img->setAlpha(alpha);
|
2024-11-17 22:40:38 +02:00
|
|
|
img->setOverlayColor(Colors::TRANSPARENCY);
|
2023-06-02 15:42:18 +02:00
|
|
|
to.draw(img, pos.topLeft(), src);
|
2023-01-11 20:57:42 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::rotate(bool on, bool vertical)
|
|
|
|
{
|
|
|
|
ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
|
|
|
|
if (on)
|
|
|
|
flags |= flag;
|
|
|
|
else
|
|
|
|
flags &= ~flag;
|
|
|
|
}
|
|
|
|
|
2023-03-13 00:18:55 +02:00
|
|
|
void CShowableAnim::setDuration(int durationMs)
|
|
|
|
{
|
|
|
|
frameTimeTotal = durationMs/(last - first);
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
|
2024-07-25 20:22:27 +02:00
|
|
|
CShowableAnim(x, y, name, flags | CREATURE_MODE, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
xOffset = 0;
|
|
|
|
yOffset = 0;
|
2017-07-12 21:01:10 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void CCreatureAnim::loopPreview(bool warMachine)
|
|
|
|
{
|
2022-12-22 01:04:58 +02:00
|
|
|
std::vector<ECreatureAnimType> available;
|
2022-12-08 19:41:02 +02:00
|
|
|
|
2022-12-22 01:04:58 +02:00
|
|
|
static const ECreatureAnimType creaPreviewList[] = {
|
2022-12-08 19:41:02 +02:00
|
|
|
ECreatureAnimType::HOLDING,
|
|
|
|
ECreatureAnimType::HITTED,
|
|
|
|
ECreatureAnimType::DEFENCE,
|
|
|
|
ECreatureAnimType::ATTACK_FRONT,
|
2022-12-14 14:21:58 +02:00
|
|
|
ECreatureAnimType::SPECIAL_FRONT
|
2022-12-08 19:41:02 +02:00
|
|
|
};
|
2022-12-22 01:04:58 +02:00
|
|
|
static const ECreatureAnimType machPreviewList[] = {
|
2022-12-08 19:41:02 +02:00
|
|
|
ECreatureAnimType::HOLDING,
|
|
|
|
ECreatureAnimType::MOVING,
|
|
|
|
ECreatureAnimType::SHOOT_UP,
|
|
|
|
ECreatureAnimType::SHOOT_FRONT,
|
|
|
|
ECreatureAnimType::SHOOT_DOWN
|
|
|
|
};
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
auto & previewList = warMachine ? machPreviewList : creaPreviewList;
|
|
|
|
|
|
|
|
for (auto & elem : previewList)
|
2022-12-22 01:04:58 +02:00
|
|
|
if (anim->size(size_t(elem)))
|
2014-07-15 10:14:49 +03:00
|
|
|
available.push_back(elem);
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
if (rnd >= available.size())
|
|
|
|
{
|
2022-12-22 01:04:58 +02:00
|
|
|
ECreatureAnimType type;
|
|
|
|
if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
|
2022-12-08 19:41:02 +02:00
|
|
|
type = ECreatureAnimType::HOLDING;
|
2014-07-15 10:14:49 +03:00
|
|
|
else
|
2022-12-08 19:41:02 +02:00
|
|
|
type = ECreatureAnimType::MOVING;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//display this anim for ~1 second (time is random, but it looks good)
|
2022-12-22 01:04:58 +02:00
|
|
|
for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
|
2014-07-15 10:14:49 +03:00
|
|
|
addLast(type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
addLast(available[rnd]);
|
|
|
|
}
|
|
|
|
|
2022-12-22 01:04:58 +02:00
|
|
|
void CCreatureAnim::addLast(ECreatureAnimType newType)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-22 01:04:58 +02:00
|
|
|
auto currType = ECreatureAnimType(group);
|
|
|
|
|
|
|
|
if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-08 19:41:02 +02:00
|
|
|
queue.push( ECreatureAnimType::MOVE_START );
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2022-12-22 01:04:58 +02:00
|
|
|
else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-08 19:41:02 +02:00
|
|
|
queue.push( ECreatureAnimType::MOVE_END );
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2022-12-08 19:41:02 +02:00
|
|
|
if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
|
2014-07-15 10:14:49 +03:00
|
|
|
queue.push(newType);
|
|
|
|
|
|
|
|
queue.push(newType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnim::reset()
|
|
|
|
{
|
|
|
|
//if we are in the middle of rotation - set flag
|
2022-12-22 01:04:58 +02:00
|
|
|
if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
|
2014-07-15 10:14:49 +03:00
|
|
|
rotate(true);
|
2022-12-22 01:04:58 +02:00
|
|
|
if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
|
2014-07-15 10:14:49 +03:00
|
|
|
rotate(false);
|
|
|
|
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
2022-12-22 01:04:58 +02:00
|
|
|
ECreatureAnimType at = queue.front();
|
2014-07-15 10:14:49 +03:00
|
|
|
queue.pop();
|
2022-12-22 01:04:58 +02:00
|
|
|
if (set(size_t(at)))
|
2014-07-15 10:14:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
2022-12-22 01:04:58 +02:00
|
|
|
ECreatureAnimType at = queue.front();
|
2014-07-15 10:14:49 +03:00
|
|
|
queue.pop();
|
2022-12-22 01:04:58 +02:00
|
|
|
if (set(size_t(at)))
|
2014-07-15 10:14:49 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-12-22 01:04:58 +02:00
|
|
|
set(size_t(ECreatureAnimType::HOLDING));
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnim::startPreview(bool warMachine)
|
|
|
|
{
|
2014-08-09 15:14:31 +03:00
|
|
|
callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-12-22 01:04:58 +02:00
|
|
|
void CCreatureAnim::clearAndSet(ECreatureAnimType type)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
while (!queue.empty())
|
|
|
|
queue.pop();
|
2022-12-22 01:04:58 +02:00
|
|
|
set(size_t(type));
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|