From c9660a840256308d4dde40613a1af3296d3fdead Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Wed, 8 Apr 2026 13:41:35 +1200 Subject: what (i cannot put the commit message into words) I added a lot of stuff, it dont really work fully yet but its very good stuff --- src/saffron.c | 34 +++++++++++++++++++++++++--------- src/saffron_internal.h | 10 ++++++++++ src/saffron_widget.c | 36 ++++++++++++++++++++++++++++++++++++ src/saffron_window.c | 24 ++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 src/saffron_internal.h create mode 100644 src/saffron_widget.c create mode 100644 src/saffron_window.c (limited to 'src') diff --git a/src/saffron.c b/src/saffron.c index dc6e470..8724101 100644 --- a/src/saffron.c +++ b/src/saffron.c @@ -1,13 +1,29 @@ #include +#include +#include #include #include -#include +#include /* meson include directories */ -typedef struct SaffronWidget { - int x, y, w, h; - void (*draw)(struct SaffronWidget *self, SDL_Renderer *renderer); - void (*on_click)(struct SaffronWidget *self); - struct SaffronWidget *parent; - struct SaffronWidget **children; - int child_count; -} SaffronWidget; +bool saffron_init(void) { + if (!SDL_Init(SDL_INIT_VIDEO)) { + printf("[saffron] SDL init failed: %s\n", SDL_GetError()); + return false; + } + if (!TTF_Init()) { + printf("[saffron] TTF init failed: %s\n", SDL_GetError()); + return false; + } + return true; +} + +void saffron_widget_draw(SaffronWidget* widget, SDL_Renderer *renderer) { + // do stuff later +} + +void saffron_widget_add_child(SaffronWidget *parent, SaffronWidget *child) { + child->parent = parent; + parent->children = realloc(parent->children, sizeof(SaffronWidget*) * (parent->child_count + 1)); + parent->children[parent->child_count] = child; + parent->child_count++; +} diff --git a/src/saffron_internal.h b/src/saffron_internal.h new file mode 100644 index 0000000..61d4195 --- /dev/null +++ b/src/saffron_internal.h @@ -0,0 +1,10 @@ +/* we could use #pragma once, but we don't because BACK IN MY DAY WE DIDNT HAVE PRAGMA ONCE!!! + * and also it's not in the C standard, if some lunatic compiles it with a custom standards-comformant compiler this is undefined behaviour + * therefore, my response is TOO BAD SO SAD */ +#ifndef SAFFRON_INTERNAL_H +#define SAFFRON_INTERNAL_H + +extern SaffronWindow **windows; +extern int window_count; + +#endif diff --git a/src/saffron_widget.c b/src/saffron_widget.c new file mode 100644 index 0000000..089c040 --- /dev/null +++ b/src/saffron_widget.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +SaffronWidget* saffron_widget_new() { + /* This function is a generic primitive for creating widgets. You wouldn't want to do this manually unless you're a lunatic. It is meant to be wrapped around by other functions that change the default parameters, for example, what sane person makes a widget 0x0x0x0? you LUNATIC! */ + SaffronWidget* widget = malloc(sizeof(SaffronWidget)); + + widget->x = 0; + widget->y = 0; + widget->w = 0; + widget->h = 0; + + widget->draw = NULL; + widget->on_click = NULL; + + widget->parent = NULL; + widget->children = NULL; + widget->child_count = 0; + + return widget; +} + +void saffron_widget_free(SaffronWidget *widget) { + /* should probably check if widget is NULL here + * oh well, too bad so sad. not my problem if the caller + * doesn't check and causes undefined behaviour. */ + for (int i = 0; i < widget->child_count; i++) { + saffron_widget_free(widget->children[i]); + } + + free(widget->children); + free(widget); +} diff --git a/src/saffron_window.c b/src/saffron_window.c new file mode 100644 index 0000000..10adfce --- /dev/null +++ b/src/saffron_window.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include + +SaffronWindow* saffron_window_new(const char* title, int w, int h) { + SaffronWindow* window = malloc(sizeof(SaffronWindow)); + window->root = saffron_widget_new(); // frick, need to implement this. + window->title = title; + window->w = w; + window->h = h; + window->sdl_window = SDL_CreateWindow(title, w, h, SDL_WINDOW_RESIZABLE); + window->renderer = SDL_CreateRenderer(window->sdl_window, NULL); + return window; +} + +void saffron_window_free(SaffronWindow* window) { + SDL_DestroyRenderer(window->renderer); + SDL_DestroyWindow(window->sdl_window); + saffron_widget_free(window->root); + free(window); +} -- cgit v1.2.3