primer comando implementat en la consola

This commit is contained in:
2026-03-27 23:19:47 +01:00
parent e85800c5ed
commit 4910d201f9

View File

@@ -2,7 +2,10 @@
#include <SDL3/SDL.h>
#include <string> // Para string
#include <cctype> // Para toupper
#include <functional> // Para function
#include <string> // Para string
#include <vector> // 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<std::string(const std::vector<std::string>& args)> execute;
};
// Convierte la entrada a uppercase y la divide en tokens por espacios
static auto parseTokens(const std::string& input) -> std::vector<std::string> {
std::vector<std::string> tokens;
std::string token;
for (unsigned char c : input) {
if (c == ' ') {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
} else {
token += static_cast<char>(std::toupper(c));
}
}
if (!token.empty()) {
tokens.push_back(token);
}
return tokens;
}
// Tabla de comandos disponibles
static const std::vector<ConsoleCommand> COMMANDS = {
{"SS", [](const std::vector<std::string>& 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>&) -> std::string {
return "Commands: SS [ON|OFF]";
}},
{"?", [](const std::vector<std::string>&) -> 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<std::string> 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;