forked from jaildesigner-jailgames/jaildoctors_dilemma
158 lines
3.4 KiB
C++
158 lines
3.4 KiB
C++
#include "fade.h"
|
|
#include "const.h"
|
|
|
|
// Constructor
|
|
Fade::Fade(SDL_Renderer *renderer)
|
|
{
|
|
renderer = renderer;
|
|
|
|
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
|
if (backbuffer == nullptr)
|
|
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
|
|
// Destructor
|
|
Fade::~Fade()
|
|
{
|
|
SDL_DestroyTexture(backbuffer);
|
|
backbuffer = nullptr;
|
|
}
|
|
|
|
// Inicializa las variables
|
|
void Fade::init(color_t color)
|
|
{
|
|
fadeType = FADE_CENTER;
|
|
enabled = false;
|
|
finished = false;
|
|
counter = 0;
|
|
this->color = color;
|
|
}
|
|
|
|
// Pinta una transición en pantalla
|
|
void Fade::render()
|
|
{
|
|
if (enabled && !finished)
|
|
{
|
|
switch (fadeType)
|
|
{
|
|
case FADE_FULLSCREEN:
|
|
rect1 = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
|
|
|
for (int i = 0; i < 256; i += 4)
|
|
{
|
|
// Dibujamos sobre el renderizador
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
|
|
// Copia el backbuffer con la imagen que había al renderizador
|
|
SDL_RenderCopy(renderer, backbuffer, nullptr, nullptr);
|
|
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, i);
|
|
SDL_RenderFillRect(renderer, &rect1);
|
|
|
|
// Vuelca el renderizador en pantalla
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
// Deja todos los buffers del mismo color
|
|
SDL_SetRenderTarget(renderer, backbuffer);
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
|
SDL_RenderClear(renderer);
|
|
break;
|
|
|
|
case FADE_CENTER:
|
|
rect1 = {0, 0, GAMECANVAS_WIDTH, 0};
|
|
rect2 = {0, 0, GAMECANVAS_WIDTH, 0};
|
|
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 64);
|
|
|
|
for (int i = 0; i < counter; i++)
|
|
{
|
|
rect1.h = rect2.h = i * 4;
|
|
rect2.y = GAMECANVAS_HEIGHT - (i * 4);
|
|
|
|
SDL_RenderFillRect(renderer, &rect1);
|
|
SDL_RenderFillRect(renderer, &rect2);
|
|
}
|
|
|
|
if ((counter * 4) > GAMECANVAS_HEIGHT)
|
|
finished = true;
|
|
break;
|
|
|
|
case FADE_RANDOM_SQUARE:
|
|
rect1 = {0, 0, 32, 32};
|
|
|
|
for (Uint16 i = 0; i < 50; i++)
|
|
{
|
|
// Crea un color al azar
|
|
color.r = 255 * (rand() % 2);
|
|
color.g = 255 * (rand() % 2);
|
|
color.b = 255 * (rand() % 2);
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 64);
|
|
|
|
// Dibujamos sobre el backbuffer
|
|
SDL_SetRenderTarget(renderer, backbuffer);
|
|
|
|
rect1.x = rand() % (GAMECANVAS_WIDTH - rect1.w);
|
|
rect1.y = rand() % (GAMECANVAS_HEIGHT - rect1.h);
|
|
SDL_RenderFillRect(renderer, &rect1);
|
|
|
|
// Volvemos a usar el renderizador de forma normal
|
|
SDL_SetRenderTarget(renderer, nullptr);
|
|
|
|
// Copiamos el backbuffer al renderizador
|
|
SDL_RenderCopy(renderer, backbuffer, nullptr, nullptr);
|
|
|
|
// Volcamos el renderizador en pantalla
|
|
SDL_RenderPresent(renderer);
|
|
SDL_Delay(100);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (finished)
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
|
SDL_RenderClear(renderer);
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables internas
|
|
void Fade::update()
|
|
{
|
|
if (enabled)
|
|
counter++;
|
|
}
|
|
|
|
// Activa el fade
|
|
void Fade::activateFade()
|
|
{
|
|
enabled = true;
|
|
finished = false;
|
|
counter = 0;
|
|
}
|
|
|
|
// Comprueba si está activo
|
|
bool Fade::isEnabled()
|
|
{
|
|
return enabled;
|
|
}
|
|
|
|
// Comprueba si ha terminado la transicion
|
|
bool Fade::hasEnded()
|
|
{
|
|
return finished;
|
|
}
|
|
|
|
// Establece el tipo de fade
|
|
void Fade::setFadeType(int fadeType)
|
|
{
|
|
fadeType = fadeType;
|
|
} |