Sistema de notificacions amb presets (F4, llisca des de dalt) (v0.9.0)

This commit is contained in:
JailDesigner committed 2026-04-19 10:54:32 +02:00
1 parent 96cbdf96b6
commit 1b430500f4
10 files changed
+208 -10

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
#include "core/rendering/notifier.hpp"
#include "core/rendering/screen.hpp"
#include "core/rendering/surface.hpp"
#include "core/rendering/text.hpp"
#include "core/resources/resource_cache.hpp"
#include "utils/defines.hpp"
auto Notifier::get() -> Notifier& {
static Notifier instance_;
return instance_;
}
Notifier::~Notifier() = default;
void Notifier::init() {
// El Director preloade la paleta "ui" i la surface "8bithud.gif" abans
// de cridar aquest init(); ací només cal construir el Text.
auto font_surface = Resource::Cache::get().getSurface("8bithud.gif");
text_ = std::make_unique<Text>(font_surface, "data/font/8bithud.fnt");
}
void Notifier::update(int delta_ms) {
const float DT = static_cast<float>(delta_ms) / 1000.0F;
switch (status_) {
case Status::RISING:
y_ += SLIDE_SPEED * DT;
if (y_ >= 0.0F) {
y_ = 0.0F;
status_ = Status::ACTIVE;
}
break;
case Status::ACTIVE:
remaining_ms_ -= delta_ms;
if (remaining_ms_ <= 0) { status_ = Status::VANISHING; }
break;
case Status::VANISHING:
y_ -= SLIDE_SPEED * DT;
if (y_ <= static_cast<float>(-HEIGHT)) {
y_ = static_cast<float>(-HEIGHT);
status_ = Status::HIDDEN;
}
break;
default:
break;
}
}
void Notifier::render() {
if (status_ == Status::HIDDEN || text_ == nullptr || current_text_.empty()) { return; }
const int TEXT_W = text_->length(current_text_, 1);
const int BOX_W = TEXT_W + (2 * PADDING_H);
const int BOX_X = (GameCanvas::WIDTH - BOX_W) / 2;
const int BOX_Y = static_cast<int>(y_);
// Fons sòlid a través de la paleta "ui" (ARGB directe al canvas, sense
// tocar cap surface paletada).
const auto& ui_palette = Resource::Cache::get().getPalette("ui");
Screen::get().fillRect(BOX_X, BOX_Y, BOX_W, HEIGHT, ui_palette[current_bg_color_]);
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
GameCanvas::CENTER_X,
BOX_Y + PADDING_V,
current_text_,
1,
current_text_color_);
}
void Notifier::notify(const std::string& text) {
notify(text, Notification::DEFAULT);
}
void Notifier::notify(const std::string& text, const Notification::Preset& preset) {
notify(text, preset.text_color, preset.bg_color, preset.time_ms);
}
void Notifier::notify(const std::string& text, Uint8 text_color, Uint8 bg_color, int time_ms) {
// Política simple: la nova notificació reemplaça la que hi havia. Per
// gestionar-ne una cua, canviar ací per una FIFO quan calga.
current_text_ = text;
current_text_color_ = text_color;
current_bg_color_ = bg_color;
remaining_ms_ = time_ms;
status_ = Status::RISING;
}
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <memory>
#include <string>
#include "game/ui/notification_presets.hpp"
class Text;
// Popup transitori a dalt i centrat. Entra lliscant des de dalt, es queda
// visible el temps configurat i surt lliscant cap amunt. Comparteix la font
// 8bithud i la paleta "ui" amb la resta d'overlays (carregades per RenderInfo
// a l'arrencada; si Notifier::init() es crida primer, les carrega ell).
//
// Ús típic:
// Notifier::get().notify("Text ràpid"); // preset DEFAULT
// Notifier::get().notify("Text ràpid", miPreset); // preset custom
// Notifier::get().notify("Urgent", 11, 3, 800); // colors i durada explícits
class Notifier {
public:
static auto get() -> Notifier&;
Notifier(const Notifier&) = delete;
Notifier(Notifier&&) = delete;
auto operator=(const Notifier&) -> Notifier& = delete;
auto operator=(Notifier&&) -> Notifier& = delete;
void init();
void update(int delta_ms);
void render();
// Tres overloads; els dos simples deleguen al tercer.
void notify(const std::string& text);
void notify(const std::string& text, const Notification::Preset& preset);
void notify(const std::string& text, Uint8 text_color, Uint8 bg_color, int time_ms);
static constexpr int PADDING_H = 6;
static constexpr int PADDING_V = 3;
static constexpr int TEXT_SIZE = 8;
static constexpr int HEIGHT = TEXT_SIZE + (2 * PADDING_V);
static constexpr float SLIDE_SPEED = 120.0F;
private:
enum class Status : std::uint8_t { HIDDEN,
RISING,
ACTIVE,
VANISHING };
Notifier() = default;
~Notifier();
std::unique_ptr<Text> text_;
Status status_{Status::HIDDEN};
float y_{static_cast<float>(-HEIGHT)};
std::string current_text_;
Uint8 current_text_color_{0};
Uint8 current_bg_color_{0};
int remaining_ms_{0};
};
+3 -7
View File
@@ -32,14 +32,10 @@ auto RenderInfo::get() -> RenderInfo& {
RenderInfo::~RenderInfo() = default;
void RenderInfo::init() {
// Paleta compartida de UI: qualsevol HUD que la vulga només ha de cridar
// Cache::getPalette("ui") i aplicar-la a la seua surface. El .gif propi
// de la font (2 colors) es descarta a favor d'aquesta.
Resource::Cache::get().loadPalette("ui", "data/palettes/sega-cube.pal");
Resource::Cache::get().loadSurface("8bithud.gif", "data/font/8bithud.gif");
// El Director preloade la paleta "ui" i la surface "8bithud.gif"; ací
// només construïm el Text. Així Notifier i altres HUDs comparteixen
// recursos sense contenir la lògica de càrrega.
auto font_surface = Resource::Cache::get().getSurface("8bithud.gif");
font_surface->setPalette(Resource::Cache::get().getPalette("ui"));
text_ = std::make_unique<Text>(font_surface, "data/font/8bithud.fnt");
// Restaura l'estat guardat a config.yaml (video.render_info).
+11
View File
@@ -161,6 +161,17 @@ void Screen::clear(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
std::ranges::fill(canvas_, COLOR);
}
void Screen::fillRect(int x, int y, int w, int h, std::uint32_t argb) {
const int X0 = std::max(x, 0);
const int Y0 = std::max(y, 0);
const int X1 = std::min(x + w, canvas_width_);
const int Y1 = std::min(y + h, canvas_height_);
for (int iy = Y0; iy < Y1; ++iy) {
std::uint32_t* row = canvas_.data() + (static_cast<std::ptrdiff_t>(iy) * canvas_width_);
std::fill(row + X0, row + X1, argb);
}
}
void Screen::present() {
fps_.increment();
fps_.calculate(SDL_GetTicks());
+3
View File
@@ -55,6 +55,9 @@ class Screen {
void applyCrtPiParams(const Rendering::CrtPiParams& params);
void clear(std::uint8_t r = 0, std::uint8_t g = 0, std::uint8_t b = 0);
// Pinta un rectangle opac al canvas ARGB del joc. Pensat per a HUDs
// simples (caixes de notificacions, barres d'estat); no gasta paleta.
void fillRect(int x, int y, int w, int h, std::uint32_t argb);
void present();
[[nodiscard]] auto getRenderer() const -> SDL_Renderer* { return renderer_; }