feat added entities and components and added failing textcomponent

This commit is contained in:
2025-07-14 00:14:45 +02:00
parent d04c0a6e15
commit f2958fa9fa
98 changed files with 4006 additions and 180 deletions

View File

@ -0,0 +1,48 @@
#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() {
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
}
}