diff options
| author | Arslaan Pathan <[email protected]> | 2026-04-08 13:41:35 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2026-04-08 13:41:35 +1200 |
| commit | c9660a840256308d4dde40613a1af3296d3fdead (patch) | |
| tree | 13e3049ea6bc6d2606bffeced7dc1ead990a1169 /src | |
| parent | 04ff8c0a6acc2cc9204492690b7f99f892ed439a (diff) | |
| download | saffron-c9660a840256308d4dde40613a1af3296d3fdead.tar.xz saffron-c9660a840256308d4dde40613a1af3296d3fdead.zip | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/saffron.c | 34 | ||||
| -rw-r--r-- | src/saffron_internal.h | 10 | ||||
| -rw-r--r-- | src/saffron_widget.c | 36 | ||||
| -rw-r--r-- | src/saffron_window.c | 24 |
4 files changed, 95 insertions, 9 deletions
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 <stdio.h> +#include <stdbool.h> +#include <stdlib.h> #include <SDL3/SDL.h> #include <SDL3_ttf/SDL_ttf.h> -#include <SDL3/SDL_main.h> +#include <saffron.h> /* 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 <SDL3/SDL.h> +#include <stdlib.h> +#include <saffron_widget.h> +#include <saffron_api.h> +#include <saffron_window.h> + +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 <stdlib.h> +#include <SDL3/SDL.h> +#include <SDL3_ttf/SDL_ttf.h> +#include <saffron_api.h> +#include <saffron_widget.h> +#include <saffron_window.h> + +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); +} |
