Files
coffee_crisis_arcade_edition/source/common/screen.cpp

344 lines
8.4 KiB
C++

#include "screen.h"
#include <string>
#include <iostream>
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
{
// Copia punteros
this->window = window;
this->renderer = renderer;
this->options = options;
this->asset = asset;
// Inicializa variables
windowWidth = 0;
windowHeight = 0;
gameCanvasWidth = options->video.gameWidth;
gameCanvasHeight = options->video.gameHeight;
borderWidth = options->video.border.width * 2;
borderHeight = options->video.border.height * 2;
dest = {0, 0, 0, 0};
borderColor = {0, 0, 0};
fade = 0;
fadeCounter = 0;
fadeLenght = 0;
shake.desp = 1;
shake.delay = 3;
shake.counter = 0;
shake.lenght = 8;
shake.remaining = 0;
shake.origin = 0;
iniFade();
// Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00};
// Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
// Establece el modo de video
setVideoMode(options->video.mode);
}
// Destructor
Screen::~Screen()
{
SDL_DestroyTexture(gameCanvas);
}
// Limpia la pantalla
void Screen::clean(color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(renderer);
}
// Prepara para empezar a dibujar en la textura de juego
void Screen::start()
{
SDL_SetRenderTarget(renderer, gameCanvas);
}
// Vuelca el contenido del renderizador en pantalla
void Screen::blit()
{
// Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr);
// Borra el contenido previo
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(renderer);
// Copia la textura de juego en el renderizador en la posición adecuada
SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest);
// Muestra por pantalla el renderizador
SDL_RenderPresent(renderer);
}
// Establece el modo de video
void Screen::setVideoMode(int videoMode)
{
// Aplica el modo de video
SDL_SetWindowFullscreen(window, videoMode);
// Si está activo el modo ventana quita el borde
if (videoMode == 0)
{
// Muestra el puntero
SDL_ShowCursor(SDL_ENABLE);
// Esconde la ventana
// SDL_HideWindow(window);
if (options->video.border.enabled)
{
windowWidth = gameCanvasWidth + borderWidth;
windowHeight = gameCanvasHeight + borderHeight;
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
}
else
{
windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
}
// Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->video.window.size, windowHeight * options->video.window.size);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
// Si está activo el modo de pantalla completa añade el borde
else if (videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
// Oculta el puntero
SDL_ShowCursor(SDL_DISABLE);
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
// Aplica el escalado al rectangulo donde se pinta la textura del juego
if (options->video.integerScale)
{
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
{
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
}
else if (options->video.keepAspect)
{
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
{
dest.h = windowHeight;
dest.w = (int)((windowHeight * ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
}
else
{
dest.w = windowWidth;
dest.h = (int)((windowWidth / ratio) + 0.5f);
dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2;
}
}
else
{
dest.w = windowWidth;
dest.h = windowHeight;
dest.x = dest.y = 0;
}
}
// Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
// Actualiza las opciones
options->video.mode = videoMode;
options->video.window.width = windowWidth;
options->video.window.height = windowHeight;
}
// Camibia entre pantalla completa y ventana
void Screen::switchVideoMode()
{
options->video.mode = (options->video.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options->video.mode);
}
// Cambia el tamaño de la ventana
void Screen::setWindowSize(int size)
{
options->video.window.size = size;
setVideoMode(0);
}
// Reduce el tamaño de la ventana
void Screen::decWindowSize()
{
--options->video.window.size;
options->video.window.size = std::max(options->video.window.size, 1);
setVideoMode(0);
}
// Aumenta el tamaño de la ventana
void Screen::incWindowSize()
{
++options->video.window.size;
options->video.window.size = std::min(options->video.window.size, 4);
setVideoMode(0);
}
// Cambia el color del borde
void Screen::setBorderColor(color_t color)
{
borderColor = color;
}
// Cambia el tipo de mezcla
void Screen::setBlendMode(SDL_BlendMode blendMode)
{
SDL_SetRenderDrawBlendMode(renderer, blendMode);
}
// Establece el tamaño del borde
void Screen::setBorderWidth(int s)
{
options->video.border.width = s;
}
// Establece el tamaño del borde
void Screen::setBorderHeight(int s)
{
options->video.border.height = s;
}
// Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value)
{
options->video.border.enabled = value;
}
// Cambia entre borde visible y no visible
void Screen::switchBorder()
{
options->video.border.enabled = !options->video.border.enabled;
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;
}
// Inicializa las variables para el fade
void Screen::iniFade()
{
fade = false;
fadeCounter = 0;
fadeLenght = 200;
}
// Actualiza el fade
void Screen::updateFade()
{
if (!fade)
{
return;
}
fadeCounter++;
if (fadeCounter > fadeLenght)
{
iniFade();
}
}
// 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();
}
// Actualiza la lógica de la clase
void Screen::update()
{
updateShake();
}
// Agita la pantalla
void Screen::startShake()
{
shake.remaining = shake.lenght;
shake.counter = shake.delay;
shake.origin = dest.x;
}
// Actualiza la logica para agitar la pantalla
void Screen::updateShake()
{
if (shake.remaining > 0)
{
if (shake.counter > 0)
{
shake.counter--;
}
else
{
shake.counter = shake.delay;
const int desp = shake.remaining % 2 == 0 ? shake.desp * (-1) : shake.desp;
dest.x = shake.origin + desp;
shake.remaining--;
}
}
else
{
dest.x = shake.origin;
}
}