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/CAnimation.h"
|
|
|
|
#include "../gui/SDL_Pixels.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/CCursorHandler.h"
|
|
|
|
|
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 "../CBitmapHandler.h"
|
|
|
|
#include "../Graphics.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CMessage.h"
|
|
|
|
#include "../CMusicHandler.h"
|
|
|
|
#include "../windows/CAdvmapInterface.h"
|
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
|
|
|
|
#include "../../lib/CRandomGenerator.h"
|
|
|
|
|
|
|
|
CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
bg = BG;
|
|
|
|
freeSurf = Free;
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
pos.w = BG->w;
|
|
|
|
pos.h = BG->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPicture::CPicture( const std::string &bmpname, int x, int y )
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
bg = BitmapHandler::loadBitmap(bmpname);
|
2017-07-12 21:01:10 +02:00
|
|
|
freeSurf = true;
|
2014-07-15 10:14:49 +03:00
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
if(bg)
|
|
|
|
{
|
|
|
|
pos.w = bg->w;
|
|
|
|
pos.h = bg->h;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = pos.h = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
createSimpleRect(r, screenFormat, color);
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CPicture::CPicture(SDL_Surface * BG, const Rect &SrcRect, int x, int y, bool free)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2016-10-16 14:27:35 +02:00
|
|
|
visible = true;
|
2014-07-15 10:14:49 +03:00
|
|
|
needRefresh = false;
|
|
|
|
srcRect = new Rect(SrcRect);
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
pos.w = srcRect->w;
|
|
|
|
pos.h = srcRect->h;
|
|
|
|
bg = BG;
|
|
|
|
freeSurf = free;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::setSurface(SDL_Surface *to)
|
|
|
|
{
|
|
|
|
bg = to;
|
|
|
|
if (srcRect)
|
|
|
|
{
|
|
|
|
pos.w = srcRect->w;
|
|
|
|
pos.h = srcRect->h;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = bg->w;
|
|
|
|
pos.h = bg->h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CPicture::~CPicture()
|
|
|
|
{
|
|
|
|
if(freeSurf)
|
|
|
|
SDL_FreeSurface(bg);
|
|
|
|
delete srcRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::init()
|
|
|
|
{
|
2016-10-16 14:27:35 +02:00
|
|
|
visible = true;
|
2014-07-15 10:14:49 +03:00
|
|
|
needRefresh = false;
|
|
|
|
srcRect = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::show(SDL_Surface * to)
|
|
|
|
{
|
2016-10-16 14:27:35 +02:00
|
|
|
if (visible && needRefresh)
|
2014-07-15 10:14:49 +03:00
|
|
|
showAll(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::showAll(SDL_Surface * to)
|
|
|
|
{
|
2016-10-16 14:27:35 +02:00
|
|
|
if(bg && visible)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(srcRect)
|
|
|
|
{
|
|
|
|
SDL_Rect srcRectCpy = *srcRect;
|
|
|
|
SDL_Rect dstRect = srcRectCpy;
|
|
|
|
dstRect.x = pos.x;
|
|
|
|
dstRect.y = pos.y;
|
|
|
|
|
|
|
|
CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
blitAt(bg, pos, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::convertToScreenBPP()
|
|
|
|
{
|
|
|
|
SDL_Surface *hlp = bg;
|
|
|
|
bg = SDL_ConvertSurface(hlp,screen->format,0);
|
2016-10-16 06:51:07 +02:00
|
|
|
CSDL_Ext::setDefaultColorKey(bg);
|
2014-07-15 10:14:49 +03:00
|
|
|
SDL_FreeSurface(hlp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::setAlpha(int value)
|
2016-10-16 06:51:07 +02:00
|
|
|
{
|
2015-02-18 16:31:55 +02:00
|
|
|
CSDL_Ext::setAlpha (bg, value);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::scaleTo(Point size)
|
|
|
|
{
|
|
|
|
SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
|
|
|
|
|
|
|
|
if(freeSurf)
|
|
|
|
SDL_FreeSurface(bg);
|
|
|
|
|
|
|
|
setSurface(scaled);
|
|
|
|
freeSurf = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
|
|
|
|
{
|
|
|
|
pos += r;
|
|
|
|
pos.w = r.w;
|
|
|
|
pos.h = r.h;
|
|
|
|
if(screenFormat)
|
|
|
|
bg = CSDL_Ext::newSurface(r.w, r.h);
|
|
|
|
else
|
2018-07-25 00:36:48 +02:00
|
|
|
bg = SDL_CreateRGBSurface(0, r.w, r.h, 8, 0, 0, 0, 0);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
SDL_FillRect(bg, nullptr, color);
|
|
|
|
freeSurf = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPicture::colorize(PlayerColor player)
|
|
|
|
{
|
|
|
|
assert(bg);
|
|
|
|
graphics->blueToPlayersAdv(bg, player);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFilledTexture::CFilledTexture(std::string imageName, Rect position):
|
|
|
|
CIntObject(0, position.topLeft()),
|
|
|
|
texture(BitmapHandler::loadBitmap(imageName))
|
|
|
|
{
|
|
|
|
pos.w = position.w;
|
|
|
|
pos.h = position.h;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFilledTexture::~CFilledTexture()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFilledTexture::showAll(SDL_Surface *to)
|
|
|
|
{
|
|
|
|
CSDL_Ext::CClipRectGuard guard(to, pos);
|
|
|
|
CSDL_Ext::fillTexture(to, texture);
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:58:18 +02:00
|
|
|
CAnimImage::CAnimImage(const std::string & 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),
|
|
|
|
player(-1),
|
|
|
|
flags(Flags)
|
|
|
|
{
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
2016-10-16 11:58:18 +02:00
|
|
|
anim = std::make_shared<CAnimation>(name);
|
2014-07-15 10:14:49 +03:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:58:18 +02:00
|
|
|
CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
|
2014-07-15 10:14:49 +03:00
|
|
|
anim(Anim),
|
|
|
|
frame(Frame),
|
|
|
|
group(Group),
|
|
|
|
player(-1),
|
|
|
|
flags(Flags)
|
|
|
|
{
|
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:52:22 +02:00
|
|
|
CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
|
|
|
|
anim(Anim),
|
|
|
|
frame(Frame),
|
|
|
|
group(Group),
|
|
|
|
player(-1),
|
|
|
|
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;
|
2014-07-15 10:14:49 +03:00
|
|
|
anim->load(frame, group);
|
|
|
|
if (flags & CShowableAnim::BASE)
|
|
|
|
anim->load(0,group);
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimImage::showAll(SDL_Surface * to)
|
|
|
|
{
|
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())
|
2020-03-31 19:52:22 +02:00
|
|
|
{
|
|
|
|
auto scaled = img->scaleFast(float(scaledSize.x) / img->width());
|
|
|
|
scaled->draw(to, pos.x, pos.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
img->draw(to, pos.x, pos.y);
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2018-03-30 12:40:40 +02:00
|
|
|
anim->load(Frame, Group);
|
2014-07-15 10:14:49 +03:00
|
|
|
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
|
|
|
{
|
|
|
|
if (flags & CShowableAnim::PLAYER_COLORED)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimImage::playerColored(PlayerColor currPlayer)
|
|
|
|
{
|
|
|
|
player = currPlayer;
|
|
|
|
flags |= CShowableAnim::PLAYER_COLORED;
|
|
|
|
anim->getImage(frame, group)->playerColored(player);
|
|
|
|
if (flags & CShowableAnim::BASE)
|
|
|
|
anim->getImage(0, group)->playerColored(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
|
2018-07-25 00:36:48 +02:00
|
|
|
anim(std::make_shared<CAnimation>(name)),
|
2014-07-15 10:14:49 +03:00
|
|
|
group(Group),
|
|
|
|
frame(0),
|
|
|
|
first(0),
|
|
|
|
frameDelay(Delay),
|
|
|
|
value(0),
|
|
|
|
flags(Flags),
|
|
|
|
xOffset(0),
|
|
|
|
yOffset(0),
|
|
|
|
alpha(255)
|
|
|
|
{
|
|
|
|
anim->loadGroup(group);
|
|
|
|
last = anim->size(group);
|
|
|
|
|
|
|
|
pos.w = anim->getImage(0, group)->width();
|
|
|
|
pos.h = anim->getImage(0, group)->height();
|
|
|
|
pos.x+= x;
|
|
|
|
pos.y+= y;
|
|
|
|
}
|
|
|
|
|
|
|
|
CShowableAnim::~CShowableAnim()
|
|
|
|
{
|
|
|
|
anim->unloadGroup(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
anim->unloadGroup(group);
|
|
|
|
anim->loadGroup(Group);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
group = Group;
|
|
|
|
frame = first = from;
|
|
|
|
last = max;
|
|
|
|
value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CShowableAnim::set(size_t Group)
|
|
|
|
{
|
|
|
|
if (anim->size(Group)== 0)
|
|
|
|
return false;
|
|
|
|
if (group != Group)
|
|
|
|
{
|
|
|
|
anim->unloadGroup(group);
|
2018-04-07 13:34:11 +02:00
|
|
|
anim->loadGroup(Group);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
first = 0;
|
|
|
|
group = Group;
|
|
|
|
last = anim->size(Group);
|
|
|
|
}
|
|
|
|
frame = value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::reset()
|
|
|
|
{
|
|
|
|
value = 0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::show(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
|
|
|
|
|
|
|
if ((flags & PLAY_ONCE) && frame + 1 == last)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( ++value == frameDelay )
|
|
|
|
{
|
|
|
|
value = 0;
|
|
|
|
if ( ++frame >= last)
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::showAll(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
if ( flags & BASE )// && frame != first)
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
|
|
|
|
{
|
|
|
|
assert(to);
|
|
|
|
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)
|
|
|
|
img->draw(to, pos.x, pos.y, &src, alpha);
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:41:02 +02:00
|
|
|
CCreatureAnim::CCreatureAnim(int x, int y, std::string name, ui8 flags, ECreatureAnimType::Type type):
|
2014-07-15 10:14:49 +03:00
|
|
|
CShowableAnim(x,y,name,flags,4,type)
|
|
|
|
{
|
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-08 19:41:02 +02:00
|
|
|
std::vector<ECreatureAnimType::Type> available;
|
|
|
|
|
|
|
|
static const ECreatureAnimType::Type creaPreviewList[] = {
|
|
|
|
ECreatureAnimType::HOLDING,
|
|
|
|
ECreatureAnimType::HITTED,
|
|
|
|
ECreatureAnimType::DEFENCE,
|
|
|
|
ECreatureAnimType::ATTACK_FRONT,
|
|
|
|
ECreatureAnimType::CAST_FRONT
|
|
|
|
};
|
|
|
|
static const ECreatureAnimType::Type machPreviewList[] = {
|
|
|
|
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)
|
|
|
|
if (anim->size(elem))
|
|
|
|
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-08 19:41:02 +02:00
|
|
|
ECreatureAnimType::Type type;
|
|
|
|
if ( anim->size(ECreatureAnimType::MOVING) == 0 )//no moving animation present
|
|
|
|
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)
|
|
|
|
for (size_t i=0; i< 12/anim->size(type) + 1; i++)
|
|
|
|
addLast(type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
addLast(available[rnd]);
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:41:02 +02:00
|
|
|
void CCreatureAnim::addLast(ECreatureAnimType::Type newType)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2022-12-08 19:41:02 +02:00
|
|
|
if (type != 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-08 19:41:02 +02:00
|
|
|
else if (type == 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-08 19:41:02 +02:00
|
|
|
if (type == ECreatureAnimType::TURN_L && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
|
2014-07-15 10:14:49 +03:00
|
|
|
rotate(true);
|
2022-12-08 19:41:02 +02:00
|
|
|
if (type == 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-08 19:41:02 +02:00
|
|
|
ECreatureAnimType::Type at = queue.front();
|
2014-07-15 10:14:49 +03:00
|
|
|
queue.pop();
|
|
|
|
if (set(at))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
2022-12-08 19:41:02 +02:00
|
|
|
ECreatureAnimType::Type at = queue.front();
|
2014-07-15 10:14:49 +03:00
|
|
|
queue.pop();
|
|
|
|
if (set(at))
|
|
|
|
return;
|
|
|
|
}
|
2022-12-08 19:41:02 +02:00
|
|
|
set(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-08 19:41:02 +02:00
|
|
|
void CCreatureAnim::clearAndSet(ECreatureAnimType::Type type)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
while (!queue.empty())
|
|
|
|
queue.pop();
|
|
|
|
set(type);
|
|
|
|
}
|