emptied textcomponent for rewriting
This commit is contained in:
@ -1,48 +1,20 @@
|
||||
#include "TextComponent.hpp"
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
TextComponent::TextComponent(const std::string& text, const std::string& fontPath, int fontSize, SDL_Color color)
|
||||
: text(text), fontPath(fontPath), fontSize(fontSize), color(color) {
|
||||
TextComponent::TextComponent(const std::string& text, const std::string& fontPath, int fontSize, SDL_Color color) : text(text), fontPath(fontPath), fontSize(fontSize), color(color) {
|
||||
}
|
||||
|
||||
TextComponent::~TextComponent() {
|
||||
if (texture) SDL_DestroyTexture(texture);
|
||||
if (font) TTF_CloseFont(font);
|
||||
}
|
||||
|
||||
void TextComponent::Init(SDL_Renderer* renderer) {
|
||||
font = TTF_OpenFont(fontPath.c_str(), fontSize);
|
||||
if (!font) {
|
||||
SDL_Log("Failed to load font: %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: implement a way to resize
|
||||
SDL_Surface* surface = TTF_RenderText_Blended(font, text.c_str(), 10, color);
|
||||
if (!surface) {
|
||||
SDL_Log("Failed to render text surface: %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
destRect = { 0, 0, (float)surface->w, (float)surface->h };
|
||||
|
||||
SDL_DestroySurface(surface);
|
||||
}
|
||||
|
||||
void TextComponent::Update(float dt) {
|
||||
// nothing dynamic for now
|
||||
}
|
||||
|
||||
void TextComponent::Render(SDL_Renderer* renderer) {
|
||||
if (texture) {
|
||||
SDL_RenderTexture(renderer, texture, nullptr, &destRect);
|
||||
}
|
||||
}
|
||||
|
||||
void TextComponent::SetText(const std::string& newText) {
|
||||
if (newText != text) {
|
||||
text = newText;
|
||||
// Re-render in Init() or a helper function you could create
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user