aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/saffron_api.h2
-rw-r--r--src/saffron_text.c14
-rw-r--r--tests/test_main.c13
3 files changed, 19 insertions, 10 deletions
diff --git a/include/saffron_api.h b/include/saffron_api.h
index 4ab4731..0518dcb 100644
--- a/include/saffron_api.h
+++ b/include/saffron_api.h
@@ -27,7 +27,7 @@ SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y);
SaffronBox* saffron_box_new(SaffronOrientation orientation, SaffronHorizontalAlignment halign, SaffronVerticalAlignment valign, int spacing, int padding, int margin, int width, int height);
void saffron_box_layout(SaffronBox* box);
-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);
void saffron_text_set_text(SaffronText* text, const char* new_text);
#endif
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;
diff --git a/tests/test_main.c b/tests/test_main.c
index c3ea050..39b41f2 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -52,20 +52,21 @@ int main(void) {
test2->h = 72;
test2->draw = my_test_draw;
- /* lunatic method 3 */
- SaffronWidget* test3 = saffron_widget_new();
- test3->w = 200;
- test3->h = 73;
- test3->draw = my_test_draw;
+ SDL_Color color = {255, 255, 255, 255};
+ TTF_Font* font = TTF_OpenFont("/usr/share/fonts/fantasque-sans-mono/FantasqueSansMono-Regular.otf", 24);
+ SaffronText* test3 = saffron_text_new("Mangoes", font, color);
+ /* become lunatic, add custom clickhandler to.. text????????????? */
+ ((SaffronWidget*)test3)->on_click = my_test_onclick;
saffron_widget_add_child(window->root, test);
saffron_widget_add_child(window->root, box);
saffron_widget_add_child(box, test2);
- saffron_widget_add_child(box, test3);
+ saffron_widget_add_child(box, (SaffronWidget*)test3);
saffron_window_main(window);
saffron_window_free(window);
+ TTF_CloseFont(font);
saffron_quit();
return 0;
}