From 04ff8c0a6acc2cc9204492690b7f99f892ed439a Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Tue, 7 Apr 2026 11:13:23 +1200 Subject: Refactor things, and start implementing API widgets --- .gitignore | 1 + include/saffron.h | 9 +++++++++ include/saffron_button.h | 11 ++++++++++ include/saffron_text.h | 0 include/saffron_widget.h | 15 ++++++++++++++ include/saffron_window.h | 0 meson.build | 6 +++++- src/main.c | 48 -------------------------------------------- src/saffron.c | 13 ++++++++++++ tests/test_main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 106 insertions(+), 49 deletions(-) create mode 100644 include/saffron.h create mode 100644 include/saffron_button.h create mode 100644 include/saffron_text.h create mode 100644 include/saffron_widget.h create mode 100644 include/saffron_window.h delete mode 100644 src/main.c create mode 100644 src/saffron.c create mode 100644 tests/test_main.c diff --git a/.gitignore b/.gitignore index 378eac2..9785597 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build +.cache diff --git a/include/saffron.h b/include/saffron.h new file mode 100644 index 0000000..8682ac4 --- /dev/null +++ b/include/saffron.h @@ -0,0 +1,9 @@ +#ifndef SAFFRON_H +#define SAFFRON_H + +#include "saffron_button.h" +#include "saffron_text.h" +#include "saffron_widget.h" +#include "saffron_window.h" + +#endif diff --git a/include/saffron_button.h b/include/saffron_button.h new file mode 100644 index 0000000..3112edf --- /dev/null +++ b/include/saffron_button.h @@ -0,0 +1,11 @@ +#ifndef SAFFRON_BUTTON_H +#define SAFFRON_BUTTON_H + +#include "saffron_widget.h" + +typedef struct { + SaffronWidget base; // must be first, or we're f**ked + const char* label; +} SaffronButton; + +#endif diff --git a/include/saffron_text.h b/include/saffron_text.h new file mode 100644 index 0000000..e69de29 diff --git a/include/saffron_widget.h b/include/saffron_widget.h new file mode 100644 index 0000000..6d7b6bd --- /dev/null +++ b/include/saffron_widget.h @@ -0,0 +1,15 @@ +#ifndef SAFFRON_WIDGET_H +#define SAFFRON_WIDGET_H + +#include + +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; + +#endif diff --git a/include/saffron_window.h b/include/saffron_window.h new file mode 100644 index 0000000..e69de29 diff --git a/meson.build b/meson.build index ce5edc9..2602bff 100644 --- a/meson.build +++ b/meson.build @@ -4,4 +4,8 @@ deps = [] deps += dependency('sdl3', static: true) deps += dependency('sdl3-ttf', static: true) -executable('saffron', 'src/main.c', dependencies: deps) +saffron_lib = static_library('saffron', 'src/saffron.c', dependencies: deps) + +saffron_dep = declare_dependency(link_with: saffron_lib, include_directories: include_directories('include'), dependencies: deps) + +executable('saffron_test', 'tests/test_main.c', dependencies: saffron_dep) diff --git a/src/main.c b/src/main.c deleted file mode 100644 index f286a0a..0000000 --- a/src/main.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include - -int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_VIDEO); - TTF_Init(); - - SDL_Window *window = SDL_CreateWindow("Saffron", 800, 600, 0); - SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); - - TTF_Font *font = TTF_OpenFont("/usr/share/fonts/maple-mono/MapleMonoNL-Regular.ttf", 20); // "works on my machine" - Arslaan, 2026 - - bool running = true; - SDL_Event event; - SDL_FRect rect; - rect.x = rect.y = 100; - rect.w = rect.h = 100; - - SDL_FRect text_rect = {200, 200, 0, 0}; - SDL_Color white = {255, 255, 255, 255}; - SDL_Surface *surface = TTF_RenderText_Blended(font, "Hello Saffron!", 0, white); - SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); - SDL_DestroySurface(surface); - SDL_GetTextureSize(texture, &text_rect.w, &text_rect.h); - - while (running) { - while (SDL_PollEvent(&event)) { - if (event.type == SDL_EVENT_QUIT) { - running = false; - } - } - - SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); - SDL_RenderClear(renderer); - SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); - SDL_RenderFillRect(renderer, &rect); - SDL_RenderTexture(renderer, texture, NULL, &text_rect); - SDL_RenderPresent(renderer); - } - - TTF_CloseFont(font); - TTF_Quit(); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; -} diff --git a/src/saffron.c b/src/saffron.c new file mode 100644 index 0000000..dc6e470 --- /dev/null +++ b/src/saffron.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +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; diff --git a/tests/test_main.c b/tests/test_main.c new file mode 100644 index 0000000..e24d17f --- /dev/null +++ b/tests/test_main.c @@ -0,0 +1,52 @@ +/* this doesnt really test saffron's api right now + * because i need to actually implement stuff + * but its good refactor for now +*/ +#include +#include +#include + +int main(int argc, char *argv[]) { + SDL_Init(SDL_INIT_VIDEO); + TTF_Init(); + + SDL_Window *window = SDL_CreateWindow("saffron", 800, 600, 0); + SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); + + TTF_Font *font = TTF_OpenFont("/usr/share/fonts/maple-mono/MapleMonoNL-Regular.ttf", 20); // "works on my machine" - Arslaan, 2026 + + bool running = true; + SDL_Event event; + SDL_FRect rect; + rect.x = rect.y = 100; + rect.w = rect.h = 100; + + SDL_FRect text_rect = {200, 200, 0, 0}; + SDL_Color white = {255, 255, 255, 255}; + SDL_Surface *surface = TTF_RenderText_Blended(font, "hello, saffron!", 0, white); + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_DestroySurface(surface); + SDL_GetTextureSize(texture, &text_rect.w, &text_rect.h); + + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT) { + running = false; + } + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(renderer, &rect); + SDL_RenderTexture(renderer, texture, NULL, &text_rect); + SDL_RenderPresent(renderer); + } + + TTF_CloseFont(font); + TTF_Quit(); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} -- cgit v1.2.3