From b586a117c8d5179ef395d15678bfe90d771a82be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Fri, 3 Jan 2025 23:35:48 +0100 Subject: [PATCH] =?UTF-8?q?Afegit=20mouse.cpp=20per=20amagar=20el=20cursor?= =?UTF-8?q?=20quan=20no=20est=C3=A0=20moventse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/credits.cpp | 14 +++++++++----- source/game.cpp | 32 +++++++++++++++++++++++++------- source/hiscore_table.cpp | 8 ++++++-- source/instructions.cpp | 8 ++++++-- source/intro.cpp | 8 +++++++- source/logo.cpp | 6 +++++- source/mouse.cpp | 32 ++++++++++++++++++++++++++++++++ source/mouse.h | 12 ++++++++++++ source/screen.cpp | 2 ++ source/title.cpp | 6 +++++- 10 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 source/mouse.cpp create mode 100644 source/mouse.h diff --git a/source/credits.cpp b/source/credits.cpp index 8251af3..bac7f69 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -22,6 +22,7 @@ #include "player.h" #include "fade.h" #include "lang.h" +#include "mouse.h" // Textos constexpr const char TEXT_COPYRIGHT[] = "@2020,2025 JailDesigner"; @@ -39,20 +40,20 @@ Credits::Credits() throw std::runtime_error("Failed to create SDL texture for text."); } section::name = section::Name::CREDITS; - //top_black_rect_ = {play_area_.x, 0, play_area_.w, black_bars_size_}; - //bottom_black_rect_ = {play_area_.x, param.game.game_area.rect.h - black_bars_size_, play_area_.w, black_bars_size_}; + // top_black_rect_ = {play_area_.x, 0, play_area_.w, black_bars_size_}; + // bottom_black_rect_ = {play_area_.x, param.game.game_area.rect.h - black_bars_size_, play_area_.w, black_bars_size_}; balloon_manager_->setPlayArea(play_area_); - + fade_in_->setColor(fade_color.r, fade_color.g, fade_color.b); fade_in_->setType(FadeType::FULLSCREEN); fade_in_->setPost(50); fade_in_->setMode(FadeMode::IN); fade_in_->activate(); - + fade_out_->setColor(0, 0, 0); fade_out_->setType(FadeType::FULLSCREEN); fade_out_->setPost(400); - + initPlayers(); SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND); fillTextTexture(); @@ -158,6 +159,9 @@ void Credits::checkEvents() section::options = section::Options::QUIT_FROM_EVENT; break; } + + // Comprueba el cursor + Mouse::handleEvent(event); } } diff --git a/source/game.cpp b/source/game.cpp index 7e011d8..84b7e2b 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -35,7 +35,7 @@ #include "tabe.h" // Para Tabe #include "text.h" // Para Text #include "texture.h" // Para Texture -#include +#include "mouse.h" struct JA_Sound_t; // lines 37-37 // Constructor @@ -1270,6 +1270,8 @@ void Game::checkEvents() } } #endif + // Comprueba el cursor + Mouse::handleEvent(event); } } @@ -1625,11 +1627,16 @@ void Game::initDemo(int player_id) { if (demo_.enabled) { + // Cambia el estado del juego + state_ = GameState::PLAYING; + // Aleatoriza la asignación del fichero con los datos del modo demostracion - const auto demo1 = rand() % 2; - const auto demo2 = (demo1 == 0) ? 1 : 0; - demo_.data.emplace_back(Resource::get()->getDemoData(demo1)); - demo_.data.emplace_back(Resource::get()->getDemoData(demo2)); + { + const auto demo1 = rand() % 2; + const auto demo2 = (demo1 == 0) ? 1 : 0; + demo_.data.emplace_back(Resource::get()->getDemoData(demo1)); + demo_.data.emplace_back(Resource::get()->getDemoData(demo2)); + } // Selecciona una pantalla al azar { @@ -1645,8 +1652,13 @@ void Game::initDemo(int player_id) Stage::total_power += Stage::get(i).power_to_complete; } + // Pone al jugador en estado jugar + { + getPlayer(player_id)->setPlayingState(PlayerState::PLAYING); + } + // Activa o no al otro jugador - if (rand() % 2 == 0) + if (rand() % 3 != 0) { const auto other_player_id = player_id == 1 ? 2 : 1; auto other_player = getPlayer(other_player_id); @@ -1804,9 +1816,15 @@ void Game::updateDemo() { if (demo_.enabled) { + // Actualiza ambos fades + fade_in_->update(); + fade_out_->update(); + // Incrementa el contador de la demo if (demo_.counter < TOTAL_DEMO_DATA) + { demo_.counter++; + } // Activa el fundido antes de acabar con los datos de la demo if (demo_.counter == TOTAL_DEMO_DATA - 200) @@ -1871,7 +1889,7 @@ void Game::updateGameStateEnteringPlayer() { state_ = GameState::SHOWING_GET_READY_MESSAGE; createMessage({paths_.at(0), paths_.at(1)}, Resource::get()->getTexture("game_text_get_ready")); - //JA_PlaySound(Resource::get()->getSound("voice_get_ready.wav")); + JA_PlaySound(Resource::get()->getSound("voice_get_ready.wav")); } } } diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index d849fb6..fd1df20 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -20,6 +20,7 @@ #include "section.h" // Para Name, name, Options, options #include "text.h" // Para Text, TEXT_CENTER, TEXT_SHADOW #include "utils.h" // Para Color, Zone, fade_color, orange_color +#include "mouse.h" // Constructor HiScoreTable::HiScoreTable() @@ -78,7 +79,7 @@ void HiScoreTable::update() Screen::get()->update(); // Actualiza las variables de globalInputs - globalInputs::update(); + globalInputs::update(); // Actualiza el fondo background_->update(); @@ -196,6 +197,9 @@ void HiScoreTable::checkEvents() reloadTextures(); } } + + // Comprueba el cursor + Mouse::handleEvent(event); } } @@ -205,7 +209,7 @@ void HiScoreTable::checkInput() // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) if (Input::get()->checkAnyButtonPressed()) { - //JA_StopMusic(); + // JA_StopMusic(); section::name = section::Name::TITLE; section::options = section::Options::TITLE_1; section::attract_mode = section::AttractMode::TITLE_TO_DEMO; diff --git a/source/instructions.cpp b/source/instructions.cpp index ccd2a24..6be937d 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -20,6 +20,7 @@ #include "texture.h" // Para Texture #include "tiled_bg.h" // Para TiledBG, TiledBGMode #include "utils.h" // Para Color, shdw_txt_color, Zone, no_color +#include "mouse.h" // Constructor Instructions::Instructions() @@ -217,7 +218,7 @@ void Instructions::update() Screen::get()->update(); // Actualiza las variables de globalInputs - globalInputs::update(); + globalInputs::update(); // Incrementa el contador counter_++; @@ -301,6 +302,9 @@ void Instructions::checkEvents() reloadTextures(); } } + + // Comprueba el cursor + Mouse::handleEvent(event); } } @@ -310,7 +314,7 @@ void Instructions::checkInput() // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) if (Input::get()->checkAnyButtonPressed()) { - //JA_StopMusic(); + // JA_StopMusic(); section::name = section::Name::TITLE; section::options = section::Options::TITLE_1; section::attract_mode = section::AttractMode::TITLE_TO_DEMO; diff --git a/source/intro.cpp b/source/intro.cpp index 2911599..2ca8b37 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -16,6 +16,7 @@ #include "texture.h" // Para Texture #include "utils.h" // Para Zone, BLOCK, Color, bg_color #include "writer.h" // Para Writer +#include "mouse.h" // Constructor Intro::Intro() @@ -169,13 +170,18 @@ void Intro::checkEvents() case SDL_WINDOWEVENT: { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + { reloadTextures(); + } break; } default: break; } + + // Comprueba el cursor + Mouse::handleEvent(event); } } @@ -384,7 +390,7 @@ void Intro::update() updateScenes(); // Actualiza las variables de globalInputs - globalInputs::update(); + globalInputs::update(); } } diff --git a/source/logo.cpp b/source/logo.cpp index 2ce04ae..5346dc4 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -13,6 +13,7 @@ #include "sprite.h" // Para Sprite #include "texture.h" // Para Texture #include "utils.h" // Para Color, Zone +#include "mouse.h" // Constructor Logo::Logo() @@ -92,6 +93,9 @@ void Logo::checkEvents() reloadTextures(); } } + + // Comprueba el cursor + Mouse::handleEvent(event); } } @@ -202,7 +206,7 @@ void Logo::update() } // Actualiza las variables de globalInputs - globalInputs::update(); + globalInputs::update(); } } diff --git a/source/mouse.cpp b/source/mouse.cpp new file mode 100644 index 0000000..54f9b46 --- /dev/null +++ b/source/mouse.cpp @@ -0,0 +1,32 @@ +#include "mouse.h" +#include + +namespace Mouse +{ + Uint32 cursorHideTime = 3000; // Tiempo en milisegundos para ocultar el cursor + Uint32 lastMouseMoveTime = 0; // Última vez que el ratón se movió + bool cursorVisible = true; // Estado del cursor + + void handleEvent(const SDL_Event &event) + { + if (event.type == SDL_MOUSEMOTION) + { + lastMouseMoveTime = SDL_GetTicks(); + if (!cursorVisible) + { + SDL_ShowCursor(SDL_ENABLE); + cursorVisible = true; + } + } + } + + void updateCursorVisibility() + { + Uint32 currentTime = SDL_GetTicks(); + if (cursorVisible && (currentTime - lastMouseMoveTime > cursorHideTime)) + { + SDL_ShowCursor(SDL_DISABLE); + cursorVisible = false; + } + } +} diff --git a/source/mouse.h b/source/mouse.h new file mode 100644 index 0000000..c5f2b70 --- /dev/null +++ b/source/mouse.h @@ -0,0 +1,12 @@ +#pragma once +#include + +namespace Mouse +{ + extern Uint32 cursorHideTime; // Tiempo en milisegundos para ocultar el cursor + extern Uint32 lastMouseMoveTime; // Última vez que el ratón se movió + extern bool cursorVisible; // 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 e55fb3f..f06515a 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -14,6 +14,7 @@ #include "notifier.h" // Para Notifier #include "on_screen_help.h" // Para OnScreenHelp #include "options.h" // Para Options, OptionsVideo, options, Options... +#include "mouse.h" #ifndef NO_SHADERS #include "jail_shader.h" // para init, render @@ -259,6 +260,7 @@ void Screen::update() Notifier::get()->update(); updateFPS(); OnScreenHelp::get()->update(); + Mouse::updateCursorVisibility(); } // Agita la pantalla diff --git a/source/title.cpp b/source/title.cpp index 153984a..5c06fef 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -24,6 +24,7 @@ #include "texture.h" // Para Texture #include "tiled_bg.h" // Para TiledBG, TiledBGMode #include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK +#include "mouse.h" // Constructor Title::Title() @@ -134,7 +135,7 @@ void Title::update() // Actualiza el mosaico de fondo tiled_bg_->update(); - + if (counter_ == 100) { fade_->activate(); @@ -270,6 +271,9 @@ void Title::checkEvents() break; } } + + // Comprueba el cursor + Mouse::handleEvent(event); } } }