2017-07-13 11:26:03 +03:00
/*
* 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
*
*/
2011-12-13 21:23:17 +00:00
# include "StdInc.h"
2014-07-13 20:53:37 +03:00
# include "CAnimation.h"
2022-09-18 15:47:49 +03:00
# include "SDL_Extensions.h"
2022-12-15 23:24:03 +02:00
# include "ColorFilter.h"
2022-09-18 15:47:49 +03:00
2014-07-13 20:53:37 +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"
2011-10-08 01:23:46 +00:00
2023-01-30 19:55:32 +02:00
# include <SDL_surface.h>
2015-01-30 23:37:28 +01:00
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
2015-01-30 23:37:28 +01:00
if ( fadingMode = = EMode : : OUT )
fadingCounter - = delta ;
else
fadingCounter + = delta ;
2016-01-29 15:16:14 +01:00
2015-01-30 23:37:28 +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 ) ,
2015-01-30 23:37:28 +01:00
fadingMode ( EMode : : NONE )
{
}
CFadeAnimation : : ~ CFadeAnimation ( )
{
if ( fadingSurface & & shouldFreeSurface )
2016-01-29 15:16:14 +01:00
SDL_FreeSurface ( fadingSurface ) ;
2015-01-30 23:37:28 +01:00
}
2017-07-15 14:08:20 +03:00
void CFadeAnimation : : init ( EMode mode , SDL_Surface * sourceSurface , bool freeSurfaceAtEnd , float animDelta )
2015-01-30 23:37:28 +01:00
{
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 ) ;
}
2015-01-30 23:37:28 +01:00
if ( animDelta < = 0.0f )
{
2017-08-11 14:38:10 +03:00
logGlobal - > warn ( " Fade anim: delta should be positive; %f given. " , animDelta ) ;
2015-01-30 23:37:28 +01:00
animDelta = DEFAULT_DELTA ;
}
2016-01-29 15:16:14 +01:00
2015-01-30 23:37:28 +01:00
if ( sourceSurface )
fadingSurface = sourceSurface ;
2016-01-29 15:16:14 +01:00
2015-01-30 23:37:28 +01:00
delta = animDelta ;
fadingMode = mode ;
fadingCounter = initialCounter ( ) ;
fading = true ;
shouldFreeSurface = freeSurfaceAtEnd ;
}
2023-01-17 22:01:35 +02:00
void CFadeAnimation : : draw ( SDL_Surface * targetSurface , const Point & targetPoint )
2016-01-29 15:16:14 +01:00
{
2015-01-30 23:37:28 +01:00
if ( ! fading | | ! fadingSurface | | fadingMode = = EMode : : NONE )
{
fading = false ;
return ;
}
2016-01-29 15:16:14 +01:00
2020-10-01 01:38:06 -07:00
CSDL_Ext : : setAlpha ( fadingSurface , ( int ) ( fadingCounter * 255 ) ) ;
2023-01-17 22:01:35 +02:00
CSDL_Ext : : blitSurface ( fadingSurface , targetSurface , targetPoint ) ; //FIXME
2015-02-18 15:31:55 +01:00
CSDL_Ext : : setAlpha ( fadingSurface , 255 ) ;
2015-01-30 23:37:28 +01:00
}