trabajando en el movimiento easing del marcador

This commit is contained in:
2022-09-23 14:09:36 +02:00
parent 52d0c182f3
commit afe5b52b6b
4 changed files with 336 additions and 8 deletions

214
source/easing.cpp Normal file
View File

@@ -0,0 +1,214 @@
#include <cmath>
#include <map>
#include "easing.h"
#ifndef PI
#define PI 3.1415926545
#endif
double easeInSine( double t ) {
return sin( 1.5707963 * t );
}
double easeOutSine( double t ) {
return 1 + sin( 1.5707963 * (--t) );
}
double easeInOutSine( double t ) {
return 0.5 * (1 + sin( 3.1415926 * (t - 0.5) ) );
}
double easeInQuad( double t ) {
return t * t;
}
double easeOutQuad( double t ) {
return t * (2 - t);
}
double easeInOutQuad( double t ) {
return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1;
}
double easeInCubic( double t ) {
return t * t * t;
}
double easeOutCubic( double t ) {
return 1 + (--t) * t * t;
}
double easeInOutCubic( double t ) {
return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
}
double easeInQuart( double t ) {
t *= t;
return t * t;
}
double easeOutQuart( double t ) {
t = (--t) * t;
return 1 - t * t;
}
double easeInOutQuart( double t ) {
if( t < 0.5 ) {
t *= t;
return 8 * t * t;
} else {
t = (--t) * t;
return 1 - 8 * t * t;
}
}
double easeInQuint( double t ) {
double t2 = t * t;
return t * t2 * t2;
}
double easeOutQuint( double t ) {
double t2 = (--t) * t;
return 1 + t * t2 * t2;
}
double easeInOutQuint( double t ) {
double t2;
if( t < 0.5 ) {
t2 = t * t;
return 16 * t * t2 * t2;
} else {
t2 = (--t) * t;
return 1 + 16 * t * t2 * t2;
}
}
double easeInExpo( double t ) {
return (pow( 2, 8 * t ) - 1) / 255;
}
double easeOutExpo( double t ) {
return 1 - pow( 2, -8 * t );
}
double easeInOutExpo( double t ) {
if( t < 0.5 ) {
return (pow( 2, 16 * t ) - 1) / 510;
} else {
return 1 - 0.5 * pow( 2, -16 * (t - 0.5) );
}
}
double easeInCirc( double t ) {
return 1 - sqrt( 1 - t );
}
double easeOutCirc( double t ) {
return sqrt( t );
}
double easeInOutCirc( double t ) {
if( t < 0.5 ) {
return (1 - sqrt( 1 - 2 * t )) * 0.5;
} else {
return (1 + sqrt( 2 * t - 1 )) * 0.5;
}
}
double easeInBack( double t ) {
return t * t * (2.70158 * t - 1.70158);
}
double easeOutBack( double t ) {
return 1 + (--t) * t * (2.70158 * t + 1.70158);
}
double easeInOutBack( double t ) {
if( t < 0.5 ) {
return t * t * (7 * t - 2.5) * 2;
} else {
return 1 + (--t) * t * 2 * (7 * t + 2.5);
}
}
double easeInElastic( double t ) {
double t2 = t * t;
return t2 * t2 * sin( t * PI * 4.5 );
}
double easeOutElastic( double t ) {
double t2 = (t - 1) * (t - 1);
return 1 - t2 * t2 * cos( t * PI * 4.5 );
}
double easeInOutElastic( double t ) {
double t2;
if( t < 0.45 ) {
t2 = t * t;
return 8 * t2 * t2 * sin( t * PI * 9 );
} else if( t < 0.55 ) {
return 0.5 + 0.75 * sin( t * PI * 4 );
} else {
t2 = (t - 1) * (t - 1);
return 1 - 8 * t2 * t2 * sin( t * PI * 9 );
}
}
double easeInBounce( double t ) {
return pow( 2, 6 * (t - 1) ) * abs( sin( t * PI * 3.5 ) );
}
double easeOutBounce( double t ) {
return 1 - pow( 2, -6 * t ) * abs( cos( t * PI * 3.5 ) );
}
double easeInOutBounce( double t ) {
if( t < 0.5 ) {
return 8 * pow( 2, 8 * (t - 1) ) * abs( sin( t * PI * 7 ) );
} else {
return 1 - 8 * pow( 2, -8 * t ) * abs( sin( t * PI * 7 ) );
}
}
easingFunction getEasingFunction( easing_functions function )
{
static std::map< easing_functions, easingFunction > easingFunctions;
if( easingFunctions.empty() )
{
easingFunctions.insert( std::make_pair( EaseInSine, easeInSine ) );
easingFunctions.insert( std::make_pair( EaseOutSine, easeOutSine ) );
easingFunctions.insert( std::make_pair( EaseInOutSine, easeInOutSine ) );
easingFunctions.insert( std::make_pair( EaseInQuad, easeInQuad ) );
easingFunctions.insert( std::make_pair( EaseOutQuad, easeOutQuad ) );
easingFunctions.insert( std::make_pair( EaseInOutQuad, easeInOutQuad ) );
easingFunctions.insert( std::make_pair( EaseInCubic, easeInCubic ) );
easingFunctions.insert( std::make_pair( EaseOutCubic, easeOutCubic ) );
easingFunctions.insert( std::make_pair( EaseInOutCubic, easeInOutCubic ) );
easingFunctions.insert( std::make_pair( EaseInQuart, easeInQuart ) );
easingFunctions.insert( std::make_pair( EaseOutQuart, easeOutQuart ) );
easingFunctions.insert( std::make_pair( EaseInOutQuart, easeInOutQuart) );
easingFunctions.insert( std::make_pair( EaseInQuint, easeInQuint ) );
easingFunctions.insert( std::make_pair( EaseOutQuint, easeOutQuint ) );
easingFunctions.insert( std::make_pair( EaseInOutQuint, easeInOutQuint ) );
easingFunctions.insert( std::make_pair( EaseInExpo, easeInExpo ) );
easingFunctions.insert( std::make_pair( EaseOutExpo, easeOutExpo ) );
easingFunctions.insert( std::make_pair( EaseInOutExpo, easeInOutExpo ) );
easingFunctions.insert( std::make_pair( EaseInCirc, easeInCirc ) );
easingFunctions.insert( std::make_pair( EaseOutCirc, easeOutCirc ) );
easingFunctions.insert( std::make_pair( EaseInOutCirc, easeInOutCirc ) );
easingFunctions.insert( std::make_pair( EaseInBack, easeInBack ) );
easingFunctions.insert( std::make_pair( EaseOutBack, easeOutBack ) );
easingFunctions.insert( std::make_pair( EaseInOutBack, easeInOutBack ) );
easingFunctions.insert( std::make_pair( EaseInElastic, easeInElastic ) );
easingFunctions.insert( std::make_pair( EaseOutElastic, easeOutElastic ) );
easingFunctions.insert( std::make_pair( EaseInOutElastic, easeInOutElastic ) );
easingFunctions.insert( std::make_pair( EaseInBounce, easeInBounce ) );
easingFunctions.insert( std::make_pair( EaseOutBounce, easeOutBounce ) );
easingFunctions.insert( std::make_pair( EaseInOutBounce, easeInOutBounce ) );
}
auto it = easingFunctions.find( function );
return it == easingFunctions.end() ? nullptr : it->second;
}

40
source/easing.h Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
enum easing_functions
{
EaseInSine,
EaseOutSine,
EaseInOutSine,
EaseInQuad,
EaseOutQuad,
EaseInOutQuad,
EaseInCubic,
EaseOutCubic,
EaseInOutCubic,
EaseInQuart,
EaseOutQuart,
EaseInOutQuart,
EaseInQuint,
EaseOutQuint,
EaseInOutQuint,
EaseInExpo,
EaseOutExpo,
EaseInOutExpo,
EaseInCirc,
EaseOutCirc,
EaseInOutCirc,
EaseInBack,
EaseOutBack,
EaseInOutBack,
EaseInElastic,
EaseOutElastic,
EaseInOutElastic,
EaseInBounce,
EaseOutBounce,
EaseInOutBounce
};
typedef double(*easingFunction)(double);
easingFunction getEasingFunction( easing_functions function );

View File

@@ -22,7 +22,10 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board)
SDL_SetTextureBlendMode(layer, SDL_BLENDMODE_BLEND);
// Inicializa las variables
rect = {SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT};
counter = 0;
movingCounter = 0;
fadingCounter = 0;
state = sb_hide;
}
@@ -39,6 +42,11 @@ ScoreBoard::~ScoreBoard()
// Dibuja el marcador en la textura
void ScoreBoard::fillTexture()
{
if (state == sb_hide)
{
return;
}
// Cambia el puntero del renderizador a la textura y la limpia
SDL_SetRenderTarget(renderer, layer);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
@@ -72,21 +80,67 @@ void ScoreBoard::render()
{
return;
}
// Dibuja la textura con el marcador en pantalla
SDL_Rect rect = {SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT};
// SDL_Rect rect = {SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT};
SDL_RenderCopy(renderer, layer, NULL, &rect);
}
// Actualiza las variables del objeto
void ScoreBoard::update()
{
if (counter == 200)
{
state = sb_show;
}
else
if (state == sb_hide)
{
counter++;
if (counter == 200)
{
state = sb_showing;
rect.y = SCOREBOARD_Y - SCOREBOARD_HEIGHT;
}
}
else if (state == sb_showing)
{
movingCounter++;
fadingCounter++;
// float step = ((float)fadingCounter / (float)SCOREBOARD_HEIGHT);
auto easingFunction = getEasingFunction(EaseOutSine);
auto step = easingFunction(((float)fadingCounter / (float)SCOREBOARD_HEIGHT));
int alpha = 0 + ((255 - 0) * step);
int pos = -32 + ((0 + 32) * step);
rect.y = pos;
SDL_SetTextureAlphaMod(layer, alpha);
if (rect.y == SCOREBOARD_Y)
{
state = sb_show;
SDL_SetTextureAlphaMod(layer, 255);
}
}
else if (state == sb_hiding)
{
movingCounter--;
fadingCounter--;
auto easingFunction = getEasingFunction(EaseOutSine);
auto step = easingFunction(((float)fadingCounter / (float)SCOREBOARD_HEIGHT));
int alpha = 0 + ((255 - 0) * step);
int pos = -32 + ((0 + 32) * step);
rect.y = pos;
SDL_SetTextureAlphaMod(layer, alpha);
if (rect.y == SCOREBOARD_Y - SCOREBOARD_HEIGHT)
{
state = sb_hide;
counter = 0;
movingCounter = 0;
fadingCounter = 0;
SDL_SetTextureAlphaMod(layer, 255);
}
}
}
@@ -99,7 +153,20 @@ void ScoreBoard::reLoadTexture()
// Resetea el tiempo de aparición del marcador
void ScoreBoard::reset()
{
if (state == sb_hide)
{
counter = 0;
state = sb_hide;
}
else
{
state = sb_hiding;
}
}
// Devuelve el rectangulo con la posición del marcador
SDL_Rect ScoreBoard::getRect()
{
return rect;
}

View File

@@ -6,6 +6,7 @@
#include "asset.h"
#include "sprite.h"
#include "const.h"
#include "easing.h"
#include <string>
#ifndef SCOREBOARD_H
@@ -36,8 +37,11 @@ private:
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
Text *text; // Objeto para escribir texto
int counter; // Contador interno
int movingCounter; // Contador para el movimiento del marcador
int fadingCounter; // Contador para el desvanecimiento del marcador
board_t *board; // Contiene las variables a mostrar en el marcador
sb_state_e state; // Estado en el que se encuentra el marcador
SDL_Rect rect; // Posición del marcador
public:
// Constructor
@@ -60,6 +64,9 @@ public:
// Resetea el tiempo de aparición del marcador
void reset();
// Devuelve el rectangulo con la posición del marcador
SDL_Rect getRect();
};
#endif