Improved font rendering interface

This commit is contained in:
2025-12-11 16:44:53 +08:00
parent f6268951b7
commit 7b098e0542
5 changed files with 70 additions and 17 deletions

View File

@@ -3,13 +3,13 @@
#include <unordered_map>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "Textstyle.h"
#include "FontManager.h"
class TextRenderer {
private:
SDL_Renderer* m_renderer;
FontManager& m_fontManager;
FontManager* m_fontManager;
// 缓存文字纹理
@@ -20,23 +20,25 @@ private:
int height;
};
std::unordered_map<std::string, CachedText> m_cache;
std::unordered_map<size_t, CachedText> m_cache;
// 创建材质
SDL_Texture* createTextTexture(const std::string& text, const std::string& fontID, SDL_Color color);
// 创建一个唯一的缓存键:文本 + 字体 + 颜色
std::string makeCacheKey(const std::string& text, const std::string& fontID, SDL_Color color);
public:
TextRenderer(FontManager& fontManager, SDL_Renderer* renderer);
TextRenderer(SDL_Renderer* renderer, FontManager* fontManager);
~TextRenderer();
//渲染文本
void renderText(const std::string& text, const std::string& fontID, int x, int y, SDL_Color color);
void renderText(const std::string& text, TextStyle style, int x, int y);
};