mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Add the textrender function
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
class FontManager {
|
||||
@@ -14,7 +15,12 @@ public:
|
||||
|
||||
// 获取已加载的字体
|
||||
TTF_Font* getFont(const std::string& key);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
// 用哈希表存储字体
|
||||
std::unordered_map<std::string, TTF_Font*> m_fonts;
|
||||
|
||||
|
||||
};
|
||||
87
src/graphics/font/TextRenderer.cpp
Normal file
87
src/graphics/font/TextRenderer.cpp
Normal file
@@ -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<float>(x), static_cast<float>(y),
|
||||
static_cast<float>(it->second.width),
|
||||
static_cast<float>(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<float>(x), static_cast<float>(y),
|
||||
static_cast<float>(surface->w),
|
||||
static_cast<float>(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);
|
||||
}
|
||||
42
src/graphics/font/TextRenderer.h
Normal file
42
src/graphics/font/TextRenderer.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#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<std::string, 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();
|
||||
|
||||
|
||||
//渲染文本
|
||||
|
||||
void renderText(const std::string& text, const std::string& fontID, int x, int y, SDL_Color color);
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user