129 lines
3.4 KiB
C++
129 lines
3.4 KiB
C++
#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;
|
|
}
|