forked from jaildesigner-jailgames/jaildoctors_dilemma
Añadido efecto básico de fade a la clase screen
This commit is contained in:
158
source/fade.cpp
158
source/fade.cpp
@@ -1,158 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
#include "ltexture.h"
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
#ifndef FADE_H
|
|
||||||
#define FADE_H
|
|
||||||
|
|
||||||
// Tipos de fundido
|
|
||||||
#define FADE_FULLSCREEN 0
|
|
||||||
#define FADE_CENTER 1
|
|
||||||
#define FADE_RANDOM_SQUARE 2
|
|
||||||
|
|
||||||
// Clase Fade
|
|
||||||
class Fade
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
|
||||||
SDL_Texture *backbuffer; // Textura para usar como backbuffer
|
|
||||||
Uint8 fadeType; // Tipo de fade a realizar
|
|
||||||
Uint16 counter; // Contador interno
|
|
||||||
bool enabled; // Indica si el fade está activo
|
|
||||||
bool finished; // Indica si ha terminado la transición
|
|
||||||
color_t color; // Colores para el fade
|
|
||||||
SDL_Rect rect1; // Rectangulo usado para crear los efectos de transición
|
|
||||||
SDL_Rect rect2; // Rectangulo usado para crear los efectos de transición
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructor
|
|
||||||
Fade(SDL_Renderer *renderer);
|
|
||||||
|
|
||||||
// Destructor
|
|
||||||
~Fade();
|
|
||||||
|
|
||||||
// Inicializa las variables
|
|
||||||
void init(color_t color);
|
|
||||||
|
|
||||||
// Pinta una transición en pantalla
|
|
||||||
void render();
|
|
||||||
|
|
||||||
// Actualiza las variables internas
|
|
||||||
void update();
|
|
||||||
|
|
||||||
// Activa el fade
|
|
||||||
void activateFade();
|
|
||||||
|
|
||||||
// Comprueba si ha terminado la transicion
|
|
||||||
bool hasEnded();
|
|
||||||
|
|
||||||
// Comprueba si está activo
|
|
||||||
bool isEnabled();
|
|
||||||
|
|
||||||
// Establece el tipo de fade
|
|
||||||
void setFadeType(int fadeType);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -112,6 +112,7 @@ void Game::checkEventHandler()
|
|||||||
case SDL_SCANCODE_M:
|
case SDL_SCANCODE_M:
|
||||||
board.music = !board.music;
|
board.music = !board.music;
|
||||||
board.music ? JA_ResumeMusic() : JA_PauseMusic();
|
board.music ? JA_ResumeMusic() : JA_PauseMusic();
|
||||||
|
screen->setFade();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_SCANCODE_P:
|
case SDL_SCANCODE_P:
|
||||||
@@ -214,8 +215,10 @@ void Game::update()
|
|||||||
checkIfPlayerIsAlive();
|
checkIfPlayerIsAlive();
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
scoreboard->update();
|
scoreboard->update();
|
||||||
|
|
||||||
updateDebugInfo();
|
updateDebugInfo();
|
||||||
updateBlackScreen();
|
updateBlackScreen();
|
||||||
|
screen->updateFX();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +240,7 @@ void Game::render()
|
|||||||
// Debug info
|
// Debug info
|
||||||
renderDebugInfo();
|
renderDebugInfo();
|
||||||
// test->render();
|
// test->render();
|
||||||
|
screen->renderFX();
|
||||||
|
|
||||||
// Actualiza la pantalla
|
// Actualiza la pantalla
|
||||||
screen->blit();
|
screen->blit();
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, i
|
|||||||
gameCanvasWidth = gameInternalResX;
|
gameCanvasWidth = gameInternalResX;
|
||||||
gameCanvasHeight = gameInternalResY;
|
gameCanvasHeight = gameInternalResY;
|
||||||
|
|
||||||
|
fade = false;
|
||||||
|
fadeCounter = 0;
|
||||||
|
fadeLenght = 200;
|
||||||
|
|
||||||
// Define el color del borde para el modo de pantalla completa
|
// Define el color del borde para el modo de pantalla completa
|
||||||
borderColor = {0x00, 0x00, 0x00};
|
borderColor = {0x00, 0x00, 0x00};
|
||||||
|
|
||||||
@@ -164,7 +168,7 @@ void Screen::switchVideoMode()
|
|||||||
{
|
{
|
||||||
options->fullScreenMode = 0;
|
options->fullScreenMode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
setVideoMode(options->fullScreenMode);
|
setVideoMode(options->fullScreenMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,4 +208,65 @@ void Screen::switchBorder()
|
|||||||
{
|
{
|
||||||
options->borderEnabled = !options->borderEnabled;
|
options->borderEnabled = !options->borderEnabled;
|
||||||
setVideoMode(0);
|
setVideoMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Activa el fade
|
||||||
|
void Screen::setFade()
|
||||||
|
{
|
||||||
|
fade = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el fade
|
||||||
|
bool Screen::fadeEnded()
|
||||||
|
{
|
||||||
|
if (fade || fadeCounter > 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el fade
|
||||||
|
void Screen::updateFade()
|
||||||
|
{
|
||||||
|
if (!fade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fadeCounter++;
|
||||||
|
if (fadeCounter > fadeLenght)
|
||||||
|
{
|
||||||
|
fade = false;
|
||||||
|
fadeCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja el fade
|
||||||
|
void Screen::renderFade()
|
||||||
|
{
|
||||||
|
if (!fade)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||||
|
color_t color = {0, 0, 0};
|
||||||
|
const float step = (float)fadeCounter / (float)fadeLenght;
|
||||||
|
const int alpha = 0 + (255 - 0) * step;
|
||||||
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza los efectos
|
||||||
|
void Screen::updateFX()
|
||||||
|
{
|
||||||
|
updateFade();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuja los efectos
|
||||||
|
void Screen::renderFX()
|
||||||
|
{
|
||||||
|
renderFade();
|
||||||
}
|
}
|
||||||
@@ -36,6 +36,17 @@ private:
|
|||||||
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
||||||
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||||
|
|
||||||
|
// EFECTOS
|
||||||
|
bool fade; // Indica si esta activo el efecto de fade
|
||||||
|
int fadeCounter; // Temporizador para el efecto de fade
|
||||||
|
int fadeLenght; // Duración del fade
|
||||||
|
|
||||||
|
// Actualiza el fade
|
||||||
|
void updateFade();
|
||||||
|
|
||||||
|
// Dibuja el fade
|
||||||
|
void renderFade();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY);
|
Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY);
|
||||||
@@ -75,6 +86,19 @@ public:
|
|||||||
|
|
||||||
// Cambia entre borde visible y no visible
|
// Cambia entre borde visible y no visible
|
||||||
void switchBorder();
|
void switchBorder();
|
||||||
|
|
||||||
|
// Activa el fade
|
||||||
|
void setFade();
|
||||||
|
|
||||||
|
// Comprueba si ha terminado el fade
|
||||||
|
bool fadeEnded();
|
||||||
|
|
||||||
|
// Actualiza los efectos
|
||||||
|
void updateFX();
|
||||||
|
|
||||||
|
//Dibuja los efectos
|
||||||
|
void renderFX();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user