clang-format
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include "screen.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm> // for max, min
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#include <string> // for basic_string, char_traits, string
|
||||
|
||||
#include <algorithm> // for max, min
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
||||
#include <string> // for basic_string, char_traits, string
|
||||
class Asset;
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
|
||||
{
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) {
|
||||
// Inicializa variables
|
||||
this->window = window;
|
||||
this->renderer = renderer;
|
||||
@@ -29,14 +30,11 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
|
||||
if (gameCanvas != nullptr)
|
||||
{
|
||||
if (gameCanvas != nullptr) {
|
||||
SDL_SetTextureScaleMode(gameCanvas, options->filter == FILTER_NEAREST ? SDL_SCALEMODE_NEAREST : SDL_SCALEMODE_LINEAR);
|
||||
}
|
||||
if (gameCanvas == nullptr)
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
if (gameCanvas == nullptr) {
|
||||
if (options->console) {
|
||||
std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -49,27 +47,23 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Screen::~Screen()
|
||||
{
|
||||
Screen::~Screen() {
|
||||
SDL_DestroyTexture(gameCanvas);
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
void Screen::clean(color_t color)
|
||||
{
|
||||
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()
|
||||
{
|
||||
void Screen::start() {
|
||||
SDL_SetRenderTarget(renderer, gameCanvas);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void Screen::blit()
|
||||
{
|
||||
void Screen::blit() {
|
||||
// Vuelve a dejar el renderizador en modo normal
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
@@ -86,29 +80,25 @@ void Screen::blit()
|
||||
}
|
||||
|
||||
// Establece el modo de video
|
||||
void Screen::setVideoMode(int videoMode)
|
||||
{
|
||||
void Screen::setVideoMode(int videoMode) {
|
||||
// Aplica el modo de video
|
||||
SDL_SetWindowFullscreen(window, videoMode != 0);
|
||||
|
||||
// Si está activo el modo ventana quita el borde
|
||||
if (videoMode == 0)
|
||||
{
|
||||
if (videoMode == 0) {
|
||||
// Muestra el puntero
|
||||
SDL_ShowCursor();
|
||||
|
||||
// Esconde la ventana
|
||||
//SDL_HideWindow(window);
|
||||
// SDL_HideWindow(window);
|
||||
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
if (options->borderEnabled) {
|
||||
windowWidth = gameCanvasWidth + borderWidth;
|
||||
windowHeight = gameCanvasHeight + borderHeight;
|
||||
dest = {0 + (borderWidth / 2), 0 + (borderHeight / 2), gameCanvasWidth, gameCanvasHeight};
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
windowWidth = gameCanvasWidth;
|
||||
windowHeight = gameCanvasHeight;
|
||||
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
@@ -119,12 +109,11 @@ void Screen::setVideoMode(int videoMode)
|
||||
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
|
||||
// Muestra la ventana
|
||||
//SDL_ShowWindow(window);
|
||||
// SDL_ShowWindow(window);
|
||||
}
|
||||
|
||||
// Si está activo el modo de pantalla completa añade el borde
|
||||
else if (videoMode == SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
else if (videoMode == SDL_WINDOW_FULLSCREEN) {
|
||||
// Oculta el puntero
|
||||
SDL_HideCursor();
|
||||
|
||||
@@ -132,12 +121,10 @@ void Screen::setVideoMode(int videoMode)
|
||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||
|
||||
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||
if (options->integerScale)
|
||||
{
|
||||
if (options->integerScale) {
|
||||
// Calcula el tamaño de la escala máxima
|
||||
int scale = 0;
|
||||
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight))
|
||||
{
|
||||
while (((gameCanvasWidth * (scale + 1)) <= windowWidth) && ((gameCanvasHeight * (scale + 1)) <= windowHeight)) {
|
||||
scale++;
|
||||
}
|
||||
|
||||
@@ -145,27 +132,20 @@ void Screen::setVideoMode(int videoMode)
|
||||
dest.h = gameCanvasHeight * scale;
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
else if (options->keepAspect)
|
||||
{
|
||||
} else if (options->keepAspect) {
|
||||
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||
if ((windowWidth - gameCanvasWidth) >= (windowHeight - 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
|
||||
{
|
||||
} else {
|
||||
dest.w = windowWidth;
|
||||
dest.h = (int)((windowWidth / ratio) + 0.5f);
|
||||
dest.x = (windowWidth - dest.w) / 2;
|
||||
dest.y = (windowHeight - dest.h) / 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dest.w = windowWidth;
|
||||
dest.h = windowHeight;
|
||||
dest.x = dest.y = 0;
|
||||
@@ -182,83 +162,70 @@ void Screen::setVideoMode(int videoMode)
|
||||
}
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void Screen::switchVideoMode()
|
||||
{
|
||||
void Screen::switchVideoMode() {
|
||||
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN : 0;
|
||||
setVideoMode(options->videoMode);
|
||||
}
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void Screen::setWindowSize(int size)
|
||||
{
|
||||
void Screen::setWindowSize(int size) {
|
||||
options->windowSize = size;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Reduce el tamaño de la ventana
|
||||
void Screen::decWindowSize()
|
||||
{
|
||||
void Screen::decWindowSize() {
|
||||
--options->windowSize;
|
||||
options->windowSize = std::max(options->windowSize, 1);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Aumenta el tamaño de la ventana
|
||||
void Screen::incWindowSize()
|
||||
{
|
||||
void Screen::incWindowSize() {
|
||||
++options->windowSize;
|
||||
options->windowSize = std::min(options->windowSize, 4);
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
void Screen::setBorderColor(color_t color)
|
||||
{
|
||||
void Screen::setBorderColor(color_t color) {
|
||||
borderColor = color;
|
||||
}
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
void Screen::setBlendMode(SDL_BlendMode blendMode) {
|
||||
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderWidth(int s)
|
||||
{
|
||||
void Screen::setBorderWidth(int s) {
|
||||
options->borderWidth = s;
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderHeight(int s)
|
||||
{
|
||||
void Screen::setBorderHeight(int s) {
|
||||
options->borderHeight = s;
|
||||
}
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void Screen::setBorderEnabled(bool value)
|
||||
{
|
||||
void Screen::setBorderEnabled(bool value) {
|
||||
options->borderEnabled = value;
|
||||
}
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void Screen::switchBorder()
|
||||
{
|
||||
void Screen::switchBorder() {
|
||||
options->borderEnabled = !options->borderEnabled;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Activa el fade
|
||||
void Screen::setFade()
|
||||
{
|
||||
void Screen::setFade() {
|
||||
fade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool Screen::fadeEnded()
|
||||
{
|
||||
if (fade || fadeCounter > 0)
|
||||
{
|
||||
bool Screen::fadeEnded() {
|
||||
if (fade || fadeCounter > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -266,16 +233,13 @@ bool Screen::fadeEnded()
|
||||
}
|
||||
|
||||
// Activa el spectrum fade
|
||||
void Screen::setspectrumFade()
|
||||
{
|
||||
void Screen::setspectrumFade() {
|
||||
spectrumFade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool Screen::spectrumFadeEnded()
|
||||
{
|
||||
if (spectrumFade || spectrumFadeCounter > 0)
|
||||
{
|
||||
bool Screen::spectrumFadeEnded() {
|
||||
if (spectrumFade || spectrumFadeCounter > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -283,33 +247,27 @@ bool Screen::spectrumFadeEnded()
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void Screen::iniFade()
|
||||
{
|
||||
void Screen::iniFade() {
|
||||
fade = false;
|
||||
fadeCounter = 0;
|
||||
fadeLenght = 200;
|
||||
}
|
||||
|
||||
// Actualiza el fade
|
||||
void Screen::updateFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
void Screen::updateFade() {
|
||||
if (!fade) {
|
||||
return;
|
||||
}
|
||||
|
||||
fadeCounter++;
|
||||
if (fadeCounter > fadeLenght)
|
||||
{
|
||||
if (fadeCounter > fadeLenght) {
|
||||
iniFade();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el fade
|
||||
void Screen::renderFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
void Screen::renderFade() {
|
||||
if (!fade) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -322,8 +280,7 @@ void Screen::renderFade()
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void Screen::iniSpectrumFade()
|
||||
{
|
||||
void Screen::iniSpectrumFade() {
|
||||
spectrumFade = false;
|
||||
spectrumFadeCounter = 0;
|
||||
spectrumFadeLenght = 50;
|
||||
@@ -332,33 +289,27 @@ void Screen::iniSpectrumFade()
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<std::string> vColors = {"black", "blue", "red", "magenta", "green", "cyan", "yellow", "bright_white"};
|
||||
for (auto v : vColors)
|
||||
{
|
||||
for (auto v : vColors) {
|
||||
spectrumColor.push_back(stringToColor(options->palette, v));
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void Screen::updateSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
void Screen::updateSpectrumFade() {
|
||||
if (!spectrumFade) {
|
||||
return;
|
||||
}
|
||||
|
||||
spectrumFadeCounter++;
|
||||
if (spectrumFadeCounter > spectrumFadeLenght)
|
||||
{
|
||||
if (spectrumFadeCounter > spectrumFadeLenght) {
|
||||
iniSpectrumFade();
|
||||
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void Screen::renderSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
void Screen::renderSpectrumFade() {
|
||||
if (!spectrumFade) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -370,15 +321,13 @@ void Screen::renderSpectrumFade()
|
||||
}
|
||||
|
||||
// Actualiza los efectos
|
||||
void Screen::updateFX()
|
||||
{
|
||||
void Screen::updateFX() {
|
||||
updateFade();
|
||||
updateSpectrumFade();
|
||||
}
|
||||
|
||||
// Dibuja los efectos
|
||||
void Screen::renderFX()
|
||||
{
|
||||
void Screen::renderFX() {
|
||||
renderFade();
|
||||
renderSpectrumFade();
|
||||
}
|
||||
Reference in New Issue
Block a user