Updated game structure
This commit is contained in:
21
src/graphics/Font.cpp
Normal file
21
src/graphics/Font.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
// Font.cpp - implementation of FontAtlas (copied into src/graphics)
|
||||
#include "graphics/Font.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
bool FontAtlas::init(const std::string& path, int basePt) { fontPath = path; baseSize = basePt; return true; }
|
||||
|
||||
void FontAtlas::shutdown() { for (auto &kv : cache) if (kv.second) TTF_CloseFont(kv.second); cache.clear(); }
|
||||
|
||||
TTF_Font* FontAtlas::getSized(int ptSize) {
|
||||
auto it = cache.find(ptSize); if (it!=cache.end()) return it->second;
|
||||
TTF_Font* f = TTF_OpenFont(fontPath.c_str(), ptSize);
|
||||
if (!f) return nullptr; cache[ptSize] = f; return f;
|
||||
}
|
||||
|
||||
void FontAtlas::draw(SDL_Renderer* r, float x, float y, const std::string& text, float scale, SDL_Color color) {
|
||||
if (scale <= 0) return; int pt = int(baseSize * scale); if (pt < 8) pt = 8; TTF_Font* f = getSized(pt); if (!f) return;
|
||||
SDL_Surface* surf = TTF_RenderText_Blended(f, text.c_str(), text.length(), color); if (!surf) return;
|
||||
SDL_Texture* tex = SDL_CreateTextureFromSurface(r, surf);
|
||||
if (tex) { SDL_FRect dst{ x, y, (float)surf->w, (float)surf->h }; SDL_RenderTexture(r, tex, nullptr, &dst); SDL_DestroyTexture(tex); }
|
||||
SDL_DestroySurface(surf);
|
||||
}
|
||||
Reference in New Issue
Block a user