Afegit globalEvents

This commit is contained in:
2025-02-23 09:53:06 +01:00
parent 8ae686a70b
commit 3ba4293e8a
16 changed files with 111 additions and 110 deletions

View File

@@ -15,6 +15,7 @@
#include "asset.h"
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
class Asset;
// Constructor
@@ -78,12 +79,7 @@ void Credits::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
}
}

View File

@@ -14,6 +14,7 @@
#include "options.h"
#include "debug.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
Demo::Demo()
@@ -75,13 +76,7 @@ void Demo::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
screen->setBorderColor(stringToColor(options.palette, "black"));
break;
}
globalEvents::check(event);
}
}

View File

@@ -18,6 +18,7 @@
#include "utils.h" // Para color_t, stringToColor, options_t
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
Ending::Ending()
@@ -161,12 +162,7 @@ void Ending::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
}
}

View File

@@ -15,6 +15,7 @@
#include "utils.h" // for color_t, stringToColor, options_t
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
Ending2::Ending2()
@@ -191,12 +192,7 @@ void Ending2::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
}
}

View File

@@ -23,6 +23,7 @@
#include "options.h"
#include "notifier.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
Game::Game()
@@ -131,24 +132,13 @@ void Game::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
screen_->setBorderColor(stringToColor(options.palette, "black"));
break;
}
if (event.type == SDL_RENDER_DEVICE_RESET || event.type == SDL_RENDER_TARGETS_RESET)
{
reLoadTextures();
}
globalEvents::check(event);
#ifdef DEBUG
if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
{
switch (event.key.keysym.scancode)
{
#ifdef DEBUG
case SDL_SCANCODE_G:
debug_->switchEnabled();
options.cheat.invincible = debug_->getEnabled();
@@ -191,11 +181,11 @@ void Game::checkEvents()
case SDL_SCANCODE_F9:
Notifier::get()->show("JAILDESIGNER IS LOGGED IN", "", 5);
break;
#endif
default:
break;
}
}
#endif
}
}

View File

@@ -13,6 +13,7 @@
#include "texture.h" // Para Texture
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
GameOver::GameOver()
@@ -127,13 +128,7 @@ void GameOver::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
options.section.subsection = 0;
break;
}
globalEvents::check(event);
}
}

24
source/global_events.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "global_events.h"
#include "options.h" // Para Options, options, OptionsGame, OptionsAudio
#include "mouse.h"
namespace globalEvents
{
// Comprueba los eventos que se pueden producir en cualquier sección del juego
void check(const SDL_Event &event)
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
return;
}
if (event.type == SDL_RENDER_DEVICE_RESET || event.type == SDL_RENDER_TARGETS_RESET)
{
//reLoadTextures();
}
Mouse::handleEvent(event);
}
}

9
source/global_events.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <SDL2/SDL_events.h>
namespace globalEvents
{
// Comprueba los eventos que se pueden producir en cualquier sección del juego
void check(const SDL_Event &event);
}

View File

@@ -69,6 +69,7 @@ namespace globalInputs
else if (Input::get()->checkInput(input_toggle_shaders, REPEAT_FALSE))
{
Screen::get()->toggleShaders();
Notifier::get()->show("HOLA");
}
else if (Input::get()->checkInput(input_toggle_palette, REPEAT_FALSE))

View File

@@ -1,3 +1,5 @@
#pragma once
namespace globalInputs
{
// Comprueba los inputs que se pueden introducir en cualquier sección del juego

View File

@@ -12,6 +12,7 @@
#include "utils.h" // for options_t, section_t, color_t, stringToC...
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
LoadingScreen::LoadingScreen()
@@ -81,12 +82,7 @@ void LoadingScreen::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
}
}

View File

@@ -12,6 +12,7 @@
#include "asset.h"
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
class Asset; // lines 11-11
// Constructor
@@ -76,12 +77,7 @@ void Logo::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
}
}

33
source/mouse.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "mouse.h"
#include <SDL2/SDL_mouse.h> // Para SDL_ShowCursor
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
namespace Mouse
{
Uint32 cursor_hide_time = 3000; // Tiempo en milisegundos para ocultar el cursor
Uint32 last_mouse_move_time = 0; // Última vez que el ratón se movió
bool cursor_visible = true; // Estado del cursor
void handleEvent(const SDL_Event &event)
{
if (event.type == SDL_MOUSEMOTION)
{
last_mouse_move_time = SDL_GetTicks();
if (!cursor_visible)
{
SDL_ShowCursor(SDL_ENABLE);
cursor_visible = true;
}
}
}
void updateCursorVisibility()
{
Uint32 current_time = SDL_GetTicks();
if (cursor_visible && (current_time - last_mouse_move_time > cursor_hide_time))
{
SDL_ShowCursor(SDL_DISABLE);
cursor_visible = false;
}
}
}

14
source/mouse.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <SDL2/SDL_events.h> // Para SDL_Event
#include <SDL2/SDL_stdinc.h> // Para Uint32
namespace Mouse
{
extern Uint32 cursor_hide_time; // Tiempo en milisegundos para ocultar el cursor
extern Uint32 last_mouse_move_time; // Última vez que el ratón se movió
extern bool cursor_visible; // Estado del cursor
void handleEvent(const SDL_Event &event);
void updateCursorVisibility();
}

View File

@@ -12,6 +12,7 @@
#include "jail_shader.h" // Para init, render
#include "notifier.h" // Para Notify
#include "options.h"
#include "mouse.h"
// [SINGLETON]
Screen *Screen::screen_ = nullptr;
@@ -489,4 +490,5 @@ void Screen::toggleShaders()
void Screen::update()
{
Notifier::get()->update();
Mouse::updateCursorVisibility();
}

View File

@@ -17,6 +17,7 @@
#include "utils.h" // Para color_t, stringToColor, options_t
#include "options.h"
#include "global_inputs.h"
#include "global_events.h"
// Constructor
Title::Title()
@@ -105,12 +106,7 @@ void Title::checkEvents()
SDL_Event event;
while (SDL_PollEvent(&event))
{
// Evento de salida de la aplicación
if (event.type == SDL_QUIT)
{
options.section.name = SECTION_QUIT;
break;
}
globalEvents::check(event);
// Solo se comprueban estas teclas si no está activo el menu de logros
if (event.type == SDL_KEYDOWN)
@@ -149,62 +145,22 @@ void Title::checkInput()
{
moveCheevosList(0);
}
}
if (input_->checkInput(input_exit, REPEAT_FALSE))
{
if (show_cheevos_)
else if (input_->checkInput(input_accept, REPEAT_FALSE))
{
hideCheevosList();
counter_ = 0;
}
else
{
options.section.name = SECTION_QUIT;
}
}
else if (input_->checkInput(input_toggle_border, REPEAT_FALSE))
{
screen_->toggleBorder();
resource_->reLoadTextures();
}
else if (input_->checkInput(input_toggle_videomode, REPEAT_FALSE))
{
screen_->toggleVideoMode();
resource_->reLoadTextures();
}
else if (input_->checkInput(input_toggle_shaders, REPEAT_FALSE))
{
screen_->toggleShaders();
}
else if (input_->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen_->decWindowSize();
resource_->reLoadTextures();
}
else if (input_->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen_->incWindowSize();
resource_->reLoadTextures();
}
else if (input_->checkInput(input_toggle_palette, REPEAT_FALSE))
{
switchPalette();
}
else if (input_->checkInput(input_accept, REPEAT_FALSE) || input_->checkInput(input_pause, REPEAT_FALSE))
if (input_->checkInput(input_accept, REPEAT_FALSE))
{
if (state_ == show_loading_screen)
{
state_ = fade_loading_screen;
}
}
globalInputs::check();
}
// Actualiza la marquesina
@@ -462,7 +418,7 @@ void Title::createCheevosTexture()
SDL_RenderClear(renderer_);
// Escribe la lista de logros en la textura
const std::string cheevosOwner = "LOCAL ACHIEVEMENTS";
const std::string cheevosOwner = "ACHIEVEMENTS";
const std::string cheevosListCaption = cheevosOwner + " (" + std::to_string(Cheevos::get()->unlocked()) + " / " + std::to_string(Cheevos::get()->count()) + ")";
int pos = 2;
info_text_->writeDX(TXT_CENTER | TXT_COLOR, cheevos_texture_->getWidth() / 2, pos, cheevosListCaption, 1, stringToColor(options.palette, "bright_green"));