#include #include #include #include #include #include static void saffron_text_free(SaffronWidget* self) { free(((SaffronText*)self)->text); } static void saffron_text_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SaffronText* text = (SaffronText*)widget; if (!text->text || !text->font) return; SDL_Surface* surface = TTF_RenderText_Blended(text->font, text->text, 0, text->color); if (!surface) return; SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FRect dest = {widget->x, widget->y, (float)surface->w, (float)surface->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* text_widget = malloc(sizeof(SaffronText)); if (!text_widget) return NULL; saffron_widget_init((SaffronWidget*)text_widget); ((SaffronWidget*)text_widget)->type = SAFFRON_WIDGET_TEXT; ((SaffronWidget*)text_widget)->draw = saffron_text_draw; ((SaffronWidget*)text_widget)->free = saffron_text_free; 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->color = color; return text_widget; }