From f6268951b7b2e89ceedf31c8b63ff06d5a10d86c Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Tue, 9 Dec 2025 22:46:37 +0800 Subject: [PATCH] Add the textrender function --- src/graphics/font/FontManager.cpp | 5 ++ src/graphics/font/FontManager.h | 6 +++ src/graphics/font/TextRenderer.cpp | 87 ++++++++++++++++++++++++++++++ src/graphics/font/TextRenderer.h | 42 +++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 src/graphics/font/TextRenderer.cpp create mode 100644 src/graphics/font/TextRenderer.h diff --git a/src/graphics/font/FontManager.cpp b/src/graphics/font/FontManager.cpp index 9a8ca6c..7fe4ad6 100644 --- a/src/graphics/font/FontManager.cpp +++ b/src/graphics/font/FontManager.cpp @@ -8,7 +8,10 @@ FontManager::~FontManager() { for (auto& pair : m_fonts) { TTF_CloseFont(pair.second); } + m_fonts.clear(); + + } TTF_Font* FontManager::loadFont(const std::string& fontID, int ptSize) { @@ -33,3 +36,5 @@ TTF_Font* FontManager::getFont(const std::string& key) { return (it != m_fonts.end()) ? it->second : nullptr; } + + diff --git a/src/graphics/font/FontManager.h b/src/graphics/font/FontManager.h index 80f46b9..ae0c51d 100644 --- a/src/graphics/font/FontManager.h +++ b/src/graphics/font/FontManager.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include class FontManager { @@ -14,7 +15,12 @@ public: // 获取已加载的字体 TTF_Font* getFont(const std::string& key); + + + private: // 用哈希表存储字体 std::unordered_map m_fonts; + + }; \ No newline at end of file diff --git a/src/graphics/font/TextRenderer.cpp b/src/graphics/font/TextRenderer.cpp new file mode 100644 index 0000000..962cc06 --- /dev/null +++ b/src/graphics/font/TextRenderer.cpp @@ -0,0 +1,87 @@ +#include "TextRenderer.h" + +TextRenderer::TextRenderer(FontManager& fontManager,SDL_Renderer* renderer) : + m_fontManager(fontManager), + m_renderer(renderer) +{ + +} + +TextRenderer::~TextRenderer() { + for (auto& pair : m_cache) { + + SDL_DestroyTexture(pair.second.texture); + + } + m_cache.clear(); +} + + + +void TextRenderer::renderText(const std::string& text, const std::string& fontID, int x, int y, SDL_Color color) { + std::string key = makeCacheKey(text, fontID, color); + auto it = m_cache.find(key); + + // 查找缓存 + if (it != m_cache.end()) { + // 使用缓存的纹理, SDL_FRect为浮点数矩形 + SDL_FRect dest = { static_cast(x), static_cast(y), + static_cast(it->second.width), + static_cast(it->second.height) }; + + // 绘制材质 NULL 的含义:绘制整个纹理(从 (0,0) 到纹理的完整宽高) &dest 目标区域 + SDL_RenderTexture(m_renderer, it->second.texture, NULL, &dest); + return; + } + + // 创建新的纹理 + TTF_Font* font = m_fontManager.getFont(fontID); + if (!font) { + SDL_Log("错误:字体未找到 %s\n", fontID.c_str()); + return; + } + // 创建文字表面 + SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(),text.length(), color); + if (!surface) { + printf("错误:无法创建文字表面\n"); + return; + } + // 创建纹理 + SDL_Texture* texture = SDL_CreateTextureFromSurface(m_renderer, surface); + if (!texture) { + SDL_Log("错误:无法创建纹理\n"); + SDL_DestroySurface(surface); + return; + } + // 保存到缓存 + CachedText cached; + cached.texture = texture; + cached.width = surface->w; + cached.height = surface->h; + m_cache[key] = cached; + + + // 渲染 + SDL_FRect dest = { static_cast(x), static_cast(y), + static_cast(surface->w), + static_cast(surface->h) }; + SDL_RenderTexture(m_renderer, texture, NULL, &dest); + + // 清理表面 + SDL_DestroySurface(surface); + + +} + +SDL_Texture* TextRenderer::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) { + return text + "|" + fontID + "|" + + std::to_string(color.r) + "-" + + std::to_string(color.g) + "-" + + std::to_string(color.b) + "-" + + std::to_string(color.a); + } diff --git a/src/graphics/font/TextRenderer.h b/src/graphics/font/TextRenderer.h new file mode 100644 index 0000000..86b9b7d --- /dev/null +++ b/src/graphics/font/TextRenderer.h @@ -0,0 +1,42 @@ +#pragma once +#include +#include +#include +#include + +#include "FontManager.h" + +class TextRenderer { +private: + SDL_Renderer* m_renderer; + FontManager& m_fontManager; + + // 缓存文字纹理 + + + struct CachedText { + SDL_Texture* texture; + int width; + int height; + }; + + std::unordered_map 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(); + + + //渲染文本 + + void renderText(const std::string& text, const std::string& fontID, int x, int y, SDL_Color color); + +}; \ No newline at end of file