diff options
| -rw-r--r-- | include/saffron_api.h | 20 | ||||
| -rw-r--r-- | include/saffron_window.h | 15 | ||||
| -rw-r--r-- | meson.build | 25 | ||||
| -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 |
7 files changed, 150 insertions, 14 deletions
diff --git a/include/saffron_api.h b/include/saffron_api.h new file mode 100644 index 0000000..a993ae6 --- /dev/null +++ b/include/saffron_api.h @@ -0,0 +1,20 @@ +#ifndef SAFFRON_API_H +#define SAFFRON_API_H + +#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" + +bool saffron_init(void); +void saffron_quit(void); + +void saffron_widget_draw(SaffronWidget* widget, SDL_Renderer *renderer); +void saffron_widget_add_child(SaffronWidget* parent, SaffronWidget* child); +SaffronWidget* saffron_widget_new(); /* generic primitive for generic widgets, e.g. the window root. ideally window root is a hbox/vbox/box once those are implemented */ +void saffron_widget_free(SaffronWidget* widget); + +SaffronWindow* saffron_window_new(const char* title, int w, int h); +void saffron_window_free(SaffronWindow* window); +void saffron_window_show(SaffronWindow* window); +void saffron_window_main(SaffronWindow* window); + +#endif diff --git a/include/saffron_window.h b/include/saffron_window.h index e69de29..80aaa39 100644 --- a/include/saffron_window.h +++ b/include/saffron_window.h @@ -0,0 +1,15 @@ +#ifndef SAFFRON_WINDOW_H +#define SAFFRON_WINDOW_H + +#include <SDL3/SDL.h> +#include "saffron_widget.h" + +typedef struct { + SaffronWidget *root; + SDL_Window *sdl_window; + SDL_Renderer *renderer; + int w, h; + const char* title; +} SaffronWindow; + +#endif diff --git a/meson.build b/meson.build index 2602bff..e53b388 100644 --- a/meson.build +++ b/meson.build @@ -1,11 +1,26 @@ project('saffron', 'c') deps = [] -deps += dependency('sdl3', static: true) -deps += dependency('sdl3-ttf', static: true) +deps += dependency('sdl3') +deps += dependency('sdl3-ttf') -saffron_lib = static_library('saffron', 'src/saffron.c', dependencies: deps) +inc = include_directories('include') -saffron_dep = declare_dependency(link_with: saffron_lib, include_directories: include_directories('include'), dependencies: deps) +sources = [ + 'src/saffron.c', + 'src/saffron_window.c', + 'src/saffron_widget.c', + # TODO add more stuff like text/button +] -executable('saffron_test', 'tests/test_main.c', dependencies: saffron_dep) +saffron_lib = static_library('saffron', sources, include_directories: inc, dependencies: deps) + +test_deps = [] +test_deps += dependency('sdl3', static: true) +test_deps += dependency('sdl3-ttf', static: true) + +saffron_dep = declare_dependency(link_with: saffron_lib, include_directories: inc, dependencies: deps) + +test_deps += saffron_dep + +executable('saffron_test', 'tests/test_main.c', dependencies: test_deps) 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); +} |
