From 3ba4293e8aebd6539c8935246b58565715397311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sun, 23 Feb 2025 09:53:06 +0100 Subject: [PATCH] Afegit globalEvents --- source/credits.cpp | 8 ++---- source/demo.cpp | 9 ++---- source/ending.cpp | 8 ++---- source/ending2.cpp | 8 ++---- source/game.cpp | 18 +++--------- source/game_over.cpp | 9 ++---- source/global_events.cpp | 24 ++++++++++++++++ source/global_events.h | 9 ++++++ source/global_inputs.cpp | 1 + source/global_inputs.h | 2 ++ source/loading_screen.cpp | 8 ++---- source/logo.cpp | 8 ++---- source/mouse.cpp | 33 +++++++++++++++++++++ source/mouse.h | 14 +++++++++ source/screen.cpp | 2 ++ source/title.cpp | 60 ++++++--------------------------------- 16 files changed, 111 insertions(+), 110 deletions(-) create mode 100644 source/global_events.cpp create mode 100644 source/global_events.h create mode 100644 source/mouse.cpp create mode 100644 source/mouse.h diff --git a/source/credits.cpp b/source/credits.cpp index 502cea0..bfda07a 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -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); } } diff --git a/source/demo.cpp b/source/demo.cpp index e883461..f653c15 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -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); } } diff --git a/source/ending.cpp b/source/ending.cpp index d54fe84..002c941 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -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); } } diff --git a/source/ending2.cpp b/source/ending2.cpp index 97e5a21..cfa7813 100644 --- a/source/ending2.cpp +++ b/source/ending2.cpp @@ -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); } } diff --git a/source/game.cpp b/source/game.cpp index 31c7a6e..c89caf0 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -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 } } diff --git a/source/game_over.cpp b/source/game_over.cpp index d93ffe4..8d02711 100644 --- a/source/game_over.cpp +++ b/source/game_over.cpp @@ -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); } } diff --git a/source/global_events.cpp b/source/global_events.cpp new file mode 100644 index 0000000..db8b5a4 --- /dev/null +++ b/source/global_events.cpp @@ -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); + } +} \ No newline at end of file diff --git a/source/global_events.h b/source/global_events.h new file mode 100644 index 0000000..8e0751f --- /dev/null +++ b/source/global_events.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace globalEvents +{ + // Comprueba los eventos que se pueden producir en cualquier sección del juego + void check(const SDL_Event &event); +} \ No newline at end of file diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp index 13a140b..6ebb932 100644 --- a/source/global_inputs.cpp +++ b/source/global_inputs.cpp @@ -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)) diff --git a/source/global_inputs.h b/source/global_inputs.h index 5047ad9..4bf63fb 100644 --- a/source/global_inputs.h +++ b/source/global_inputs.h @@ -1,3 +1,5 @@ +#pragma once + namespace globalInputs { // Comprueba los inputs que se pueden introducir en cualquier sección del juego diff --git a/source/loading_screen.cpp b/source/loading_screen.cpp index 518fef0..2871b1e 100644 --- a/source/loading_screen.cpp +++ b/source/loading_screen.cpp @@ -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); } } diff --git a/source/logo.cpp b/source/logo.cpp index 7eacabc..51bd65e 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -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); } } diff --git a/source/mouse.cpp b/source/mouse.cpp new file mode 100644 index 0000000..c853640 --- /dev/null +++ b/source/mouse.cpp @@ -0,0 +1,33 @@ +#include "mouse.h" +#include // Para SDL_ShowCursor +#include // 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; + } + } +} diff --git a/source/mouse.h b/source/mouse.h new file mode 100644 index 0000000..d99ea3c --- /dev/null +++ b/source/mouse.h @@ -0,0 +1,14 @@ +#pragma once + +#include // Para SDL_Event +#include // 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(); +} \ No newline at end of file diff --git a/source/screen.cpp b/source/screen.cpp index 894bf8d..02b9471 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -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(); } \ No newline at end of file diff --git a/source/title.cpp b/source/title.cpp index 4b9c3d1..8a96fcb 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -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,13 +106,8 @@ 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"));