1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-21 12:06:49 +02:00
vcmi/client/render/CFadeAnimation.cpp

112 lines
2.7 KiB
C++
Raw Normal View History

/*
* CAnimation.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
*
*/
#include "StdInc.h"
#include "CAnimation.h"
2022-09-18 15:47:49 +03:00
#include "SDL_Extensions.h"
#include "ColorFilter.h"
2022-09-18 15:47:49 +03:00
#include "../CBitmapHandler.h"
#include "../Graphics.h"
2023-01-30 00:12:43 +02:00
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/filesystem/ISimpleResourceLoader.h"
#include "../../lib/JsonNode.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/vcmi_endian.h"
#include <SDL_surface.h>
float CFadeAnimation::initialCounter() const
{
if (fadingMode == EMode::OUT)
return 1.0f;
return 0.0f;
}
void CFadeAnimation::update()
{
if (!fading)
return;
2016-01-29 15:16:14 +01:00
if (fadingMode == EMode::OUT)
fadingCounter -= delta;
else
fadingCounter += delta;
2016-01-29 15:16:14 +01:00
if (isFinished())
{
fading = false;
if (shouldFreeSurface)
{
SDL_FreeSurface(fadingSurface);
fadingSurface = nullptr;
}
}
}
bool CFadeAnimation::isFinished() const
{
if (fadingMode == EMode::OUT)
return fadingCounter <= 0.0f;
return fadingCounter >= 1.0f;
}
CFadeAnimation::CFadeAnimation()
2016-11-27 17:48:18 +03:00
: delta(0), fadingSurface(nullptr), fading(false), fadingCounter(0), shouldFreeSurface(false),
fadingMode(EMode::NONE)
{
}
CFadeAnimation::~CFadeAnimation()
{
if (fadingSurface && shouldFreeSurface)
2016-01-29 15:16:14 +01:00
SDL_FreeSurface(fadingSurface);
}
void CFadeAnimation::init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd, float animDelta)
{
if (fading)
{
2015-02-02 17:42:42 +01:00
// in that case, immediately finish the previous fade
// (alternatively, we could just return here to ignore the new fade request until this one finished (but we'd need to free the passed bitmap to avoid leaks))
2017-08-10 19:39:27 +03:00
logGlobal->warn("Tried to init fading animation that is already running.");
2015-02-02 17:42:42 +01:00
if (fadingSurface && shouldFreeSurface)
2016-01-29 15:16:14 +01:00
SDL_FreeSurface(fadingSurface);
}
if (animDelta <= 0.0f)
{
2017-08-11 14:38:10 +03:00
logGlobal->warn("Fade anim: delta should be positive; %f given.", animDelta);
animDelta = DEFAULT_DELTA;
}
2016-01-29 15:16:14 +01:00
if (sourceSurface)
fadingSurface = sourceSurface;
2016-01-29 15:16:14 +01:00
delta = animDelta;
fadingMode = mode;
fadingCounter = initialCounter();
fading = true;
shouldFreeSurface = freeSurfaceAtEnd;
}
void CFadeAnimation::draw(SDL_Surface * targetSurface, const Point &targetPoint)
2016-01-29 15:16:14 +01:00
{
if (!fading || !fadingSurface || fadingMode == EMode::NONE)
{
fading = false;
return;
}
2016-01-29 15:16:14 +01:00
CSDL_Ext::setAlpha(fadingSurface, (int)(fadingCounter * 255));
CSDL_Ext::blitSurface(fadingSurface, targetSurface, targetPoint); //FIXME
CSDL_Ext::setAlpha(fadingSurface, 255);
}