From 7067214c34aef91021613b7909dfc9117bf38c36 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Mon, 8 Dec 2025 21:16:20 +0800 Subject: [PATCH] Add TTF support --- src/core/GameApplication.cpp | 5 ++++- src/core/GameApplication.h | 2 +- src/graphics/font/FontManager.cpp | 35 +++++++++++++++++++++++++++++++ src/graphics/font/FontManager.h | 20 ++++++++++++++++++ src/main.cpp | 1 + 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/graphics/font/FontManager.cpp create mode 100644 src/graphics/font/FontManager.h diff --git a/src/core/GameApplication.cpp b/src/core/GameApplication.cpp index 99b8f7c..bd11a97 100644 --- a/src/core/GameApplication.cpp +++ b/src/core/GameApplication.cpp @@ -14,7 +14,10 @@ bool GameApplication::initialize() { SDL_Log("SDL初始化失败: %s", SDL_GetError()); return false; } - + if (!TTF_Init()) { + SDL_Log("无法初始化 SDL_ttf: %s", SDL_GetError()); + return false; + } m_inputManager = std::make_unique(); m_windowManager = std::make_unique(); diff --git a/src/core/GameApplication.h b/src/core/GameApplication.h index c11bd81..9aa2be0 100644 --- a/src/core/GameApplication.h +++ b/src/core/GameApplication.h @@ -5,7 +5,7 @@ #include "scenes/base/SceneManager.h" #include "input/InputManager.h" #include "utils/Config.h" - +#include class GameApplication { private: diff --git a/src/graphics/font/FontManager.cpp b/src/graphics/font/FontManager.cpp new file mode 100644 index 0000000..9a8ca6c --- /dev/null +++ b/src/graphics/font/FontManager.cpp @@ -0,0 +1,35 @@ +#include "FontManager.h" +#include +FontManager::FontManager() { + +} + +FontManager::~FontManager() { + for (auto& pair : m_fonts) { + TTF_CloseFont(pair.second); + } + m_fonts.clear(); +} + +TTF_Font* FontManager::loadFont(const std::string& fontID, int ptSize) { + std::string path = "assets/fonts/" + fontID; + std::string key = fontID + std::to_string(ptSize); + auto it = m_fonts.find(key); + // 检查字体是否已经加载 + if (it != m_fonts.end()) { + return it->second; + } + + TTF_Font* font = TTF_OpenFont(path.c_str(), ptSize); + if (!font) { + throw std::runtime_error("无法加载字体: " + key); + } + m_fonts[key] = font; + return font; +} + +TTF_Font* FontManager::getFont(const std::string& key) { + auto it = m_fonts.find(key); + return (it != m_fonts.end()) ? it->second : nullptr; +} + diff --git a/src/graphics/font/FontManager.h b/src/graphics/font/FontManager.h new file mode 100644 index 0000000..80f46b9 --- /dev/null +++ b/src/graphics/font/FontManager.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include + +class FontManager { +public: + FontManager(); + ~FontManager(); + + // 加载字体(路径 + 字号) + // ptSize 为字号 + TTF_Font* loadFont(const std::string& fontID, int ptSize); + + // 获取已加载的字体 + TTF_Font* getFont(const std::string& key); +private: + // 用哈希表存储字体 + std::unordered_map m_fonts; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8b50112..0980bc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,5 +58,6 @@ SDL_AppResult SDL_AppIterate(void *appstate) { // 4. 应用退出回调(清理资源) void SDL_AppQuit(void *appstate, SDL_AppResult result) { delete static_cast(appstate); + TTF_Quit(); SDL_Quit(); } \ No newline at end of file