Files
coffee_crisis/source/screen.cpp
2022-09-26 18:18:27 +02:00

363 lines
8.7 KiB
C++

#include "screen.h"
#include <string>
#include <stdio.h>
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY)
{
// Inicializa variables
this->window = window;
this->renderer = renderer;
this->options = options;
gameCanvasWidth = gameInternalResX;
gameCanvasHeight = gameInternalResY;
iniFade();
iniSpectrumFade();
// 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);
if (gameCanvas == NULL)
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
// Establece el modo de video
setVideoMode(options->fullScreenMode);
// Calcula los anclajes
anchor.left = 0;
anchor.right = gameCanvasWidth;
anchor.center = gameCanvasWidth / 2;
anchor.top = 0;
anchor.bottom = gameCanvasHeight;
anchor.middle = gameCanvasHeight / 2;
}
// Destructor
Screen::~Screen()
{
renderer = nullptr;
}
// 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, NULL);
// 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, NULL, &dest);
// Muestra por pantalla el renderizador
SDL_RenderPresent(renderer);
}
// Establece el modo de video
void Screen::setVideoMode(int fullScreenMode)
{
// Aplica el modo de video
SDL_SetWindowFullscreen(window, fullScreenMode);
// Si está activo el modo ventana quita el borde
if (fullScreenMode == 0)
{
if (options->borderEnabled)
{
const int incWidth = gameCanvasWidth * options->borderSize;
const int incHeight = gameCanvasHeight * options->borderSize;
screenWidth = gameCanvasWidth + incWidth;
screenHeight = gameCanvasHeight + incHeight;
dest = {0 + (incWidth / 2), 0 + (incHeight / 2), gameCanvasWidth, gameCanvasHeight};
}
else
{
screenWidth = gameCanvasWidth;
screenHeight = gameCanvasHeight;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
}
// Modifica el tamaño del renderizador y de la ventana
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
SDL_SetWindowSize(window, screenWidth * options->windowSize, screenHeight * options->windowSize);
}
// Si está activo el modo de pantalla completa añade el borde
else if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &screenWidth, &screenHeight);
// Aplica el escalado al rectangulo donde se pinta la textura del juego
if (options->integerScale)
{
// Calcula el tamaño de la escala máxima
int scale = 0;
while (((gameCanvasWidth * (scale + 1)) <= screenWidth) && ((gameCanvasHeight * (scale + 1)) <= screenHeight))
{
scale++;
}
dest.w = gameCanvasWidth * scale;
dest.h = gameCanvasHeight * scale;
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
else if (options->keepAspect)
{
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((screenWidth - gameCanvasWidth) >= (screenHeight - gameCanvasHeight))
{
dest.h = screenHeight;
dest.w = (int)((screenHeight * ratio) + 0.5f);
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
else
{
dest.w = screenWidth;
dest.h = (int)((screenWidth / ratio) + 0.5f);
dest.x = (screenWidth - dest.w) / 2;
dest.y = (screenHeight - dest.h) / 2;
}
}
else
{
dest.w = screenWidth;
dest.h = screenHeight;
dest.x = dest.y = 0;
}
// Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
}
// Actualiza el valor de la variable
options->fullScreenMode = fullScreenMode;
}
// Camibia entre pantalla completa y ventana
void Screen::switchVideoMode()
{
if (options->fullScreenMode == 0)
{
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else
{
options->fullScreenMode = 0;
}
setVideoMode(options->fullScreenMode);
}
// Cambia el tamaño de la ventana
void Screen::setWindowSize(int size)
{
options->windowSize = size;
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::setBorderSize(float s)
{
options->borderSize = s;
}
// Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value)
{
options->borderEnabled = value;
}
// Cambia entre borde visible y no visible
void Screen::switchBorder()
{
options->borderEnabled = !options->borderEnabled;
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;
}
// Activa el spectrum fade
void Screen::setspectrumFade()
{
spectrumFade = true;
}
// Comprueba si ha terminado el spectrum fade
bool Screen::spectrumFadeEnded()
{
if (spectrumFade || spectrumFadeCounter > 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);
}
// Inicializa las variables para el fade spectrum
void Screen::iniSpectrumFade()
{
spectrumFade = false;
spectrumFadeCounter = 0;
spectrumFadeLenght = 50;
spectrumColor.clear();
color_t c;
c = stringToColor("black");
spectrumColor.push_back(c);
c = stringToColor("blue");
spectrumColor.push_back(c);
c = stringToColor("red");
spectrumColor.push_back(c);
c = stringToColor("magenta");
spectrumColor.push_back(c);
c = stringToColor("green");
spectrumColor.push_back(c);
c = stringToColor("cyan");
spectrumColor.push_back(c);
c = stringToColor("yellow");
spectrumColor.push_back(c);
c = stringToColor("bright_white");
spectrumColor.push_back(c);
}
// Actualiza el spectrum fade
void Screen::updateSpectrumFade()
{
if (!spectrumFade)
{
return;
}
spectrumFadeCounter++;
if (spectrumFadeCounter > spectrumFadeLenght)
{
iniSpectrumFade();
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
}
}
// Dibuja el spectrum fade
void Screen::renderSpectrumFade()
{
if (!spectrumFade)
{
return;
}
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
const int max = spectrumColor.size() - 1;
const int index = max + (0 - max) * step;
const color_t c = spectrumColor[index];
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
}
// Actualiza los efectos
void Screen::updateFX()
{
updateFade();
updateSpectrumFade();
}
// Dibuja los efectos
void Screen::renderFX()
{
renderFade();
renderSpectrumFade();
}