millorada una mica la classe Debug en quant a mostrar info

This commit is contained in:
2026-03-28 21:58:54 +01:00
parent a21f530dd4
commit 9282d661aa
6 changed files with 79 additions and 50 deletions

View File

@@ -36,24 +36,51 @@ void Debug::render() { // NOLINT(readability-make-member-function-const)
auto text = Resource::Cache::get()->getText("aseprite"); auto text = Resource::Cache::get()->getText("aseprite");
int y = y_; int y = y_;
int w = 0; int w = 0;
constexpr int DESP_Y = 7;
const int CHAR_SIZE = text->getCharacterSize();
// Watch window: valores persistentes (key: value)
for (const auto& [key, value] : watches_) {
const std::string LINE = key + ": " + value;
text->write(x_, y, LINE);
w = std::max(w, text->length(LINE));
y += DESP_Y;
if (y > 192 - CHAR_SIZE) {
y = y_;
x_ += w + 2;
w = 0;
}
}
// Slot one-shot: mensajes de un solo frame
for (const auto& s : slot_) { for (const auto& s : slot_) {
text->write(x_, y, s); text->write(x_, y, s);
w = (std::max(w, (int)s.length())); w = std::max(w, text->length(s));
y += text->getCharacterSize() + 1; y += DESP_Y;
if (y > 192 - text->getCharacterSize()) { if (y > 192 - CHAR_SIZE) {
y = y_; y = y_;
x_ += (w * text->getCharacterSize()) + 2; x_ += w + 2;
w = 0;
} }
} }
y = 0; y = 0;
for (const auto& l : log_) { for (const auto& l : log_) {
text->writeColored(x_ + 10, y, l, static_cast<Uint8>(PaletteColor::WHITE)); text->writeColored(x_ + 10, y, l, static_cast<Uint8>(PaletteColor::WHITE));
y += text->getCharacterSize() + 1; y += CHAR_SIZE + 1;
} }
} }
// Establece/actualiza un valor persistente en el watch window
void Debug::set(const std::string& key, const std::string& value) {
watches_[key] = value;
}
// Elimina un valor del watch window
void Debug::unset(const std::string& key) {
watches_.erase(key);
}
// Establece la posición donde se colocará la información de debug // Establece la posición donde se colocará la información de debug
void Debug::setPos(SDL_FPoint p) { void Debug::setPos(SDL_FPoint p) {
x_ = p.x; x_ = p.x;

View File

@@ -4,6 +4,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <map> // Para map
#include <string> // Para string #include <string> // Para string
#include <vector> // Para vector #include <vector> // Para vector
@@ -27,10 +28,13 @@ class Debug {
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Obtiene si el debug está activo [[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Obtiene si el debug está activo
void add(const std::string& text) { slot_.push_back(text); } // Añade texto al slot de debug void add(const std::string& text) { slot_.push_back(text); } // Añade texto one-shot al slot (se limpia cada frame)
void clear() { slot_.clear(); } // Limpia el slot de debug void clear() { slot_.clear(); } // Limpia el slot one-shot (no afecta a watches)
void addToLog(const std::string& text) { log_.push_back(text); } // Añade texto al log void addToLog(const std::string& text) { log_.push_back(text); } // Añade texto al log
void clearLog() { log_.clear(); } // Limpia el log void clearLog() { log_.clear(); } // Limpia el log
void set(const std::string& key, const std::string& value); // Establece/actualiza un valor persistente en el watch window
void unset(const std::string& key); // Elimina un valor del watch window
void clearWatches() { watches_.clear(); } // Limpia todos los watches
void setEnabled(bool value) { enabled_ = value; } // Establece si el debug está activo void setEnabled(bool value) { enabled_ = value; } // Establece si el debug está activo
void toggleEnabled() { enabled_ = !enabled_; } // Alterna el estado del debug void toggleEnabled() { enabled_ = !enabled_; } // Alterna el estado del debug
@@ -47,8 +51,9 @@ class Debug {
~Debug() = default; // Destructor ~Debug() = default; // Destructor
// Variables // Variables
std::vector<std::string> slot_; // Vector con los textos a escribir std::map<std::string, std::string> watches_; // Watch window: valores persistentes (key→value)
std::vector<std::string> log_; // Vector con los textos a escribir std::vector<std::string> slot_; // One-shot: textos que se limpian cada frame
std::vector<std::string> log_; // Log persistente
int x_ = 0; // Posicion donde escribir el texto de debug int x_ = 0; // Posicion donde escribir el texto de debug
int y_ = 0; // Posición donde escribir el texto de debug int y_ = 0; // Posición donde escribir el texto de debug
bool enabled_ = false; // Indica si esta activo el modo debug bool enabled_ = false; // Indica si esta activo el modo debug

View File

@@ -84,21 +84,21 @@ void Player::move(float delta_time) {
} }
syncSpriteAndCollider(); // Actualiza la posición del sprite y las colisiones syncSpriteAndCollider(); // Actualiza la posición del sprite y las colisiones
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->add(std::string("X : " + std::to_string(static_cast<int>(x_)))); Debug::get()->set("P.X", std::to_string(static_cast<int>(x_)));
Debug::get()->add(std::string("Y : " + std::to_string(static_cast<int>(y_)))); Debug::get()->set("P.Y", std::to_string(static_cast<int>(y_)));
Debug::get()->add(std::string("LGP: " + std::to_string(last_grounded_position_))); Debug::get()->set("P.LGP", std::to_string(last_grounded_position_));
switch (state_) { switch (state_) {
case State::ON_GROUND: case State::ON_GROUND:
Debug::get()->add(std::string("ON_GROUND")); Debug::get()->set("P.STATE", "ON_GROUND");
break; break;
case State::ON_SLOPE: case State::ON_SLOPE:
Debug::get()->add(std::string("ON_SLOPE")); Debug::get()->set("P.STATE", "ON_SLOPE");
break; break;
case State::JUMPING: case State::JUMPING:
Debug::get()->add(std::string("JUMPING")); Debug::get()->set("P.STATE", "JUMPING");
break; break;
case State::FALLING: case State::FALLING:
Debug::get()->add(std::string("FALLING")); Debug::get()->set("P.STATE", "FALLING");
break; break;
} }
#endif #endif
@@ -235,6 +235,9 @@ void Player::moveOnGround(float delta_time) {
y_ = SLOPE_Y - HEIGHT; y_ = SLOPE_Y - HEIGHT;
transitionToState(State::ON_SLOPE); transitionToState(State::ON_SLOPE);
} }
#ifdef _DEBUG
Debug::get()->set("sl.detect_y", SLOPE_Y != Collision::NONE ? std::to_string(SLOPE_Y) : "-");
#endif
// Comprueba si está sobre una rampa // Comprueba si está sobre una rampa
if (isOnSlope()) { transitionToState(State::ON_SLOPE); } if (isOnSlope()) { transitionToState(State::ON_SLOPE); }
@@ -279,12 +282,21 @@ void Player::moveOnSlope(float delta_time) {
const int MAX_X = std::max(current_slope_->x1, current_slope_->x2); const int MAX_X = std::max(current_slope_->x1, current_slope_->x2);
const bool OUT_OF_BOUNDS = (X < MIN_X) || (X > MAX_X); const bool OUT_OF_BOUNDS = (X < MIN_X) || (X > MAX_X);
#ifdef _DEBUG
Debug::get()->set("sl.foot", std::to_string(X));
Debug::get()->set("sl.y_c", std::to_string(static_cast<int>(y_)));
Debug::get()->set("sl.oob", OUT_OF_BOUNDS ? "YES" : "ok");
#endif
if (OUT_OF_BOUNDS) { if (OUT_OF_BOUNDS) {
// Determinar si estamos saliendo por arriba o por abajo de la rampa // Determinar si estamos saliendo por arriba o por abajo de la rampa
const bool EXITING_DOWNWARD = (X > current_slope_->x2 && IS_LEFT_SLOPE) || const bool EXITING_DOWNWARD = (X > current_slope_->x2 && IS_LEFT_SLOPE) ||
(X < current_slope_->x1 && !IS_LEFT_SLOPE); (X < current_slope_->x1 && !IS_LEFT_SLOPE);
const bool EXITING_UPWARD = (X < current_slope_->x1 && IS_LEFT_SLOPE) || const bool EXITING_UPWARD = (X < current_slope_->x1 && IS_LEFT_SLOPE) ||
(X > current_slope_->x2 && !IS_LEFT_SLOPE); (X > current_slope_->x2 && !IS_LEFT_SLOPE);
#ifdef _DEBUG
Debug::get()->set("sl.oob", EXITING_DOWNWARD ? "DOWN" : "UP");
#endif
if (EXITING_DOWNWARD) { if (EXITING_DOWNWARD) {
// Salida por abajo: no hacer nada // Salida por abajo: no hacer nada
@@ -576,18 +588,17 @@ void Player::updateCurrentSlope() {
} }
} }
// Debug output #ifdef _DEBUG
/*
if (current_slope_ != nullptr) { if (current_slope_ != nullptr) {
const char* TYPE = isLeftSlope() ? "Left \\" : "Right /"; Debug::get()->set("sl.type", isLeftSlope() ? "L\\" : "R/");
std::cout << "[SLOPE] " << TYPE Debug::get()->set("sl.p1", std::to_string(current_slope_->x1) + "," + std::to_string(current_slope_->y1));
<< " from (" << current_slope_->x1 << "," << current_slope_->y1 << ")" Debug::get()->set("sl.p2", std::to_string(current_slope_->x2) + "," + std::to_string(current_slope_->y2));
<< " to (" << current_slope_->x2 << "," << current_slope_->y2 << ")\n";
} else { } else {
std::cout << "[SLOPE] nullptr\n"; Debug::get()->set("sl.type", "null");
Debug::get()->unset("sl.p1");
Debug::get()->unset("sl.p2");
} }
*/ #endif
} }
// Comprueba que el jugador no toque ningun tile de los que matan // Comprueba que el jugador no toque ningun tile de los que matan

View File

@@ -79,25 +79,25 @@ auto CollisionMap::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
// Calcula la base del tile // Calcula la base del tile
int base = ((p.y / TILE_SIZE) * TILE_SIZE) + TILE_SIZE; int base = ((p.y / TILE_SIZE) * TILE_SIZE) + TILE_SIZE;
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->add("BASE = " + std::to_string(base)); Debug::get()->set("slope.BASE", std::to_string(base));
#endif #endif
// Calcula cuanto se ha entrado en el tile horizontalmente // Calcula cuanto se ha entrado en el tile horizontalmente
const int POS = (static_cast<int>(p.x) % TILE_SIZE); // Esto da un valor entre 0 y 7 const int POS = (static_cast<int>(p.x) % TILE_SIZE); // Esto da un valor entre 0 y 7
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->add("POS = " + std::to_string(POS)); Debug::get()->set("slope.POS", std::to_string(POS));
#endif #endif
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa // Se resta a la base la cantidad de pixeles pos en funcion de la rampa
if (slope == Tile::SLOPE_R) { if (slope == Tile::SLOPE_R) {
base -= POS + 1; base -= POS + 1;
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->add("BASE_R = " + std::to_string(base)); Debug::get()->set("slope.result", "BASE_R=" + std::to_string(base));
#endif #endif
} else { } else {
base -= (TILE_SIZE - POS); base -= (TILE_SIZE - POS);
#ifdef _DEBUG #ifdef _DEBUG
Debug::get()->add("BASE_L = " + std::to_string(base)); Debug::get()->set("slope.result", "BASE_L=" + std::to_string(base));
#endif #endif
} }

View File

@@ -207,10 +207,6 @@ void Game::update() {
Audio::update(); // Actualiza el objeto Audio Audio::update(); // Actualiza el objeto Audio
Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen Screen::get()->update(DELTA_TIME); // Actualiza el objeto Screen
#ifdef _DEBUG
updateDebugInfo();
#endif
} }
// Actualiza el juego en estado PLAYING // Actualiza el juego en estado PLAYING
@@ -432,18 +428,9 @@ static void toggleCheat(Options::Cheat::State& cheat, const std::string& label)
Notifier::get()->show({label + (ENABLED ? Locale::get()->get("game.enabled") : Locale::get()->get("game.disabled"))}, Notifier::Style::DEFAULT, -1, true); // NOLINT(readability-static-accessed-through-instance) Notifier::get()->show({label + (ENABLED ? Locale::get()->get("game.enabled") : Locale::get()->get("game.disabled"))}, Notifier::Style::DEFAULT, -1, true); // NOLINT(readability-static-accessed-through-instance)
} }
// Pasa la información de debug
void Game::updateDebugInfo() {
// Debug::get()->add("X = " + std::to_string(static_cast<int>(player_->x_)) + ", Y = " + std::to_string(static_cast<int>(player_->y_)));
// Debug::get()->add("VX = " + std::to_string(player_->vx_).substr(0, 4) + ", VY = " + std::to_string(player_->vy_).substr(0, 4));
// Debug::get()->add("STATE = " + std::to_string(static_cast<int>(player_->state_)));
}
// Pone la información de debug en pantalla // Pone la información de debug en pantalla
void Game::renderDebugInfo() { void Game::renderDebugInfo() {
if (!Debug::get()->isEnabled()) { if (!Debug::get()->isEnabled()) { return; }
return;
}
auto surface = Screen::get()->getRendererSurface(); auto surface = Screen::get()->getRendererSurface();
@@ -589,8 +576,8 @@ void Game::handleDebugMouseDrag(float delta_time) {
} }
debug_dragging_player_ = true; debug_dragging_player_ = true;
Debug::get()->add(std::string("X : " + std::to_string(static_cast<int>(player_rect.x)))); Debug::get()->set("drag.x", std::to_string(static_cast<int>(player_rect.x)));
Debug::get()->add(std::string("Y : " + std::to_string(static_cast<int>(player_rect.y)))); Debug::get()->set("drag.y", std::to_string(static_cast<int>(player_rect.y)));
} else if (debug_dragging_player_) { } else if (debug_dragging_player_) {
// Botón soltado después de arrastrar: finalizar teleport // Botón soltado después de arrastrar: finalizar teleport
player_->finalizeDebugTeleport(); player_->finalizeDebugTeleport();

View File

@@ -92,7 +92,6 @@ class Game {
void demoInit(); // DEMO MODE: Inicializa las variables para el modo demo void demoInit(); // DEMO MODE: Inicializa las variables para el modo demo
void demoCheckRoomChange(float delta_time); // DEMO MODE: Comprueba si se ha de cambiar de habitación void demoCheckRoomChange(float delta_time); // DEMO MODE: Comprueba si se ha de cambiar de habitación
#ifdef _DEBUG #ifdef _DEBUG
void updateDebugInfo(); // Pone la información de debug en pantalla
static void renderDebugInfo(); // Pone la información de debug en pantalla static void renderDebugInfo(); // Pone la información de debug en pantalla
void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos
void handleDebugMouseDrag(float delta_time); // Maneja el arrastre del jugador con el ratón (debug) void handleDebugMouseDrag(float delta_time); // Maneja el arrastre del jugador con el ratón (debug)