treballant en la consola

This commit is contained in:
2026-03-27 22:24:55 +01:00
parent 3712f0c8d9
commit f25ee18329
8 changed files with 218 additions and 1 deletions

128
source/game/ui/console.cpp Normal file
View File

@@ -0,0 +1,128 @@
#include "game/ui/console.hpp"
#include <SDL3/SDL.h>
#include <string> // Para string
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/sprite/sprite.hpp" // Para Sprite
#include "core/rendering/surface.hpp" // Para Surface
#include "core/rendering/text.hpp" // Para Text
#include "core/resources/resource_cache.hpp" // Para Resource
#include "game/options.hpp" // Para Options
// [SINGLETON]
Console* Console::console = nullptr;
// [SINGLETON]
void Console::init(const std::string& font_name) {
Console::console = new Console(font_name);
}
// [SINGLETON]
void Console::destroy() {
delete Console::console;
Console::console = nullptr;
}
// [SINGLETON]
auto Console::get() -> Console* {
return Console::console;
}
// Constructor
Console::Console(const std::string& font_name)
: text_(Resource::Cache::get()->getText(font_name)) {
const int TEXT_SIZE = 6;
const int PADDING_IN_V = TEXT_SIZE / 2;
height_ = static_cast<float>((TEXT_SIZE * 2) + (PADDING_IN_V * 2));
y_ = -height_;
buildSurface();
}
// Crea la Surface con el aspecto visual de la consola
void Console::buildSurface() {
const float WIDTH = Options::game.width;
surface_ = std::make_shared<Surface>(WIDTH, height_);
auto previous_renderer = Screen::get()->getRendererSurface();
Screen::get()->setRendererSurface(surface_);
// Fondo y borde
surface_->clear(BG_COLOR);
SDL_FRect rect = {.x = 0, .y = 0, .w = WIDTH, .h = height_};
surface_->drawRectBorder(&rect, BORDER_COLOR);
// Texto de marcador de posición
const int TEXT_SIZE = 6;
const int PADDING_IN_H = TEXT_SIZE;
const int PADDING_IN_V = TEXT_SIZE / 2;
text_->writeColored(PADDING_IN_H, PADDING_IN_V, "> _", BORDER_COLOR);
Screen::get()->setRendererSurface(previous_renderer);
// Posición inicial (fuera de pantalla por arriba)
SDL_FRect sprite_rect = {.x = 0, .y = y_, .w = WIDTH, .h = height_};
sprite_ = std::make_shared<Sprite>(surface_, sprite_rect);
}
// Actualiza la animación de la consola
void Console::update(float delta_time) {
if (status_ == Status::HIDDEN) {
return;
}
switch (status_) {
case Status::RISING: {
y_ += SLIDE_SPEED * delta_time;
if (y_ >= 0.0F) {
y_ = 0.0F;
status_ = Status::ACTIVE;
}
break;
}
case Status::VANISHING: {
y_ -= SLIDE_SPEED * delta_time;
if (y_ <= -height_) {
y_ = -height_;
status_ = Status::HIDDEN;
}
break;
}
default:
break;
}
SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_};
sprite_->setPosition(rect);
}
// Renderiza la consola
void Console::render() {
if (status_ == Status::HIDDEN) {
return;
}
sprite_->render();
}
// Activa o desactiva la consola
void Console::toggle() {
switch (status_) {
case Status::HIDDEN:
status_ = Status::RISING;
break;
case Status::ACTIVE:
status_ = Status::VANISHING;
break;
default:
// Durante RISING o VANISHING no se hace nada
break;
}
}
// Indica si la consola está activa (visible o en animación)
auto Console::isActive() -> bool {
return status_ != Status::HIDDEN;
}

View File

@@ -0,0 +1,59 @@
#pragma once
#include <SDL3/SDL.h>
#include <memory> // Para shared_ptr
#include <string> // Para string
class Surface;
class Sprite;
class Text;
class Console {
public:
// Singleton
static void init(const std::string& font_name);
static void destroy();
static auto get() -> Console*;
// Métodos principales
void update(float delta_time);
void render();
void toggle();
// Consultas
auto isActive() -> bool; // true si RISING, ACTIVE o VANISHING
private:
enum class Status {
HIDDEN,
RISING,
ACTIVE,
VANISHING,
};
// Constantes visuales
static constexpr Uint8 BG_COLOR = 0; // PaletteColor::BLACK
static constexpr Uint8 BORDER_COLOR = 9; // PaletteColor::BRIGHT_GREEN
static constexpr float SLIDE_SPEED = 120.0F;
// [SINGLETON]
static Console* console;
// Constructor y destructor privados [SINGLETON]
explicit Console(const std::string& font_name);
~Console() = default;
// Métodos privados
void buildSurface(); // Crea la Surface con el aspecto visual
// Objetos de renderizado
std::shared_ptr<Text> text_;
std::shared_ptr<Surface> surface_;
std::shared_ptr<Sprite> sprite_;
// Estado de la animación
Status status_{Status::HIDDEN};
float y_{0.0F}; // Posición Y actual (animada)
float height_{0.0F}; // Altura del panel
};