Wip: Add basic vector math

This commit is contained in:
Timothy O'Barr
2026-09-29 16:31:02 -05:00
parent 1a69261952
commit 867980c3f9
5 changed files with 107 additions and 14 deletions
+6 -8
View File
@@ -3,31 +3,29 @@
#include <cstdio>
SDL_AppResult Text::OpenFont(fs::path fontPath) {
SDL_AppResult Text::OpenFont(const fs::path &font) {
if (!TTF_Init()) {
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
loadedFont = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f);
loadedFont = TTF_OpenFont(fs::absolute(font).c_str(), 12.0f);
if (!loadedFont) {
printf("Font not found! \n");
return SDL_APP_FAILURE;
}
color = WHITE;
return SDL_APP_CONTINUE;
}
bool Text::SetText(std::string text, Window *window) {
bool Text::SetText(const std::string &text, const Window *win, SDL_FRect rect) {
buffer += text;
SDL_DestroySurface(textRenderingSurface);
textRenderingSurface = TTF_RenderText_Blended(loadedFont, buffer.c_str(), 0, color);
if (textRenderingSurface) {
font_texture = SDL_CreateTextureFromSurface(window->GetRenderer(), textRenderingSurface);
font_texture = SDL_CreateTextureFromSurface(win->GetRenderer(), textRenderingSurface);
SDL_DestroySurface(textRenderingSurface);
}
if (!font_texture) {
@@ -38,7 +36,7 @@ bool Text::SetText(std::string text, Window *window) {
return true;
}
void Text::Render() {
void Text::Render() const {
int w = 0, h = 0;
SDL_FRect dst;
const float scale = 4.0f;
@@ -49,5 +47,5 @@ void Text::Render() {
SDL_GetTextureSize(font_texture, &dst.w, &dst.h);
dst.x = ((w / scale) - dst.w) / 2;
dst.y = ((h / scale) - dst.h) / 2;
SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst);
SDL_RenderTexture(window.GetRenderer(), font_texture, nullptr, &dst);
}
+5 -5
View File
@@ -19,7 +19,7 @@ private:
TTF_Font *loadedFont = nullptr;
SDL_Surface *textRenderingSurface = nullptr;
SDL_Color color;
SDL_Color color = {};
SDL_Texture *font_texture = nullptr;
@@ -28,8 +28,8 @@ private:
public:
SDL_AppResult OpenFont(fs::path fontPath);
bool SetText(std::string text, Window *window);
void Render();
void CloseFont() { TTF_CloseFont(loadedFont); }
SDL_AppResult OpenFont(const fs::path &font);
bool SetText(const std::string &text, const Window *win, SDL_FRect rect);
void Render() const;
void CloseFont() const { TTF_CloseFont(loadedFont); }
};