diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/saffron_text.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/saffron_text.c b/src/saffron_text.c index e2ac850..6e7accf 100644 --- a/src/saffron_text.c +++ b/src/saffron_text.c @@ -17,14 +17,14 @@ static void saffron_text_draw(SaffronWidget* widget, SDL_Renderer* renderer) { if (!surface) return; SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); - SDL_FRect dest = {widget->x, widget->y, (float)surface->w, (float)surface->h}; + SDL_FRect dest = {widget->x, widget->y, (float)widget->w, (float)widget->h}; SDL_RenderTexture(renderer, texture, NULL, &dest); SDL_DestroyTexture(texture); SDL_DestroySurface(surface); } -SaffronText* saffron_text_new(const char* text, int font_size, SDL_Color color) { +SaffronText* saffron_text_new(const char* text, TTF_Font* font, SDL_Color color) { SaffronText* text_widget = malloc(sizeof(SaffronText)); if (!text_widget) return NULL; @@ -34,11 +34,19 @@ SaffronText* saffron_text_new(const char* text, int font_size, SDL_Color color) ((SaffronWidget*)text_widget)->draw = saffron_text_draw; ((SaffronWidget*)text_widget)->free = saffron_text_free; + int w, h; + if (!TTF_GetStringSize(font, text, 0, &w, &h)) { + w = 0; + h = 0; + } + ((SaffronWidget*)text_widget)->w = w; + ((SaffronWidget*)text_widget)->h = h; + text_widget->text = malloc(strlen(text) + 1); if (text_widget->text) { strcpy(text_widget->text, text); } - text_widget->font = NULL; // TODO initialize a font here + text_widget->font = font; text_widget->color = color; return text_widget; |
