mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Improved font rendering interface
This commit is contained in:
@@ -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);;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
};
|
||||
47
src/graphics/font/Textstyle.h
Normal file
47
src/graphics/font/Textstyle.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @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<std::string>{}(fontID);
|
||||
size_t h2 = std::hash<int>{}(fontSize);
|
||||
size_t h3 = std::hash<Uint8>{}(color.r);
|
||||
size_t h4 = std::hash<Uint8>{}(color.g);
|
||||
size_t h5 = std::hash<Uint8>{}(color.b);
|
||||
size_t h6 = std::hash<Uint8>{}(color.a);
|
||||
|
||||
// 组合哈希(使用黄金比例乘法混合)
|
||||
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3) ^ (h5 << 4) ^ (h6 << 5);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user