diff --git a/src/graphics/font/FontManager.cpp b/src/graphics/font/FontManager.cpp index 7fe4ad6..29066e9 100644 --- a/src/graphics/font/FontManager.cpp +++ b/src/graphics/font/FontManager.cpp @@ -31,9 +31,13 @@ TTF_Font* FontManager::loadFont(const std::string& fontID, int ptSize) { return font; } -TTF_Font* FontManager::getFont(const std::string& key) { +TTF_Font* FontManager::getFont(const std::string& key, int ptSize) { auto it = m_fonts.find(key); - return (it != m_fonts.end()) ? it->second : nullptr; + if (it != m_fonts.end()) { + return it->second; + } + + return loadFont(key, ptSize);; } diff --git a/src/graphics/font/FontManager.h b/src/graphics/font/FontManager.h index ae0c51d..ddaaa58 100644 --- a/src/graphics/font/FontManager.h +++ b/src/graphics/font/FontManager.h @@ -14,7 +14,7 @@ public: TTF_Font* loadFont(const std::string& fontID, int ptSize); // 获取已加载的字体 - TTF_Font* getFont(const std::string& key); + TTF_Font* getFont(const std::string& key, int ptSize); diff --git a/src/graphics/font/TextRenderer.cpp b/src/graphics/font/TextRenderer.cpp index 962cc06..b3e0290 100644 --- a/src/graphics/font/TextRenderer.cpp +++ b/src/graphics/font/TextRenderer.cpp @@ -1,6 +1,6 @@ #include "TextRenderer.h" -TextRenderer::TextRenderer(FontManager& fontManager,SDL_Renderer* renderer) : +TextRenderer::TextRenderer(SDL_Renderer* renderer, FontManager* fontManager) : m_fontManager(fontManager), m_renderer(renderer) { @@ -18,8 +18,8 @@ TextRenderer::~TextRenderer() { -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); +void TextRenderer::renderText(const std::string& text, TextStyle style, int x, int y) { + auto key = style.hash(); auto it = m_cache.find(key); // 查找缓存 @@ -35,13 +35,13 @@ void TextRenderer::renderText(const std::string& text, const std::string& fontID } // 创建新的纹理 - TTF_Font* font = m_fontManager.getFont(fontID); + TTF_Font* font = m_fontManager->getFont(style.fontID, style.fontSize); if (!font) { - SDL_Log("错误:字体未找到 %s\n", fontID.c_str()); + SDL_Log("错误:字体未找到 %s\n", style.fontID.c_str()); return; } // 创建文字表面 - SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(),text.length(), color); + SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(),text.length(), style.color); if (!surface) { printf("错误:无法创建文字表面\n"); return; @@ -74,7 +74,7 @@ void TextRenderer::renderText(const std::string& text, const std::string& fontID } SDL_Texture* TextRenderer::createTextTexture(const std::string& text, const std::string& fontID, SDL_Color color) { - + return nullptr; } diff --git a/src/graphics/font/TextRenderer.h b/src/graphics/font/TextRenderer.h index 86b9b7d..d212895 100644 --- a/src/graphics/font/TextRenderer.h +++ b/src/graphics/font/TextRenderer.h @@ -3,13 +3,13 @@ #include #include #include - +#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 m_cache; + + + + 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(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); }; \ No newline at end of file diff --git a/src/graphics/font/Textstyle.h b/src/graphics/font/Textstyle.h new file mode 100644 index 0000000..2dffc0f --- /dev/null +++ b/src/graphics/font/Textstyle.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include + +/** + * @brief 文本样式定义结构体 + * + * 封装字体、大小、颜色等文本渲染属性 + * 用于统一管理文本外观 + */ +struct TextStyle { + std::string fontID; ///< 字体标识符(在FontManager中注册的ID) + int fontSize = 16; ///< 字体大小(像素) + SDL_Color color = {255, 255, 255, 255}; ///< 文本颜色(RGBA) + + /** + * @brief 比较两个样式是否相等(用于缓存查找) + * @param other 另一个TextStyle对象 + * @return true 如果所有属性都相等 + */ + bool operator==(const TextStyle& other) const { + return fontID == other.fontID && + fontSize == other.fontSize && + color.r == other.color.r && + color.g == other.color.g && + color.b == other.color.b && + color.a == other.color.a; + } + + /** + * @brief 计算样式的哈希值(用于unordered_map) + * @return size_t 哈希值 + */ + size_t hash() const { + // 使用组合哈希技术 + size_t h1 = std::hash{}(fontID); + size_t h2 = std::hash{}(fontSize); + size_t h3 = std::hash{}(color.r); + size_t h4 = std::hash{}(color.g); + size_t h5 = std::hash{}(color.b); + size_t h6 = std::hash{}(color.a); + + // 组合哈希(使用黄金比例乘法混合) + return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3) ^ (h5 << 4) ^ (h6 << 5); + } +}; \ No newline at end of file