El responsable de comprobar si se ha pulsado alguna tecla para cambiar el tamaño de la venta, el modo de pantalla completa o la activación de los shaders pasa a ser la clase screen

This commit is contained in:
2024-07-05 14:09:38 +02:00
parent 5e7212dfaa
commit 62f3c42e7b
14 changed files with 209 additions and 187 deletions

View File

@@ -8,13 +8,14 @@
#endif
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options)
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options)
{
// Copia punteros
this->window = window;
this->renderer = renderer;
this->options = options;
this->asset = asset;
this->input = input;
this->options = options;
// Inicializa variables
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
@@ -80,7 +81,20 @@ void Screen::blit()
// Atenua la pantalla
doAttenuate();
#ifndef RASPI
#ifdef RASPI
// 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);
#else
if (options->video.shaders)
{
shader::render();
@@ -100,19 +114,6 @@ void Screen::blit()
// Muestra por pantalla el renderizador
SDL_RenderPresent(renderer);
}
#else
// 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);
#endif
}
@@ -200,7 +201,9 @@ void Screen::setVideoMode(int videoMode)
}
}
#ifndef RASPI
#ifdef RASPI
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
#else
// Reinicia los shaders
if (options->video.shaders)
{
@@ -215,8 +218,6 @@ void Screen::setVideoMode(int videoMode)
{
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
}
#else
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
#endif
// Actualiza las opciones
@@ -369,6 +370,30 @@ void Screen::update()
updateShake();
}
// Comprueba las entradas
void Screen::checkInput()
{
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
incWindowSize();
}
else if (input->checkInput(input_video_shaders, REPEAT_FALSE))
{
switchShaders();
}
}
// Agita la pantalla
void Screen::shake()
{

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h>
#include "asset.h"
#include "utils.h"
#include "input.h"
#include "../const.h"
#include <vector>
@@ -19,6 +20,7 @@ private:
SDL_Window *window; // Ventana de la aplicación
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con el listado de recursos
Input *input; // Objeto para leer las entradas de teclado o mando
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
options_t *options; // Variable con todas las opciones del programa
@@ -81,7 +83,7 @@ private:
public:
// Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options);
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options);
// Destructor
~Screen();
@@ -89,6 +91,9 @@ public:
// Actualiza la lógica de la clase
void update();
// Comprueba las entradas
void checkInput();
// Limpia la pantalla
void clean(color_t color = {0x00, 0x00, 0x00});

View File

@@ -16,17 +16,17 @@ Director::Director(int argc, char *argv[])
{
// Inicializa variables
section = new section_t();
section->name = SECTION_PROG_HI_SCORE_TABLE;
section->name = SECTION_PROG_LOGO;
// Comprueba los parametros del programa
checkProgramArguments(argc, argv);
// Crea la carpeta del sistema donde guardar datos
createSystemFolder("jailgames");
#ifndef DEBUG
createSystemFolder("jailgames/coffee_crisis_arcade_edition");
#else
#ifdef DEBUG
createSystemFolder("jailgames/coffee_crisis_arcade_edition_debug");
#else
createSystemFolder("jailgames/coffee_crisis_arcade_edition");
#endif
// Inicializa las opciones del programa
@@ -61,7 +61,7 @@ Director::Director(int argc, char *argv[])
input = new Input(asset->get("gamecontrollerdb.txt"));
initInput();
screen = new Screen(window, renderer, asset, options);
screen = new Screen(window, renderer, asset, input, options);
// Carga los sonidos del juego
loadSounds();

View File

@@ -169,13 +169,11 @@ void Game::init(int playerID)
// Crea los jugadores
Player *player1 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((0 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, playerTextures[0], playerAnimations);
players.push_back(player1);
Player *player2 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((1 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, playerTextures[1], playerAnimations);
players.push_back(player2);
numPlayers = 2;
// Variables relacionadas con la dificultad
switch (difficulty)
@@ -1630,7 +1628,7 @@ void Game::updateDeath()
fade->activate();
}
}
if (fade->hasEnded())
{
// section->subsection = SUBSECTION_GAME_GAMEOVER;
@@ -2504,6 +2502,9 @@ void Game::update()
// Actualiza el contador de juego
counter++;
// Comprueba si la música ha de estar sonando
checkMusicStatus();
// Actualiza el objeto screen
screen->update();
@@ -2693,26 +2694,6 @@ void Game::checkGameInput()
pause(!paused);
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_video_shaders, REPEAT_FALSE))
{
screen->switchShaders();
}
// Modo Demo activo
if (demo.enabled)
{
@@ -2885,6 +2866,9 @@ void Game::checkGameInput()
}
}
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Pinta diferentes mensajes en la pantalla
@@ -2981,39 +2965,32 @@ void Game::disableTimeStopItem()
}
}
// Comprueba si la música ha de estar sonando
void Game::checkMusicStatus()
{
// Si la música no está sonando
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
{
// Reproduce la música
if (!gameCompleted)
{
if (players[0]->isAlive() || players[1]->isAlive())
{
JA_PlayMusic(music);
}
}
}
}
// Bucle para el juego
void Game::run()
{
while (section->name == SECTION_PROG_GAME)
{
// Sección juego jugando
if ((section->subsection == SUBSECTION_GAME_PLAY_1P) || (section->subsection == SUBSECTION_GAME_PLAY_2P))
{
// Si la música no está sonando
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
{
// Reproduce la música
if (!gameCompleted)
{
if (players[0]->isAlive())
{
JA_PlayMusic(music);
}
}
}
// Comprueba el teclado/mando
checkGameInput();
// Actualiza la lógica del juego
update();
// Comprueba los eventos que hay en cola
checkEvents();
// Dibuja los objetos
render();
}
checkGameInput();
update();
checkEvents(); // Tiene que ir antes del render
render();
}
}
@@ -3198,6 +3175,11 @@ void Game::checkEvents()
{
pause(false);
}
if (eventHandler->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
reloadTextures();
}
}
}
}

View File

@@ -467,6 +467,9 @@ private:
// Pausa el juego
void pause(bool value);
// Comprueba si la música ha de estar sonando
void checkMusicStatus();
public:
// Constructor
Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section, JA_Music_t *music);

View File

@@ -58,12 +58,6 @@ HiScoreTable::~HiScoreTable()
// Actualiza las variables
void HiScoreTable::update()
{
// Comprueba los eventos
checkEventHandler();
// Comprueba las entradas
checkInput();
// Actualiza las variables
if (SDL_GetTicks() - ticks > ticksSpeed)
{
@@ -144,8 +138,15 @@ void HiScoreTable::render()
screen->blit();
}
// Recarga todas las texturas
void HiScoreTable::reloadTextures()
{
text->reLoadTexture();
fillTexture();
}
// Comprueba los eventos
void HiScoreTable::checkEventHandler()
void HiScoreTable::checkEvents()
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0)
@@ -156,6 +157,15 @@ void HiScoreTable::checkEventHandler()
section->name = SECTION_PROG_QUIT;
break;
}
// Comprueba si se ha cambiado el tamaño de la ventana
else if (eventHandler->type == SDL_WINDOWEVENT)
{
if (eventHandler->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
reloadTextures();
}
}
}
}
@@ -167,27 +177,15 @@ void HiScoreTable::checkInput()
section->name = SECTION_PROG_QUIT;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE))
{
JA_StopMusic();
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Bucle para la pantalla de instrucciones
@@ -195,7 +193,9 @@ void HiScoreTable::run()
{
while (section->name == SECTION_PROG_HI_SCORE_TABLE)
{
checkInput();
update();
checkEvents(); // Tiene que ir antes del render
render();
}
}

View File

@@ -48,7 +48,7 @@ private:
void render();
// Comprueba los eventos
void checkEventHandler();
void checkEvents();
// Comprueba las entradas
void checkInput();
@@ -59,6 +59,9 @@ private:
// Crea el contenido de la textura con la lista de puntuaciones
void fillTexture();
// Recarga todas las texturas
void reloadTextures();
public:
// Constructor
HiScoreTable(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, options_t *options, section_t *section);

View File

@@ -26,8 +26,6 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset,
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param->gameWidth, param->gameHeight);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
// Inicializa variables
section->name = SECTION_PROG_INSTRUCTIONS;
ticks = 0;
@@ -35,7 +33,7 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset,
counter = 0;
counterEnd = 700;
view = {0, 0, param->gameWidth, param->gameHeight};
spritePos = {0,0};
spritePos = {0, 0};
// Rellena la textura de texto
fillTexture();
@@ -153,17 +151,17 @@ void Instructions::fillTexture()
// Calcula cual es el texto más largo de las descripciones de los items
int lenght = 0;
for (int i= 17; i <=21;++i)
for (int i = 17; i <= 21; ++i)
{
const int l = text->lenght(lang->getText(i));
lenght = l > lenght? l : lenght;
lenght = l > lenght ? l : lenght;
}
const int anchorItem = (param->gameWidth - (lenght + 24)) / 2;
// BORRAR ESTO
//SDL_SetRenderDrawColor(renderer, 255, 255, 255, 32);
//SDL_Rect rect = {10, firstLine, param->gameWidth - 20, size};
//SDL_RenderFillRect(renderer, &rect);
// SDL_SetRenderDrawColor(renderer, 255, 255, 255, 32);
// SDL_Rect rect = {10, firstLine, param->gameWidth - 20, size};
// SDL_RenderFillRect(renderer, &rect);
// Escribe el texto de las instrucciones
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, firstLine, lang->getText(11), 1, orangeColor, 1, shdwTxtColor);
@@ -179,11 +177,11 @@ void Instructions::fillTexture()
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, anchor2, lang->getText(16), 1, orangeColor, 1, shdwTxtColor);
const int anchor3 = anchor2 + spacePostHeader;
text->writeShadowed(anchorItem+24, anchor3 + spaceBetweenItemLines * 0, lang->getText(17), shdwTxtColor);
text->writeShadowed(anchorItem+24, anchor3 + spaceBetweenItemLines * 1, lang->getText(18), shdwTxtColor);
text->writeShadowed(anchorItem+24, anchor3 + spaceBetweenItemLines * 2, lang->getText(19), shdwTxtColor);
text->writeShadowed(anchorItem+24, anchor3 + spaceBetweenItemLines * 3, lang->getText(20), shdwTxtColor);
text->writeShadowed(anchorItem+24, anchor3 + spaceBetweenItemLines * 4, lang->getText(21), shdwTxtColor);
text->writeShadowed(anchorItem + 24, anchor3 + spaceBetweenItemLines * 0, lang->getText(17), shdwTxtColor);
text->writeShadowed(anchorItem + 24, anchor3 + spaceBetweenItemLines * 1, lang->getText(18), shdwTxtColor);
text->writeShadowed(anchorItem + 24, anchor3 + spaceBetweenItemLines * 2, lang->getText(19), shdwTxtColor);
text->writeShadowed(anchorItem + 24, anchor3 + spaceBetweenItemLines * 3, lang->getText(20), shdwTxtColor);
text->writeShadowed(anchorItem + 24, anchor3 + spaceBetweenItemLines * 4, lang->getText(21), shdwTxtColor);
// Deja el renderizador como estaba
SDL_SetRenderTarget(renderer, temp);
@@ -220,9 +218,6 @@ void Instructions::fillBackbuffer()
// Actualiza las variables
void Instructions::update()
{
// Comprueba las entradas
checkInput();
// Actualiza las variables
if (SDL_GetTicks() - ticks > ticksSpeed)
{
@@ -271,6 +266,17 @@ void Instructions::render()
screen->blit();
}
// Recarga todas las texturas
void Instructions::reloadTextures()
{
for (auto tex : itemTextures)
{
tex->reLoad();
}
text->reLoadTexture();
fillTexture();
}
// Comprueba los eventos
void Instructions::checkEvents()
{
@@ -283,6 +289,15 @@ void Instructions::checkEvents()
section->name = SECTION_PROG_QUIT;
break;
}
// Comprueba si se ha cambiado el tamaño de la ventana
else if (eventHandler->type == SDL_WINDOWEVENT)
{
if (eventHandler->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
reloadTextures();
}
}
}
}
@@ -294,27 +309,15 @@ void Instructions::checkInput()
section->name = SECTION_PROG_QUIT;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE))
{
JA_StopMusic();
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Bucle para la pantalla de instrucciones
@@ -322,8 +325,9 @@ void Instructions::run()
{
while (section->name == SECTION_PROG_INSTRUCTIONS)
{
checkInput();
update();
checkEvents();
checkEvents(); // Tiene que ir antes del render
render();
}
}

View File

@@ -67,6 +67,9 @@ private:
// Actualiza los sprites
void updateSprites();
// Recarga todas las texturas
void reloadTextures();
public:
// Constructor
Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section);

View File

@@ -161,6 +161,13 @@ Intro::~Intro()
}
}
// Recarga todas las texturas
void Intro::reloadTextures()
{
texture->reLoad();
text->reLoadTexture();
}
// Comprueba los eventos
void Intro::checkEvents()
{
@@ -173,6 +180,15 @@ void Intro::checkEvents()
section->name = SECTION_PROG_QUIT;
break;
}
// Comprueba si se ha cambiado el tamaño de la ventana
else if (eventHandler->type == SDL_WINDOWEVENT)
{
if (eventHandler->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
reloadTextures();
}
}
}
}
@@ -184,27 +200,15 @@ void Intro::checkInput()
section->name = SECTION_PROG_QUIT;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE))
{
JA_StopMusic();
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Actualiza las escenas de la intro
@@ -366,8 +370,6 @@ void Intro::updateScenes()
// Actualiza las variables del objeto
void Intro::update()
{
checkInput();
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
@@ -420,8 +422,9 @@ void Intro::run()
while (section->name == SECTION_PROG_INTRO)
{
checkInput();
update();
checkEvents();
checkEvents(); // Tiene que ir antes del render
render();
}
}

View File

@@ -54,6 +54,9 @@ private:
// Actualiza las escenas de la intro
void updateScenes();
// Recarga todas las texturas
void reloadTextures();
public:
// Constructor
Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, param_t *param, section_t *section, JA_Music_t *music);

View File

@@ -67,6 +67,13 @@ Logo::~Logo()
delete eventHandler;
}
// Recarga todas las texturas
void Logo::reloadTextures()
{
jailTexture->reLoad();
sinceTexture->reLoad();
}
// Comprueba el manejador de eventos
void Logo::checkEvents()
{
@@ -79,6 +86,15 @@ void Logo::checkEvents()
section->name = SECTION_PROG_QUIT;
break;
}
// Comprueba si se ha cambiado el tamaño de la ventana
else if (eventHandler->type == SDL_WINDOWEVENT)
{
if (eventHandler->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
reloadTextures();
}
}
}
}
@@ -87,22 +103,7 @@ void Logo::checkInput()
{
if (input->checkInput(input_exit, REPEAT_FALSE))
{
section->name = SECTION_PROG_TITLE;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
section->name = SECTION_PROG_QUIT;
}
else if (input->checkAnyInput())
@@ -110,6 +111,9 @@ void Logo::checkInput()
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Gestiona el logo de JAILGAME
@@ -288,8 +292,9 @@ void Logo::run()
while (section->name == SECTION_PROG_LOGO)
{
checkInput();
update();
checkEvents();
checkEvents(); // Tiene que ir antes del render
render();
}
}

View File

@@ -58,6 +58,9 @@ private:
// Gestiona el color de las texturas
void updateTextureColors();
// Recarga todas las texturas
void reloadTextures();
public:
// Constructor
Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, param_t *param, section_t *section);

View File

@@ -243,25 +243,8 @@ void Title::checkInput()
postFade = 0;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_video_shaders, REPEAT_FALSE))
{
screen->switchShaders();
}
// Comprueba el input para el resto de objetos
screen->checkInput();
}
// Bucle para el titulo del juego
@@ -271,7 +254,7 @@ void Title::run()
{
checkInput();
update();
checkEvents();
checkEvents(); // Tiene que ir antes del render
render();
}
}