diff options
| -rw-r--r-- | include/saffron.h | 8 | ||||
| -rw-r--r-- | include/saffron_api.h | 4 | ||||
| -rw-r--r-- | include/saffron_theme.h | 25 | ||||
| -rw-r--r-- | include/saffron_widget.h | 2 | ||||
| -rw-r--r-- | meson.build | 3 | ||||
| -rw-r--r-- | src/saffron.c | 9 | ||||
| -rw-r--r-- | src/saffron_button.c | 4 | ||||
| -rw-r--r-- | src/saffron_text.c | 10 | ||||
| -rw-r--r-- | src/saffron_widget.c | 17 | ||||
| -rw-r--r-- | src/saffron_window.c | 2 | ||||
| -rw-r--r-- | tests/test_main.c | 14 |
11 files changed, 88 insertions, 10 deletions
diff --git a/include/saffron.h b/include/saffron.h index 53f27fb..1178a51 100644 --- a/include/saffron.h +++ b/include/saffron.h @@ -4,11 +4,19 @@ #include <SDL3/SDL.h> #include <SDL3_ttf/SDL_ttf.h> +/* Dear LSP, + * I KNOW THESE ARE NOT USED + * PLEASE LEAVE ME ALONE + * Kind regards, + * Arslaan Pathan + * 2026-04-30 +*/ #include "saffron_api.h" #include "saffron_button.h" #include "saffron_text.h" #include "saffron_widget.h" #include "saffron_window.h" #include "saffron_layout.h" +#include "saffron_theme.h" #endif diff --git a/include/saffron_api.h b/include/saffron_api.h index d64b480..8ad340c 100644 --- a/include/saffron_api.h +++ b/include/saffron_api.h @@ -28,10 +28,12 @@ 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, TTF_Font* font, SDL_Color color); +SaffronText* saffron_text_new(const char* text, TTF_Font* font); void saffron_text_set_text(SaffronText* text, const char* new_text); SaffronButton* saffron_button_new(bool enabled, void (*callback)(SaffronButton* self), int w, int h); SaffronButton* saffron_button_new_with_text(SaffronText* text, bool enabled, void (*callback)(SaffronButton* self), int w, int h); +void saffron_widget_set_theme(SaffronWidget* widget, SaffronTheme* theme); + #endif diff --git a/include/saffron_theme.h b/include/saffron_theme.h new file mode 100644 index 0000000..1425a5d --- /dev/null +++ b/include/saffron_theme.h @@ -0,0 +1,25 @@ +#ifndef SAFFRON_THEME_H +#define SAFFRON_THEME_H + +#include <SDL3/SDL.h> + +typedef struct { + int r; + int g; + int b; + int a; +} SaffronColor; + +typedef struct { + SaffronColor bg; + SaffronColor fg; + SaffronColor primary; + SaffronColor secondary; + SaffronColor tertiary; +} SaffronTheme; + +extern const SaffronTheme SAFFRON_DEFAULT_THEME; + +#define SF_MACRO_DEFAULT_THEME ((SaffronTheme*)&SAFFRON_DEFAULT_THEME) + +#endif diff --git a/include/saffron_widget.h b/include/saffron_widget.h index 8b9fc3c..8e7ad12 100644 --- a/include/saffron_widget.h +++ b/include/saffron_widget.h @@ -2,6 +2,7 @@ #define SAFFRON_WIDGET_H #include <SDL3/SDL.h> +#include "saffron_theme.h" typedef enum { SAFFRON_SIZE_FIXED, // keep normal size @@ -28,6 +29,7 @@ typedef struct SaffronWidget { bool pixel_perfect; // Do we scale dynamically (SaffronSizeMode, etc) or use raw x, y, w, h values? If this is true, the SaffronSizeModes from earlier will be ignored. this pretty much tells the engine NOT to layout your widgets, don't expect it to allocate size for it. get better SaffronWidgetType type; void (*free)(struct SaffronWidget* self); + SaffronTheme* theme; } SaffronWidget; #endif diff --git a/meson.build b/meson.build index 9b1e5fa..27420ee 100644 --- a/meson.build +++ b/meson.build @@ -11,7 +11,8 @@ sources = [ 'src/saffron_window.c', 'src/saffron_widget.c', 'src/saffron_layout.c', - 'src/saffron_text.c' + 'src/saffron_text.c', + 'src/saffron_button.c' ] saffron_lib = static_library('saffron', sources, include_directories: inc, dependencies: deps) diff --git a/src/saffron.c b/src/saffron.c index 65816bc..64c7842 100644 --- a/src/saffron.c +++ b/src/saffron.c @@ -4,6 +4,15 @@ #include <SDL3_ttf/SDL_ttf.h> #include <saffron.h> /* meson include directories */ +/* why is this defined in saffron.c, you ask, and not in a separate saffron_theme.c file? because i cant be bothered to make a whole separate .c file for theming which is pretty much mostly headers and some tweaks around the engine */ +const SaffronTheme SAFFRON_DEFAULT_THEME = { + .bg = {30, 30, 46, 255}, + .fg = {205, 214, 244, 255}, + .primary = {137, 180, 250, 255}, + .secondary = {166, 227, 161, 255}, + .tertiary = {203, 166, 247, 255} +}; + bool saffron_init(void) { if (!SDL_Init(SDL_INIT_VIDEO)) { printf("[Saffron] SDL init failed: %s\n", SDL_GetError()); diff --git a/src/saffron_button.c b/src/saffron_button.c index f0cf77e..0fdbd10 100644 --- a/src/saffron_button.c +++ b/src/saffron_button.c @@ -3,7 +3,9 @@ #include <saffron.h> static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) { - // do stuff here + SaffronButton* btn = (SaffronButton*)widget; + + // waiting to finish saffrontheme first } SaffronButton* saffron_button_new(bool enabled, void (*callback)(SaffronButton* self), int width, int height) { diff --git a/src/saffron_text.c b/src/saffron_text.c index 6e7accf..3beb219 100644 --- a/src/saffron_text.c +++ b/src/saffron_text.c @@ -13,7 +13,12 @@ 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); + SaffronTheme* theme = ((SaffronWidget*)text)->theme; + if (!theme) return; + + SaffronColor text_color = theme->primary; + SDL_Color color = {text_color.r, text_color.g, text_color.b, text_color.a}; + SDL_Surface* surface = TTF_RenderText_Blended(text->font, text->text, 0, color); if (!surface) return; SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); @@ -24,7 +29,7 @@ static void saffron_text_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SDL_DestroySurface(surface); } -SaffronText* saffron_text_new(const char* text, TTF_Font* font, SDL_Color color) { +SaffronText* saffron_text_new(const char* text, TTF_Font* font) { SaffronText* text_widget = malloc(sizeof(SaffronText)); if (!text_widget) return NULL; @@ -47,7 +52,6 @@ SaffronText* saffron_text_new(const char* text, TTF_Font* font, SDL_Color color) strcpy(text_widget->text, text); } text_widget->font = font; - text_widget->color = color; return text_widget; } diff --git a/src/saffron_widget.c b/src/saffron_widget.c index 955e265..6cb1664 100644 --- a/src/saffron_widget.c +++ b/src/saffron_widget.c @@ -1,3 +1,5 @@ +#include "saffron_api.h" +#include "saffron_theme.h" #include <SDL3/SDL.h> #include <stdio.h> #include <stdlib.h> @@ -23,6 +25,7 @@ void saffron_widget_init(SaffronWidget* widget) { widget->type = SAFFRON_WIDGET_UNKNOWN; widget->free = NULL; + widget->theme = NULL; } SaffronWidget* saffron_widget_new(void) { @@ -75,6 +78,14 @@ void saffron_widget_add_child(SaffronWidget *parent, SaffronWidget *child) { if (parent->type == SAFFRON_WIDGET_BOX) { saffron_box_layout((SaffronBox*)parent); } + + if (!child->theme) { + if (parent->theme) { + child->theme = parent->theme; /* inherit from parent */ + } else { + child->theme = SF_MACRO_DEFAULT_THEME; + } + } } SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y) { @@ -100,3 +111,9 @@ SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y) { return widget; } + +void saffron_widget_set_theme(SaffronWidget *widget, SaffronTheme *theme) { + if (!theme) return; + if (!widget) return; + widget->theme = theme; +} diff --git a/src/saffron_window.c b/src/saffron_window.c index 5687f16..41bc050 100644 --- a/src/saffron_window.c +++ b/src/saffron_window.c @@ -1,3 +1,4 @@ +#include "saffron_theme.h" #include <SDL3/SDL_render.h> #include <SDL3/SDL_video.h> #include <stdio.h> @@ -17,6 +18,7 @@ SaffronWindow* saffron_window_new(const char* title, int w, int h) { Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; window->sdl_window = SDL_CreateWindow(title, w, h, flags); window->renderer = SDL_CreateRenderer(window->sdl_window, NULL); + window->root->theme = SF_MACRO_DEFAULT_THEME; return window; } diff --git a/tests/test_main.c b/tests/test_main.c index 39b41f2..cb92189 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -1,5 +1,6 @@ #include "saffron_api.h" #include "saffron_layout.h" +#include "saffron_theme.h" #include <stdio.h> #include <saffron.h> #include <SDL3/SDL.h> @@ -11,11 +12,16 @@ * THEY will make draw functions for you * THIS IS A BAD IDEA */ void my_test_draw(SaffronWidget* self, SDL_Renderer* renderer) { - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + /* make use of the theme! */ + SaffronTheme* theme = self->theme; + if (!theme) return; // good luck + SaffronColor secondary = theme->secondary; + SaffronColor tertiary = theme->tertiary; + SDL_SetRenderDrawColor(renderer, secondary.r, secondary.g, secondary.b, secondary.a); SDL_FRect rect = {self->x, self->y, self->w, self->h}; SDL_RenderFillRect(renderer, &rect); - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_SetRenderDrawColor(renderer, tertiary.r, tertiary.g, tertiary.b, tertiary.a); SDL_RenderRect(renderer, &rect); } @@ -33,6 +39,7 @@ int main(void) { /* replace the root with our own, because we want horizontal */ saffron_widget_free(window->root); window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_LEFT, SAFFRON_VALIGN_TOP, 5, 5, 0, window->w, window->h); + window->root->theme = SF_MACRO_DEFAULT_THEME; /* i guess IM THE LUNATIC NOW * DEAL WITH IT */ @@ -52,9 +59,8 @@ int main(void) { test2->h = 72; test2->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); + SaffronText* test3 = saffron_text_new("Mangoes", font); /* become lunatic, add custom clickhandler to.. text????????????? */ ((SaffronWidget*)test3)->on_click = my_test_onclick; |
