diff options
| -rw-r--r-- | include/saffron.h | 3 | ||||
| -rw-r--r-- | include/saffron_api.h | 7 | ||||
| -rw-r--r-- | include/saffron_button.h | 8 | ||||
| -rw-r--r-- | include/saffron_text.h | 15 | ||||
| -rw-r--r-- | include/saffron_widget.h | 1 | ||||
| -rw-r--r-- | src/saffron_layout.c | 1 | ||||
| -rw-r--r-- | src/saffron_text.c | 31 |
7 files changed, 62 insertions, 4 deletions
diff --git a/include/saffron.h b/include/saffron.h index d5cc59f..53f27fb 100644 --- a/include/saffron.h +++ b/include/saffron.h @@ -1,6 +1,9 @@ #ifndef SAFFRON_H #define SAFFRON_H +#include <SDL3/SDL.h> +#include <SDL3_ttf/SDL_ttf.h> + #include "saffron_api.h" #include "saffron_button.h" #include "saffron_text.h" diff --git a/include/saffron_api.h b/include/saffron_api.h index 64251e0..4ab4731 100644 --- a/include/saffron_api.h +++ b/include/saffron_api.h @@ -4,6 +4,10 @@ #include "saffron_widget.h" /* satisfy the lsp and also if someone includes api without the saffron.h wrapper (you lunatic) then it still works */ #include "saffron_window.h" #include "saffron_layout.h" +#include "saffron_text.h" +#include <SDL3/SDL_render.h> +#include <SDL3_ttf/SDL_ttf.h> +#include <SDL3/SDL_pixels.h> bool saffron_init(void); void saffron_quit(void); @@ -23,4 +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); +void saffron_text_set_text(SaffronText* text, const char* new_text); + #endif diff --git a/include/saffron_button.h b/include/saffron_button.h index 3112edf..ab4ed97 100644 --- a/include/saffron_button.h +++ b/include/saffron_button.h @@ -1,11 +1,13 @@ #ifndef SAFFRON_BUTTON_H #define SAFFRON_BUTTON_H +#include "saffron_layout.h" #include "saffron_widget.h" -typedef struct { - SaffronWidget base; // must be first, or we're f**ked - const char* label; +typedef struct SaffronButton { + SaffronBox base; + void (*callback)(struct SaffronButton* self); + bool enabled; } SaffronButton; #endif diff --git a/include/saffron_text.h b/include/saffron_text.h index e69de29..51bfc47 100644 --- a/include/saffron_text.h +++ b/include/saffron_text.h @@ -0,0 +1,15 @@ +#ifndef SAFFRON_TEXT_H +#define SAFFRON_TEXT_H + +#include "saffron_widget.h" +#include <SDL3/SDL_pixels.h> +#include <SDL3_ttf/SDL_ttf.h> + +typedef struct { + SaffronWidget base; + char* text; + TTF_Font* font; + SDL_Color color; +} SaffronText; + +#endif diff --git a/include/saffron_widget.h b/include/saffron_widget.h index 7fc6bc8..ddf4f78 100644 --- a/include/saffron_widget.h +++ b/include/saffron_widget.h @@ -12,6 +12,7 @@ typedef enum { typedef enum { SAFFRON_WIDGET_UNKNOWN, SAFFRON_WIDGET_BOX, + SAFFRON_WIDGET_TEXT } SaffronWidgetType; typedef struct SaffronWidget { diff --git a/src/saffron_layout.c b/src/saffron_layout.c index 725537f..72d915c 100644 --- a/src/saffron_layout.c +++ b/src/saffron_layout.c @@ -2,7 +2,6 @@ #include "saffron_api.h" #include "saffron_widget.h" #include <SDL3/SDL_video.h> -#include <stdio.h> #include <stdlib.h> #include <SDL3/SDL.h> #include <SDL3_ttf/SDL_ttf.h> diff --git a/src/saffron_text.c b/src/saffron_text.c new file mode 100644 index 0000000..8a093ea --- /dev/null +++ b/src/saffron_text.c @@ -0,0 +1,31 @@ +#include <SDL3/SDL_render.h> +#include <saffron.h> +#include <SDL3/SDL.h> +#include <SDL3_ttf/SDL_ttf.h> +#include <stdlib.h> + +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); + + // todo actually do stuff + // and return something + // im tired ill do this later +} |
