Cut per-frame text tail and label allocations
- Font: walk UTF-8 codepoints in place instead of building a vector; reuse the texture-cache key buffer on the hit path. - Text tails: show/ShowAll test isShowing (kept equal to list membership) instead of a linear find per tail, which made the per-frame ShowAllTextTail O(N^2); memoize label widths measured by both Arrange and Render. - Tail painter reuses one CGraphicTextInstance and font name. - CGraphicTextInstance::Render no longer copies the text unless secret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
33552b9eeb
commit
4662e06b35
@@ -132,7 +132,7 @@ void TextTailManager::ShowCharacterTextTail(uint32_t vid) {
|
||||
if (it == m_characterMap.end()) return;
|
||||
|
||||
auto tail = it->second;
|
||||
if (std::find(m_characterList.begin(), m_characterList.end(), tail) == m_characterList.end()) {
|
||||
if (!tail->isShowing) { // isShowing <=> in m_characterList
|
||||
tail->isShowing = true;
|
||||
m_characterList.push_back(tail);
|
||||
}
|
||||
@@ -200,7 +200,7 @@ void TextTailManager::ShowItemTextTail(uint32_t vid) {
|
||||
if (it == m_itemMap.end()) return;
|
||||
|
||||
auto tail = it->second;
|
||||
if (std::find(m_itemList.begin(), m_itemList.end(), tail) == m_itemList.end()) {
|
||||
if (!tail->isShowing) { // isShowing <=> in m_itemList
|
||||
tail->isShowing = true;
|
||||
m_itemList.push_back(tail);
|
||||
}
|
||||
@@ -237,13 +237,13 @@ bool TextTailManager::IsChatTextTail(uint32_t vid) const {
|
||||
|
||||
void TextTailManager::ShowAllTextTail() {
|
||||
for (auto& [vid, tail] : m_characterMap) {
|
||||
if (std::find(m_characterList.begin(), m_characterList.end(), tail) == m_characterList.end()) {
|
||||
if (!tail->isShowing) { // isShowing <=> in m_characterList
|
||||
tail->isShowing = true;
|
||||
m_characterList.push_back(tail);
|
||||
}
|
||||
}
|
||||
for (auto& [vid, tail] : m_itemMap) {
|
||||
if (std::find(m_itemList.begin(), m_itemList.end(), tail) == m_itemList.end()) {
|
||||
if (!tail->isShowing) { // isShowing <=> in m_itemList
|
||||
tail->isShowing = true;
|
||||
m_itemList.push_back(tail);
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ public:
|
||||
void Render(SDL_Renderer* renderer, const Gr2SceneRenderer* sceneRenderer = nullptr);
|
||||
using TextPainter = std::function<void(float,float,const std::string&,uint32_t,float,bool)>;
|
||||
using TextMeasure = std::function<float(const std::string&,float)>;
|
||||
void SetTextPainter(TextPainter painter, TextMeasure measure) { m_textPainter = std::move(painter); m_textMeasure = std::move(measure); }
|
||||
void SetTextPainter(TextPainter painter, TextMeasure measure) { m_textPainter = std::move(painter); m_textMeasure = std::move(measure); m_widths.clear(); }
|
||||
|
||||
// Queries & Getters
|
||||
size_t GetCharacterCount() const { return m_characterMap.size(); }
|
||||
@@ -148,9 +148,16 @@ public:
|
||||
private:
|
||||
TextPainter m_textPainter;
|
||||
TextMeasure m_textMeasure;
|
||||
// Names are measured by Arrange and again by Render every frame; memoize per scale.
|
||||
float TextWidth(const std::string& text, float scale = 1.0f) const {
|
||||
return m_textMeasure ? m_textMeasure(text, scale) : float(text.size()) * 8.0f * scale;
|
||||
if (!m_textMeasure) return float(text.size()) * 8.0f * scale;
|
||||
auto& widths = m_widths[scale];
|
||||
auto it = widths.find(text);
|
||||
if (it != widths.end()) return it->second;
|
||||
if (widths.size() >= 4096) widths.clear();
|
||||
return widths.emplace(text, m_textMeasure(text, scale)).first->second;
|
||||
}
|
||||
mutable std::unordered_map<float, std::unordered_map<std::string, float>> m_widths;
|
||||
bool isIn(const TextTailItem& src, const TextTailItem& tgt) const;
|
||||
void RenderTextTailBox(SDL_Renderer* renderer, const TextTailItem& tail);
|
||||
void RenderText(SDL_Renderer* renderer, float x, float y, const std::string& text, uint32_t color, float scale = 1.0f, bool shadow = true);
|
||||
|
||||
+15
-10
@@ -1,6 +1,7 @@
|
||||
#include "Font.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <list>
|
||||
@@ -13,8 +14,9 @@
|
||||
|
||||
namespace UI {
|
||||
namespace {
|
||||
std::vector<unsigned> Codepoints(const std::string& text) {
|
||||
std::vector<unsigned> out;
|
||||
// Visits codepoints in place; measuring/drawing runs per label per frame, so no vector.
|
||||
template <class Visit>
|
||||
void ForEachCodepoint(const std::string& text, Visit&& visit) {
|
||||
for (size_t i = 0; i < text.size();) {
|
||||
unsigned c = static_cast<unsigned char>(text[i++]);
|
||||
int n = c >= 0xf0 && c <= 0xf4 ? 3 : c >= 0xe0 && c < 0xf0 ? 2 : c >= 0xc2 && c < 0xe0 ? 1 : 0;
|
||||
@@ -24,9 +26,8 @@ std::vector<unsigned> Codepoints(const std::string& text) {
|
||||
value = (value << 6) | (static_cast<unsigned char>(text[end++]) & 0x3f);
|
||||
if (end - i == static_cast<size_t>(n)) { i = end; c = value; }
|
||||
// Some legacy labels include a trailing NUL; SDL's debug font hid it.
|
||||
if (c >= 32 && c != 127) out.push_back(c);
|
||||
if (c >= 32 && c != 127) visit(c);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
#ifdef AVA2_HAS_FREETYPE
|
||||
struct Glyph {
|
||||
@@ -108,12 +109,12 @@ int TextFontSize(const std::string& name) {
|
||||
|
||||
float MeasureText(const std::string& text, int size) {
|
||||
float width = 0;
|
||||
for (unsigned code : Codepoints(text)) {
|
||||
ForEachCodepoint(text, [&](unsigned code) {
|
||||
#ifdef AVA2_HAS_FREETYPE
|
||||
if (Cache().face) { width += Cache().Get(size, code).advance; continue; }
|
||||
if (Cache().face) { width += Cache().Get(size, code).advance; return; }
|
||||
#endif
|
||||
width += size * 0.5f;
|
||||
}
|
||||
});
|
||||
return width;
|
||||
}
|
||||
|
||||
@@ -122,7 +123,11 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x
|
||||
#ifdef AVA2_HAS_FREETYPE
|
||||
if (Cache().face) {
|
||||
auto& cache = TextureCache(renderer);
|
||||
const std::string key = std::to_string(size) + ":" + text;
|
||||
// Reused buffer: the hit path runs for every label every frame.
|
||||
static std::string key;
|
||||
char prefix[16];
|
||||
key.assign(prefix, std::snprintf(prefix, sizeof(prefix), "%d:", size));
|
||||
key += text;
|
||||
auto found = cache.entries.find(key);
|
||||
if (found != cache.entries.end()) {
|
||||
cache.recent.splice(cache.recent.begin(), cache.recent, found->second.recent);
|
||||
@@ -139,7 +144,7 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x
|
||||
if (!surface) return;
|
||||
SDL_FillSurfaceRect(surface, nullptr, 0);
|
||||
float pen = 0;
|
||||
for (unsigned code : Codepoints(text)) {
|
||||
ForEachCodepoint(text, [&](unsigned code) {
|
||||
const auto& glyph = Cache().Get(size, code);
|
||||
const int gx = int(std::round(pen * 2)) + glyph.left;
|
||||
const int gy = size * 2 - glyph.top;
|
||||
@@ -155,7 +160,7 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x
|
||||
}
|
||||
}
|
||||
pen += glyph.advance;
|
||||
}
|
||||
});
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_DestroySurface(surface);
|
||||
if (texture) {
|
||||
|
||||
@@ -782,15 +782,16 @@ namespace UI
|
||||
SDL_Renderer* renderer = pDevice->GetRenderer();
|
||||
if (!renderer) return;
|
||||
|
||||
std::string displayText = m_text;
|
||||
std::string secretText;
|
||||
if (m_secret)
|
||||
displayText.assign(m_text.length(), '*');
|
||||
secretText.assign(m_text.length(), '*');
|
||||
const std::string& displayText = m_secret ? secretText : m_text;
|
||||
|
||||
if (displayText.empty() && !m_showCursor)
|
||||
return;
|
||||
|
||||
const int fontSize = TextFontSize(m_font);
|
||||
const float charWidth = MeasureText("*", fontSize);
|
||||
const float charWidth = m_showCursor ? MeasureText("*", fontSize) : 0.0f;
|
||||
const float charHeight = float(fontSize);
|
||||
float textWidth = MeasureText(displayText, fontSize);
|
||||
float textHeight = charHeight;
|
||||
|
||||
@@ -615,16 +615,21 @@ static PyObject* texttail_register_item(PyObject*, PyObject* args) {
|
||||
void initTextTail() {
|
||||
// Use the same FreeType font path as UI text, preserving the Retina render
|
||||
// scale. SDL_RenderDebugText's fixed bitmap font is only a host fallback.
|
||||
// One reused instance per callback: text tails paint several labels per actor per frame.
|
||||
static auto fontFor=[](UI::CGraphicTextInstance& text,int& lastSize,float scale) {
|
||||
const int size=int(12 * scale);
|
||||
if(size!=lastSize){lastSize=size;text.SetFontName(("Tahoma:" + std::to_string(size)).c_str());}
|
||||
};
|
||||
TextTailManager::Instance().SetTextPainter(
|
||||
[](float x,float y,const std::string& value,uint32_t color,float scale,bool outline) {
|
||||
UI::CGraphicTextInstance text;
|
||||
text.SetFontName(("Tahoma:" + std::to_string(int(12 * scale))).c_str());
|
||||
static UI::CGraphicTextInstance text;static int lastSize=-1;
|
||||
fontFor(text,lastSize,scale);
|
||||
text.SetValue(value.c_str()); text.SetFontColor(color);
|
||||
text.SetPosition(x,y); text.SetOutline(outline); text.Render();
|
||||
},
|
||||
[](const std::string& value,float scale) {
|
||||
UI::CGraphicTextInstance text;
|
||||
text.SetFontName(("Tahoma:" + std::to_string(int(12 * scale))).c_str());
|
||||
static UI::CGraphicTextInstance text;static int lastSize=-1;
|
||||
fontFor(text,lastSize,scale);
|
||||
text.SetValue(value.c_str()); int width=0; text.GetTextSize(&width,nullptr); return float(width);
|
||||
});
|
||||
static PyMethodDef methods[] = {
|
||||
|
||||
Reference in New Issue
Block a user