RenderInfo amb tres estats (off/dalt/baix), temps de joc i persistència; ombra, mono-dígits i easing compartits (v0.14.0)
This commit is contained in:
1 parent
46a578fd86
commit
6f3fafbfff
17 files changed
+376
-97
No files matched your search
@@ -25,8 +25,8 @@ void Notifier::update(int delta_ms) {
|
||||
switch (status_) {
|
||||
case Status::RISING:
|
||||
y_ += SLIDE_SPEED * DT;
|
||||
if (y_ >= 0.0F) {
|
||||
y_ = 0.0F;
|
||||
if (y_ >= static_cast<float>(MARGIN_TOP)) {
|
||||
y_ = static_cast<float>(MARGIN_TOP);
|
||||
status_ = Status::ACTIVE;
|
||||
}
|
||||
break;
|
||||
@@ -37,7 +37,7 @@ void Notifier::update(int delta_ms) {
|
||||
case Status::VANISHING:
|
||||
y_ -= SLIDE_SPEED * DT;
|
||||
if (y_ <= static_cast<float>(-HEIGHT)) {
|
||||
y_ = static_cast<float>(-HEIGHT);
|
||||
y_ = static_cast<float>(-HEIGHT - MARGIN_TOP);
|
||||
status_ = Status::HIDDEN;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -42,6 +42,7 @@ class Notifier {
|
||||
static constexpr int PADDING_V = 3;
|
||||
static constexpr int TEXT_SIZE = 8;
|
||||
static constexpr int HEIGHT = TEXT_SIZE + (2 * PADDING_V);
|
||||
static constexpr int MARGIN_TOP = 2; // Separació amb la vora superior del canvas.
|
||||
static constexpr float SLIDE_SPEED = 120.0F;
|
||||
|
||||
private:
|
||||
@@ -55,7 +56,7 @@ class Notifier {
|
||||
|
||||
std::unique_ptr<Text> text_;
|
||||
Status status_{Status::HIDDEN};
|
||||
float y_{static_cast<float>(-HEIGHT)};
|
||||
float y_{static_cast<float>(-HEIGHT - MARGIN_TOP)};
|
||||
std::string current_text_;
|
||||
Uint8 current_text_color_{0};
|
||||
Uint8 current_bg_color_{0};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "core/rendering/render_info.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "core/rendering/screen.hpp"
|
||||
@@ -10,18 +12,33 @@
|
||||
#include "game/options.hpp"
|
||||
#include "game/ui/ui_palette.hpp"
|
||||
#include "utils/defines.hpp"
|
||||
#include "utils/easing.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int TEXT_SIZE = 8;
|
||||
constexpr int PADDING_V = (RenderInfo::HEIGHT - TEXT_SIZE) / 2;
|
||||
|
||||
// Llindar per saltar el dibuixat d'un segment quasi totalment transparent.
|
||||
// Evita una fileta d'un sol píxel quan acaba de començar a aparèixer.
|
||||
constexpr float VISIBLE_EPSILON = 0.01F;
|
||||
|
||||
auto prettify(std::string s) -> std::string {
|
||||
std::ranges::replace(s, '-', ' ');
|
||||
std::ranges::transform(s, s.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||
return s;
|
||||
}
|
||||
|
||||
auto formatTime(int elapsed_ms) -> std::string {
|
||||
const int TOTAL_CS = elapsed_ms / 10;
|
||||
const int MIN = TOTAL_CS / (60 * 100);
|
||||
const int SEC = (TOTAL_CS / 100) % 60;
|
||||
const int CENTI = TOTAL_CS % 100;
|
||||
std::array<char, 16> buf{};
|
||||
std::snprintf(buf.data(), buf.size(), "%d:%02d.%02d", MIN, SEC, CENTI);
|
||||
return std::string{buf.data()};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
auto RenderInfo::get() -> RenderInfo& {
|
||||
@@ -32,80 +49,167 @@ auto RenderInfo::get() -> RenderInfo& {
|
||||
RenderInfo::~RenderInfo() = default;
|
||||
|
||||
void RenderInfo::init() {
|
||||
// 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");
|
||||
text_ = std::make_unique<Text>(font_surface, "data/font/8bithud.fnt");
|
||||
|
||||
// Restaura l'estat guardat a config.yaml (video.render_info).
|
||||
if (Options::video.render_info) {
|
||||
status_ = Status::ACTIVE;
|
||||
y_ = 0.0F;
|
||||
// Restaura l'estat guardat: si l'overlay estava engegat, l'activem ja
|
||||
// plenament visible (sense animació d'entrada inicial).
|
||||
if (Options::video.render_info_position != Defaults::RenderInfoPosition::OFF) {
|
||||
visible_pos_ = Options::video.render_info_position;
|
||||
info_anim_ = 1.0F;
|
||||
}
|
||||
|
||||
// El segment de temps pot estar activat per persistència: si la barra
|
||||
// apareix ja plena, el seu segment també ha d'estar-ho.
|
||||
segments_[3].mono_digits = true;
|
||||
if (info_anim_ >= 1.0F) {
|
||||
refreshSegments();
|
||||
for (auto& seg : segments_) { seg.anim = seg.visible ? 1.0F : 0.0F; }
|
||||
}
|
||||
}
|
||||
|
||||
void RenderInfo::update(int delta_ms) {
|
||||
elapsed_ms_ += 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::VANISHING:
|
||||
y_ -= SLIDE_SPEED * DT;
|
||||
if (y_ <= static_cast<float>(-HEIGHT)) {
|
||||
y_ = static_cast<float>(-HEIGHT);
|
||||
status_ = Status::HIDDEN;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
// Transició global de posició. Si la posició desitjada coincideix amb la
|
||||
// visible, entrem (anim→1). Si no, primer eixim (anim→0) del lloc actual;
|
||||
// quan arriba a 0 commutem visible_pos_ i deixem que el proper frame
|
||||
// dispari l'entrada. Excepció: de OFF a TOP/BOTTOM commutem en sec.
|
||||
const auto DESIRED = Options::video.render_info_position;
|
||||
if (DESIRED == visible_pos_) {
|
||||
// Si el destí és OFF, no cal animar cap a un lloc — la barra ja ha
|
||||
// d'estar fora. Sense aquest guard, en arribar de BOTTOM→OFF la
|
||||
// anim tornaria a pujar cap a 1 i el render (branca BOTTOM per
|
||||
// defecte) la tornaria a pintar abaix.
|
||||
if (visible_pos_ == Defaults::RenderInfoPosition::OFF) {
|
||||
info_anim_ = 0.0F;
|
||||
} else if (info_anim_ < 1.0F) {
|
||||
info_anim_ = std::min(1.0F, info_anim_ + (SLIDE_SPEED * DT));
|
||||
}
|
||||
} else if (visible_pos_ == Defaults::RenderInfoPosition::OFF) {
|
||||
visible_pos_ = DESIRED;
|
||||
info_anim_ = 0.0F;
|
||||
} else {
|
||||
info_anim_ -= SLIDE_SPEED * DT;
|
||||
if (info_anim_ <= 0.0F) {
|
||||
info_anim_ = 0.0F;
|
||||
visible_pos_ = DESIRED;
|
||||
}
|
||||
}
|
||||
|
||||
// Decidim què ha d'eixir cada segment i fem tendir la seua anim.
|
||||
refreshSegments();
|
||||
for (auto& seg : segments_) {
|
||||
const float TARGET = seg.visible ? 1.0F : 0.0F;
|
||||
if (seg.anim < TARGET) {
|
||||
seg.anim = std::min(TARGET, seg.anim + (SEG_SPEED * DT));
|
||||
} else if (seg.anim > TARGET) {
|
||||
seg.anim = std::max(TARGET, seg.anim - (SEG_SPEED * DT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RenderInfo::render() {
|
||||
if (status_ == Status::HIDDEN || text_ == nullptr) { return; }
|
||||
|
||||
std::string line = std::to_string(Screen::get().getLastFPS()) + " fps";
|
||||
void RenderInfo::refreshSegments() {
|
||||
// [0] FPS (sempre visible mentre l'overlay apareix)
|
||||
segments_[0].text = std::to_string(Screen::get().getLastFPS()) + " fps";
|
||||
segments_[0].visible = true;
|
||||
segments_[0].mono_digits = false;
|
||||
|
||||
// [1] Driver GPU (sempre visible)
|
||||
const auto& driver = Screen::get().getGPUDriver();
|
||||
line += " | " + (driver.empty() ? std::string("sdl") : driver);
|
||||
segments_[1].text = std::string(" | ") + (driver.empty() ? std::string("sdl") : driver);
|
||||
segments_[1].visible = true;
|
||||
segments_[1].mono_digits = false;
|
||||
|
||||
// [2] Shader + preset (només amb shader activat)
|
||||
if (Options::video.shader.enabled) {
|
||||
const std::string& shader = Options::video.shader.current_shader;
|
||||
const std::string& preset = (shader == "crtpi")
|
||||
? Options::video.shader.current_crtpi_preset
|
||||
: Options::video.shader.current_postfx_preset;
|
||||
line += " | " + shader + " " + prettify(preset);
|
||||
segments_[2].text = " | " + shader + " " + prettify(preset);
|
||||
}
|
||||
segments_[2].visible = Options::video.shader.enabled;
|
||||
segments_[2].mono_digits = false;
|
||||
|
||||
line = prettify(line);
|
||||
// [3] Temps de joc (togglejable i persistent)
|
||||
segments_[3].text = " | " + formatTime(elapsed_ms_);
|
||||
segments_[3].visible = Options::video.render_info_show_time;
|
||||
segments_[3].mono_digits = true;
|
||||
|
||||
text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
|
||||
GameCanvas::CENTER_X,
|
||||
static_cast<int>(y_) + PADDING_V,
|
||||
line,
|
||||
1,
|
||||
UiColor::RENDER_INFO_TEXT);
|
||||
// Els tres primers segments baixem a minúscules per coherència estètica
|
||||
// amb la resta de la línia; el [3] (temps) manté dígits i puntuació.
|
||||
segments_[0].text = prettify(segments_[0].text);
|
||||
segments_[1].text = prettify(segments_[1].text);
|
||||
segments_[2].text = prettify(segments_[2].text);
|
||||
}
|
||||
|
||||
void RenderInfo::toggle() {
|
||||
switch (status_) {
|
||||
case Status::HIDDEN:
|
||||
status_ = Status::RISING;
|
||||
Options::video.render_info = true;
|
||||
Options::saveToFile();
|
||||
break;
|
||||
case Status::ACTIVE:
|
||||
status_ = Status::VANISHING;
|
||||
Options::video.render_info = false;
|
||||
Options::saveToFile();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
auto RenderInfo::segmentWidth(const Segment& seg, int digit_cell_w) const -> int {
|
||||
if (text_ == nullptr || seg.text.empty()) { return 0; }
|
||||
return seg.mono_digits
|
||||
? text_->lengthMonoDigits(seg.text, digit_cell_w)
|
||||
: text_->length(seg.text);
|
||||
}
|
||||
|
||||
void RenderInfo::render() {
|
||||
if (text_ == nullptr) { return; }
|
||||
if (visible_pos_ == Defaults::RenderInfoPosition::OFF && info_anim_ <= 0.0F) { return; }
|
||||
|
||||
const int DIGIT_CELL = text_->getCharacterSize() - 1;
|
||||
|
||||
// Amplada total considerant animació individual: cada segment contribueix
|
||||
// menys quan està apareixent o desapareixent, per a que el conjunt
|
||||
// s'expandeixca/es contraga suaument.
|
||||
float total_w = 0.0F;
|
||||
for (const auto& seg : segments_) {
|
||||
if (seg.anim <= 0.0F || seg.text.empty()) { continue; }
|
||||
const int W = segmentWidth(seg, DIGIT_CELL);
|
||||
total_w += static_cast<float>(W) * Easing::outQuad(seg.anim);
|
||||
}
|
||||
if (total_w <= 0.0F) { return; }
|
||||
|
||||
// Y final/inicial segons posició. El lliscament va de (fora) a (dins)
|
||||
// interpolat amb outQuad de la anim global.
|
||||
const int CH = TEXT_SIZE;
|
||||
int final_y = 0;
|
||||
int start_y = 0;
|
||||
if (visible_pos_ == Defaults::RenderInfoPosition::TOP) {
|
||||
final_y = PADDING_V;
|
||||
start_y = -CH - 1;
|
||||
} else {
|
||||
final_y = GameCanvas::HEIGHT - CH - PADDING_V - 1;
|
||||
start_y = GameCanvas::HEIGHT;
|
||||
}
|
||||
const int Y = start_y + static_cast<int>(static_cast<float>(final_y - start_y) * Easing::outQuad(info_anim_));
|
||||
|
||||
// Centrat horitzontal sobre l'amplada animada.
|
||||
float cur_x = (static_cast<float>(GameCanvas::WIDTH) - total_w) / 2.0F;
|
||||
for (const auto& seg : segments_) {
|
||||
if (seg.anim <= VISIBLE_EPSILON || seg.text.empty()) { continue; }
|
||||
const int XI = static_cast<int>(cur_x);
|
||||
const int SEG_W = segmentWidth(seg, DIGIT_CELL);
|
||||
|
||||
if (seg.mono_digits) {
|
||||
text_->writeMonoDigits(XI + 1, Y + 1, seg.text, UiColor::RENDER_INFO_SHADOW, DIGIT_CELL);
|
||||
text_->writeMonoDigits(XI, Y, seg.text, UiColor::RENDER_INFO_TEXT, DIGIT_CELL);
|
||||
} else {
|
||||
text_->writeColored(XI + 1, Y + 1, seg.text, UiColor::RENDER_INFO_SHADOW);
|
||||
text_->writeColored(XI, Y, seg.text, UiColor::RENDER_INFO_TEXT);
|
||||
}
|
||||
|
||||
cur_x += static_cast<float>(SEG_W) * Easing::outQuad(seg.anim);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderInfo::cyclePosition() {
|
||||
const auto CURRENT = Options::video.render_info_position;
|
||||
const int NEXT = (static_cast<int>(CURRENT) + 1) % 3;
|
||||
Options::video.render_info_position = static_cast<Defaults::RenderInfoPosition>(NEXT);
|
||||
Options::saveToFile();
|
||||
}
|
||||
|
||||
void RenderInfo::toggleShowTime() {
|
||||
Options::video.render_info_show_time = !Options::video.render_info_show_time;
|
||||
Options::saveToFile();
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "utils/defaults.hpp"
|
||||
|
||||
class Text;
|
||||
|
||||
// Overlay de debug que mostra FPS, driver GPU i shader + preset actiu. Es
|
||||
// dibuixa a la part superior de la pantalla amb una animació de lliscament
|
||||
// (RISING / ACTIVE / VANISHING / HIDDEN). L'activa la tecla F3 a l'escena.
|
||||
// Overlay de debug amb tres estats (OFF / TOP / BOTTOM). Mostra una barra
|
||||
// d'info dividida en segments independents (fps, driver, shader, temps) que
|
||||
// apareixen i desapareixen suaument. La posició es commuta amb una tecla
|
||||
// (cicle OFF → TOP → BOTTOM) i el segment de temps es pot mostrar o amagar
|
||||
// per separat; les dos opcions persisteixen a config.yaml.
|
||||
class RenderInfo {
|
||||
public:
|
||||
static auto get() -> RenderInfo&;
|
||||
@@ -17,29 +23,41 @@ class RenderInfo {
|
||||
auto operator=(const RenderInfo&) -> RenderInfo& = delete;
|
||||
auto operator=(RenderInfo&&) -> RenderInfo& = delete;
|
||||
|
||||
// Carrega la font 8bithud. S'ha de cridar després de configurar Screen.
|
||||
void init();
|
||||
|
||||
void update(int delta_ms);
|
||||
void render();
|
||||
void toggle();
|
||||
|
||||
[[nodiscard]] auto isActive() const -> bool { return status_ != Status::HIDDEN; }
|
||||
// Avança la posició OFF → TOP → BOTTOM → OFF i persisteix.
|
||||
static void cyclePosition();
|
||||
|
||||
// Mostra/oculta el segment de temps i persisteix. Només té efecte visual
|
||||
// quan la barra és visible (TOP/BOTTOM).
|
||||
static void toggleShowTime();
|
||||
|
||||
// Alçada fixa de l'overlay: TEXT_SIZE (8) + padding vertical.
|
||||
static constexpr int HEIGHT = 10;
|
||||
static constexpr float SLIDE_SPEED = 120.0F;
|
||||
static constexpr float SLIDE_SPEED = 5.0F; // posició global (1/s)
|
||||
static constexpr float SEG_SPEED = 6.0F; // segments individuals (1/s)
|
||||
|
||||
private:
|
||||
enum class Status : std::uint8_t { HIDDEN,
|
||||
RISING,
|
||||
ACTIVE,
|
||||
VANISHING };
|
||||
static constexpr int SEGMENT_COUNT = 4; // fps · driver · shader · temps
|
||||
|
||||
struct Segment {
|
||||
std::string text;
|
||||
float anim{0.0F}; // 0 = fora, 1 = dins
|
||||
bool visible{false}; // objectiu cap al que tendeix anim
|
||||
bool mono_digits{false}; // true només al segment de temps
|
||||
};
|
||||
|
||||
RenderInfo() = default;
|
||||
~RenderInfo();
|
||||
|
||||
void refreshSegments();
|
||||
[[nodiscard]] auto segmentWidth(const Segment& seg, int digit_cell_w) const -> int;
|
||||
|
||||
std::unique_ptr<Text> text_;
|
||||
Status status_{Status::HIDDEN};
|
||||
float y_{static_cast<float>(-HEIGHT)};
|
||||
Defaults::RenderInfoPosition visible_pos_{Defaults::RenderInfoPosition::OFF};
|
||||
float info_anim_{0.0F}; // 0..1, suavitzat amb outQuad al render
|
||||
std::array<Segment, SEGMENT_COUNT> segments_{};
|
||||
int elapsed_ms_{0};
|
||||
};
|
||||
@@ -224,6 +224,54 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string& text, int kerni
|
||||
}
|
||||
}
|
||||
|
||||
// Amplada fixa per dígits: cada '0'-'9' ocupa digit_cell_w centrat dins de
|
||||
// la cel·la; la resta de glifs avancen amb la seua amplada natural. Emprat
|
||||
// pel comptador de temps de l'overlay RenderInfo per a que els dígits no
|
||||
// balleteigen quan canvien.
|
||||
void Text::writeMonoDigits(int x, int y, const std::string& text, Uint8 color, int digit_cell_w, int kerning) {
|
||||
int shift = 0;
|
||||
std::size_t pos = 0;
|
||||
|
||||
sprite_->setY(static_cast<float>(y));
|
||||
while (pos < text.size()) {
|
||||
std::uint32_t cp = nextCodepoint(text, pos);
|
||||
auto it = offset_.find(cp);
|
||||
if (it == offset_.end()) { it = offset_.find('?'); }
|
||||
if (it == offset_.end()) { continue; }
|
||||
|
||||
const bool IS_DIGIT = (cp >= '0' && cp <= '9');
|
||||
const int GLYPH_W = it->second.w;
|
||||
const int CELL_W = IS_DIGIT ? digit_cell_w : GLYPH_W;
|
||||
const int GLYPH_X = IS_DIGIT ? x + shift + ((CELL_W - GLYPH_W) / 2) : x + shift;
|
||||
|
||||
sprite_->setClip(static_cast<float>(it->second.x),
|
||||
static_cast<float>(it->second.y),
|
||||
static_cast<float>(box_width_),
|
||||
static_cast<float>(box_height_));
|
||||
sprite_->setX(static_cast<float>(GLYPH_X));
|
||||
sprite_->render(1, color);
|
||||
|
||||
shift += CELL_W + kerning;
|
||||
}
|
||||
}
|
||||
|
||||
auto Text::lengthMonoDigits(const std::string& text, int digit_cell_w, int kerning) const -> int {
|
||||
int shift = 0;
|
||||
std::size_t pos = 0;
|
||||
|
||||
while (pos < text.size()) {
|
||||
std::uint32_t cp = nextCodepoint(text, pos);
|
||||
auto it = offset_.find(cp);
|
||||
if (it == offset_.end()) { it = offset_.find('?'); }
|
||||
if (it == offset_.end()) { continue; }
|
||||
|
||||
const bool IS_DIGIT = (cp >= '0' && cp <= '9');
|
||||
shift += (IS_DIGIT ? digit_cell_w : it->second.w) + kerning;
|
||||
}
|
||||
|
||||
return shift > 0 ? shift - kerning : 0;
|
||||
}
|
||||
|
||||
auto Text::length(const std::string& text, int kerning) const -> int {
|
||||
int shift = 0;
|
||||
std::size_t pos = 0;
|
||||
|
||||
@@ -51,7 +51,13 @@ class Text {
|
||||
void writeCentered(int x, int y, const std::string& text, int kerning = 1, int length = -1);
|
||||
void writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning = 1, Uint8 text_color = 0, Uint8 shadow_distance = 1, Uint8 shadow_color = 0, int length = -1);
|
||||
|
||||
// Dibuixa amb amplada fixa només per als dígits '0'-'9' (cadascun centrat
|
||||
// dins d'una cel·la de digit_cell_w). La resta de glifs mantenen amplada
|
||||
// natural. Evita jitter en comptadors tipus "9:59.59".
|
||||
void writeMonoDigits(int x, int y, const std::string& text, Uint8 color, int digit_cell_w, int kerning = 1);
|
||||
|
||||
[[nodiscard]] auto length(const std::string& text, int kerning = 1) const -> int;
|
||||
[[nodiscard]] auto lengthMonoDigits(const std::string& text, int digit_cell_w, int kerning = 1) const -> int;
|
||||
[[nodiscard]] auto getCharacterSize() const -> int { return box_width_; }
|
||||
[[nodiscard]] auto glyphWidth(std::uint32_t codepoint, int kerning = 0) const -> int;
|
||||
[[nodiscard]] auto getGlyphClip(std::uint32_t codepoint) const -> SDL_FRect;
|
||||
|
||||
Reference in new issue
Block a user