From 7bad27d686742e889131b1d902864f5c4fc9f117 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 28 Mar 2026 00:02:14 +0100 Subject: [PATCH] canvi i reinici d'escene en la consola --- source/core/system/director.cpp | 13 ++++ source/game/scene_manager.hpp | 2 + source/game/ui/console.cpp | 113 +++++++++++++++++++++++++++----- source/game/ui/console.hpp | 13 +++- 4 files changed, 121 insertions(+), 20 deletions(-) diff --git a/source/core/system/director.cpp b/source/core/system/director.cpp index 490335e..cc7cb2f 100644 --- a/source/core/system/director.cpp +++ b/source/core/system/director.cpp @@ -358,6 +358,8 @@ void Director::runGame() { auto Director::run() -> int { // Bucle principal while (SceneManager::current != SceneManager::Scene::QUIT) { + const SceneManager::Scene ACTIVE = SceneManager::current; + switch (SceneManager::current) { case SceneManager::Scene::LOGO: runLogo(); @@ -395,9 +397,20 @@ auto Director::run() -> int { runEnding2(); break; + case SceneManager::Scene::RESTART_CURRENT: + // La escena salió por RESTART_CURRENT → relanzar la escena guardada + SceneManager::current = SceneManager::scene_before_restart; + break; + default: break; } + + // Si la escena que acaba de correr dejó RESTART_CURRENT pendiente, + // restaurar la escena que estaba activa para relanzarla en la próxima iteración + if (SceneManager::current == SceneManager::Scene::RESTART_CURRENT) { + SceneManager::current = ACTIVE; + } } return 0; diff --git a/source/game/scene_manager.hpp b/source/game/scene_manager.hpp index e5c0582..59ca8ea 100644 --- a/source/game/scene_manager.hpp +++ b/source/game/scene_manager.hpp @@ -20,6 +20,7 @@ namespace SceneManager { GAME_OVER, // Pantalla de game over ENDING, // Final del juego (ending 1) ENDING2, // Final del juego (ending 2) + RESTART_CURRENT, // Especial: reinicia la escena que estaba corriendo QUIT // Salir del programa }; @@ -40,5 +41,6 @@ namespace SceneManager { inline Scene current = Scene::LOGO; // Escena actual inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual #endif + inline Scene scene_before_restart = Scene::LOGO; // escena a relanzar tras RESTART_CURRENT } // namespace SceneManager diff --git a/source/game/ui/console.cpp b/source/game/ui/console.cpp index 3e2715e..84ae0c7 100644 --- a/source/game/ui/console.cpp +++ b/source/game/ui/console.cpp @@ -13,6 +13,7 @@ #include "core/rendering/text.hpp" // Para Text #include "core/resources/resource_cache.hpp" // Para Resource #include "game/options.hpp" // Para Options +#include "game/scene_manager.hpp" // Para SceneManager // ── Sistema de comandos ──────────────────────────────────────────────────────── @@ -62,18 +63,20 @@ static auto parseTokens(const std::string& input) -> std::vector { // Texto de ayuda común para HELP y ? static void printHelp() { SDL_Log("=== JDD CONSOLE COMMANDS ==="); - SDL_Log(" SS [ON|OFF] Supersampling (Ctrl+F4)"); - SDL_Log(" POSTFX [ON|OFF|NEXT] Post-FX effects / next preset (F4/Shift+F4)"); - SDL_Log(" BORDER [ON|OFF] Decorative border (B)"); - SDL_Log(" FULLSCREEN [ON|OFF] Fullscreen mode (F3)"); - SDL_Log(" ZOOM [UP|DOWN] Window zoom (F1/F2)"); - SDL_Log(" INTSCALE [ON|OFF] Integer scaling (F7)"); - SDL_Log(" VSYNC [ON|OFF] Vertical sync"); - SDL_Log(" PALETTE [NEXT|PREV] Color palette (F5/F6)"); + SDL_Log(" SS [ON|OFF] Supersampling (Ctrl+F4)"); + SDL_Log(" POSTFX [ON|OFF|NEXT] Post-FX / next preset (F4/Shift+F4)"); + SDL_Log(" BORDER [ON|OFF] Decorative border (B)"); + SDL_Log(" FULLSCREEN [ON|OFF] Fullscreen mode (F3)"); + SDL_Log(" ZOOM [UP|DOWN] Window zoom (F1/F2)"); + SDL_Log(" INTSCALE [ON|OFF] Integer scaling (F7)"); + SDL_Log(" VSYNC [ON|OFF] Vertical sync"); + SDL_Log(" PALETTE [NEXT|PREV] Color palette (F5/F6)"); #ifdef _DEBUG - SDL_Log(" DEBUG Toggle debug overlay (F12)"); + SDL_Log(" DEBUG Toggle debug overlay (F12)"); #endif - SDL_Log(" HELP/? Show this help in terminal"); + SDL_Log(" SCENE [LOGO|LOADING|TITLE|CREDITS|GAME|ENDING|ENDING2|RESTART]"); + SDL_Log(" EXIT / QUIT Quit application"); + SDL_Log(" HELP/? Show this help in terminal"); } // Tabla de comandos disponibles @@ -175,6 +178,48 @@ static const std::vector COMMANDS = { }}, #endif + // SCENE [LOGO|LOADING|TITLE|CREDITS|GAME|ENDING|ENDING2|RESTART] — Cambiar o reiniciar escena + {.keyword = "SCENE", .execute = [](const std::vector& args) -> std::string { + if (args.empty()) { return "Usage: SCENE [LOGO|LOADING|TITLE|CREDITS|GAME|ENDING|ENDING2|RESTART]"; } + + // RESTART: reinicia la escena actual (funciona desde cualquier escena) + if (args[0] == "RESTART") { + SceneManager::scene_before_restart = SceneManager::current; + SceneManager::current = SceneManager::Scene::RESTART_CURRENT; + return "Restarting..."; + } + + // Para el resto: si pedimos la escena que ya está activa → también reiniciar + const auto GO_TO = [](SceneManager::Scene target, const std::string& label) -> std::string { + if (SceneManager::current == target) { + SceneManager::scene_before_restart = target; + SceneManager::current = SceneManager::Scene::RESTART_CURRENT; + } else { + SceneManager::current = target; + } + return "Scene: " + label; + }; + + if (args[0] == "LOGO") { return GO_TO(SceneManager::Scene::LOGO, "Logo"); } + if (args[0] == "LOADING") { return GO_TO(SceneManager::Scene::LOADING_SCREEN, "Loading"); } + if (args[0] == "TITLE") { return GO_TO(SceneManager::Scene::TITLE, "Title"); } + if (args[0] == "CREDITS") { return GO_TO(SceneManager::Scene::CREDITS, "Credits"); } + if (args[0] == "GAME") { return GO_TO(SceneManager::Scene::GAME, "Game"); } + if (args[0] == "ENDING") { return GO_TO(SceneManager::Scene::ENDING, "Ending"); } + if (args[0] == "ENDING2") { return GO_TO(SceneManager::Scene::ENDING2, "Ending 2"); } + return "Unknown scene: " + args[0]; + }}, + + // EXIT / QUIT — Cerrar la aplicacion + {.keyword = "EXIT", .execute = [](const std::vector&) -> std::string { + SceneManager::current = SceneManager::Scene::QUIT; + return "Quitting..."; + }}, + {.keyword = "QUIT", .execute = [](const std::vector&) -> std::string { + SceneManager::current = SceneManager::Scene::QUIT; + return "Quitting..."; + }}, + // HELP / ? — Muestra ayuda en la terminal del sistema {.keyword = "HELP", .execute = [](const std::vector&) -> std::string { printHelp(); return "Help printed to terminal"; @@ -321,8 +366,10 @@ void Console::toggle() { SDL_StartTextInput(SDL_GetKeyboardFocus()); break; case Status::ACTIVE: - status_ = Status::VANISHING; - msg_line_ = std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION); + status_ = Status::VANISHING; + msg_line_ = std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION); + history_index_ = -1; + saved_input_.clear(); SDL_StopTextInput(SDL_GetKeyboardFocus()); break; default: @@ -343,11 +390,33 @@ void Console::handleEvent(const SDL_Event& event) { } if (event.type == SDL_EVENT_KEY_DOWN) { - if (event.key.scancode == SDL_SCANCODE_BACKSPACE && !input_line_.empty()) { - input_line_.pop_back(); - } else if (event.key.scancode == SDL_SCANCODE_RETURN || - event.key.scancode == SDL_SCANCODE_KP_ENTER) { - processCommand(); + switch (event.key.scancode) { + case SDL_SCANCODE_BACKSPACE: + if (!input_line_.empty()) { input_line_.pop_back(); } + break; + case SDL_SCANCODE_RETURN: + case SDL_SCANCODE_KP_ENTER: + processCommand(); + break; + case SDL_SCANCODE_UP: + // Navegar hacia atrás en el historial + if (history_index_ < static_cast(history_.size()) - 1) { + if (history_index_ == -1) { saved_input_ = input_line_; } + ++history_index_; + input_line_ = history_[static_cast(history_index_)]; + } + break; + case SDL_SCANCODE_DOWN: + // Navegar hacia el presente en el historial + if (history_index_ >= 0) { + --history_index_; + input_line_ = (history_index_ == -1) + ? saved_input_ + : history_[static_cast(history_index_)]; + } + break; + default: + break; } } } @@ -355,6 +424,14 @@ void Console::handleEvent(const SDL_Event& event) { // Ejecuta el comando introducido y reinicia la línea de input void Console::processCommand() { if (!input_line_.empty()) { + // Añadir al historial (sin duplicados consecutivos) + if (history_.empty() || history_.front() != input_line_) { + history_.push_front(input_line_); + if (static_cast(history_.size()) > MAX_HISTORY_SIZE) { + history_.pop_back(); + } + } + const auto TOKENS = parseTokens(input_line_); if (!TOKENS.empty()) { const std::string& cmd = TOKENS[0]; @@ -376,6 +453,8 @@ void Console::processCommand() { } } input_line_.clear(); + history_index_ = -1; + saved_input_.clear(); cursor_timer_ = 0.0F; cursor_visible_ = true; } diff --git a/source/game/ui/console.hpp b/source/game/ui/console.hpp index d196df1..60cd942 100644 --- a/source/game/ui/console.hpp +++ b/source/game/ui/console.hpp @@ -2,6 +2,7 @@ #include +#include // Para deque (historial) #include // Para shared_ptr #include // Para string @@ -43,9 +44,10 @@ class Console { // Constantes de consola static constexpr std::string_view CONSOLE_NAME = "JDD Console"; static constexpr std::string_view CONSOLE_VERSION = "v1.0"; - static constexpr int MAX_LINE_CHARS = 28; - static constexpr float CURSOR_ON_TIME = 0.5F; - static constexpr float CURSOR_OFF_TIME = 0.3F; + static constexpr int MAX_LINE_CHARS = 28; + static constexpr int MAX_HISTORY_SIZE = 20; + static constexpr float CURSOR_ON_TIME = 0.5F; + static constexpr float CURSOR_OFF_TIME = 0.3F; // [SINGLETON] static Console* console; @@ -74,4 +76,9 @@ class Console { std::string input_line_; float cursor_timer_{0.0F}; bool cursor_visible_{true}; + + // Historial de comandos (navegable con flechas arriba/abajo) + std::deque history_; + int history_index_{-1}; // -1 = en la entrada actual (presente) + std::string saved_input_; // guarda input_line_ al empezar a navegar };