feat: rendering pages on demand

This commit is contained in:
MihailRis 2026-02-16 01:42:48 +03:00
parent 381cb77d08
commit 47809b4024
9 changed files with 106 additions and 39 deletions

View file

@ -1,11 +1,13 @@
#include "Font.hpp"
#include "Batch2D.hpp"
#include "Batch3D.hpp"
#include "coders/vector_fonts.hpp"
#include "Texture.hpp"
#include "window/Camera.hpp"
#include <limits>
#include <utility>
#include "Texture.hpp"
#include "Batch2D.hpp"
#include "Batch3D.hpp"
#include "window/Camera.hpp"
inline constexpr uint GLYPH_SIZE = 16;
inline constexpr uint MAX_CODEPAGES = 10000; // idk ho many codepages unicode has
@ -15,13 +17,15 @@ Font::Font(
std::vector<std::unique_ptr<Texture>> pages,
std::vector<Glyph> glyphs,
int lineHeight,
int yoffset
int yoffset,
std::optional<std::weak_ptr<vector_fonts::FontFile>> fontFile
)
: lineHeight(lineHeight),
yoffset(yoffset),
glyphInterval(lineHeight / 2),
pages(std::move(pages)),
glyphs(std::move(glyphs)) {
glyphs(std::move(glyphs)),
fontFile(std::move(fontFile)) {
}
Font::~Font() = default;
@ -139,7 +143,7 @@ static inline void draw_glyph(
template <class Batch>
static inline void draw_text(
const Font& font,
Font& font,
Batch& batch,
std::wstring_view text,
const glm::vec3& pos,
@ -244,7 +248,7 @@ void Font::draw(
const FontStylesScheme* styles,
size_t styleMapOffset,
float scale
) const {
) {
draw_text(
*this, batch, text,
glm::vec3(x, y, 0),
@ -264,7 +268,7 @@ void Font::draw(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up
) const {
) {
draw_text(
*this, batch, text, pos,
right * static_cast<float>(glyphInterval),
@ -296,3 +300,28 @@ std::unique_ptr<Font> Font::createBitmapFont(
}
return std::make_unique<Font>(std::move(textures), std::move(glyphs), res, 4);
}
const Glyph* Font::getGlyph(int codepoint) {
if (codepoint < 0) {
return nullptr;
}
if (codepoint < glyphs.size()) {
return &glyphs.at(codepoint);
}
if (!this->fontFile.has_value() || this->fontFile->expired()) {
return nullptr;
}
int codepage = codepoint >> 8;
if (codepage >= 1024) {
return nullptr;
}
if (glyphs.size() < (codepage << 8)) {
glyphs.resize(codepage << 8);
}
auto fontFile = this->fontFile->lock();
if (pages.size() <= codepage) {
pages.resize(codepage);
}
pages.push_back(fontFile->renderPage(codepage, glyphs, lineHeight));
return &glyphs.at(codepoint);
}

View file

@ -3,6 +3,7 @@
#include <memory>
#include <string>
#include <vector>
#include <optional>
#include <glm/glm.hpp>
#include "typedefs.hpp"
@ -18,18 +19,23 @@ class ImageData;
class Font;
namespace vector_fonts {
class FontFile;
}
struct Glyph {
int yOffset;
int xAdvance;
};
class Font {
public:
struct Glyph {
int yOffset;
int xAdvance;
};
Font(
std::vector<std::unique_ptr<Texture>> pages,
std::vector<Glyph> glyphs,
int lineHeight,
int yoffset
int yoffset,
std::optional<std::weak_ptr<vector_fonts::FontFile>> fontFile = std::nullopt
);
~Font();
@ -61,7 +67,7 @@ public:
const FontStylesScheme* styles,
size_t styleMapOffset,
float scale = 1
) const;
);
void draw(
Batch3D& batch,
@ -71,7 +77,7 @@ public:
const glm::vec3& pos,
const glm::vec3& right={1, 0, 0},
const glm::vec3& up={0, 1, 0}
) const;
);
const Texture* getPage(int page) const;
@ -79,11 +85,7 @@ public:
return {std::nullopt, lineHeight, yoffset, glyphInterval};
}
const Glyph* getGlyph(int codepoint) const {
return (codepoint < 0 || codepoint >= glyphs.size())
? nullptr
: &glyphs.at(codepoint);
}
const Glyph* getGlyph(int codepoint);
static std::unique_ptr<Font> createBitmapFont(
std::vector<std::unique_ptr<ImageData>> pages
@ -94,4 +96,5 @@ private:
int glyphInterval;
std::vector<std::unique_ptr<Texture>> pages;
std::vector<Glyph> glyphs;
std::optional<std::weak_ptr<vector_fonts::FontFile>> fontFile;
};

View file

@ -45,7 +45,7 @@ void TextsRenderer::renderNote(
}
opacity = preset.xrayOpacity;
}
const auto& font = assets.require<Font>(FONT_DEFAULT);
auto& font = assets.require<Font>(FONT_DEFAULT);
glm::vec3 xvec = note.getAxisX();
glm::vec3 yvec = note.getAxisY();

View file

@ -276,13 +276,13 @@ void SlotView::draw(const DrawContext& pctx, const Assets& assets) {
drawItemIcon(batch, stack, item, assets, tint, pos);
if (stack.getCount() > 1 || stack.getFields() != nullptr) {
const auto& font = assets.require<Font>(FONT_DEFAULT);
auto& font = assets.require<Font>(FONT_DEFAULT);
drawItemInfo(batch, stack, item, font, pos);
}
}
static void draw_shaded_text(
Batch2D& batch, const Font& font, const std::wstring& text, int x, int y
Batch2D& batch, Font& font, const std::wstring& text, int x, int y
) {
batch.setColor({0, 0, 0, 1.0f});
font.draw(batch, text, x + 1, y + 1, nullptr, 0);
@ -294,7 +294,7 @@ void SlotView::drawItemInfo(
Batch2D& batch,
const ItemStack& stack,
const ItemDef& item,
const Font& font,
Font& font,
const glm::vec2& pos
) {
const int SLOT_SIZE = InventoryView::SLOT_SIZE;

View file

@ -81,7 +81,7 @@ namespace gui {
Batch2D& batch,
const ItemStack& stack,
const ItemDef& item,
const Font& font,
Font& font,
const glm::vec2& pos
);