From 4910d201f962764d25a3d20140e3939dc11edd0e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 27 Mar 2026 23:19:47 +0100 Subject: [PATCH] primer comando implementat en la consola --- source/game/ui/console.cpp | 78 +++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/source/game/ui/console.cpp b/source/game/ui/console.cpp index db9fd8d..c3fb4d2 100644 --- a/source/game/ui/console.cpp +++ b/source/game/ui/console.cpp @@ -2,7 +2,10 @@ #include -#include // Para string +#include // Para toupper +#include // Para function +#include // Para string +#include // Para vector #include "core/rendering/screen.hpp" // Para Screen #include "core/rendering/sprite/sprite.hpp" // Para Sprite @@ -11,6 +14,62 @@ #include "core/resources/resource_cache.hpp" // Para Resource #include "game/options.hpp" // Para Options +// ── Sistema de comandos ──────────────────────────────────────────────────────── + +struct ConsoleCommand { + std::string_view keyword; + std::function& args)> execute; +}; + +// Convierte la entrada a uppercase y la divide en tokens por espacios +static auto parseTokens(const std::string& input) -> std::vector { + std::vector tokens; + std::string token; + for (unsigned char c : input) { + if (c == ' ') { + if (!token.empty()) { + tokens.push_back(token); + token.clear(); + } + } else { + token += static_cast(std::toupper(c)); + } + } + if (!token.empty()) { + tokens.push_back(token); + } + return tokens; +} + +// Tabla de comandos disponibles +static const std::vector COMMANDS = { + {"SS", [](const std::vector& args) -> std::string { + if (args.empty()) { + Screen::get()->toggleSupersampling(); + return std::string("Supersampling ") + (Options::video.supersampling ? "ON" : "OFF"); + } + if (args[0] == "ON") { + if (Options::video.supersampling) { return "Supersampling already ON"; } + Screen::get()->toggleSupersampling(); + return "Supersampling ON"; + } + if (args[0] == "OFF") { + if (!Options::video.supersampling) { return "Supersampling already OFF"; } + Screen::get()->toggleSupersampling(); + return "Supersampling OFF"; + } + return "Usage: SS [ON|OFF]"; + }}, + {"HELP", [](const std::vector&) -> std::string { + return "Commands: SS [ON|OFF]"; + }}, + {"?", [](const std::vector&) -> std::string { + return "Commands: SS [ON|OFF]"; + }}, +}; + +// ── Singleton ───────────────────────────────────────────────────────────────── + // [SINGLETON] Console* Console::console = nullptr; @@ -177,7 +236,22 @@ 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()) { - msg_line_ = "OK"; + const auto TOKENS = parseTokens(input_line_); + if (!TOKENS.empty()) { + const std::string& cmd = TOKENS[0]; + const std::vector ARGS(TOKENS.begin() + 1, TOKENS.end()); + bool found = false; + for (const auto& command : COMMANDS) { + if (command.keyword == cmd) { + msg_line_ = command.execute(ARGS); + found = true; + break; + } + } + if (!found) { + msg_line_ = "Unknown: " + cmd; + } + } } input_line_.clear(); cursor_timer_ = 0.0F;