diff options
| -rw-r--r-- | include/saffron.h | 1 | ||||
| -rw-r--r-- | src/saffron.c | 11 | ||||
| -rw-r--r-- | src/saffron_internal.h | 10 | ||||
| -rw-r--r-- | src/saffron_widget.c | 31 | ||||
| -rw-r--r-- | src/saffron_window.c | 24 | ||||
| -rw-r--r-- | tests/test_main.c | 96 |
6 files changed, 102 insertions, 71 deletions
diff --git a/include/saffron.h b/include/saffron.h index 8682ac4..aae8086 100644 --- a/include/saffron.h +++ b/include/saffron.h @@ -1,6 +1,7 @@ #ifndef SAFFRON_H #define SAFFRON_H +#include "saffron_api.h" #include "saffron_button.h" #include "saffron_text.h" #include "saffron_widget.h" diff --git a/src/saffron.c b/src/saffron.c index 8724101..e9f9ca6 100644 --- a/src/saffron.c +++ b/src/saffron.c @@ -17,13 +17,8 @@ bool saffron_init(void) { return true; } -void saffron_widget_draw(SaffronWidget* widget, SDL_Renderer *renderer) { - // do stuff later +void saffron_quit(void) { + TTF_Quit(); + SDL_Quit(); } -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 deleted file mode 100644 index 61d4195..0000000 --- a/src/saffron_internal.h +++ /dev/null @@ -1,10 +0,0 @@ -/* 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 index 089c040..85b2013 100644 --- a/src/saffron_widget.c +++ b/src/saffron_widget.c @@ -4,7 +4,7 @@ #include <saffron_api.h> #include <saffron_window.h> -SaffronWidget* saffron_widget_new() { +SaffronWidget* saffron_widget_new(void) { /* 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)); @@ -24,9 +24,12 @@ SaffronWidget* saffron_widget_new() { } 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. */ + /* follow-up to my previous comment: + * yeah it is the caller's responsibility to check + * for undefined behaviour but this one line will save + * so much time down the line */ + if (!widget) return; + for (int i = 0; i < widget->child_count; i++) { saffron_widget_free(widget->children[i]); } @@ -34,3 +37,23 @@ void saffron_widget_free(SaffronWidget *widget) { free(widget->children); free(widget); } + +void saffron_widget_draw(SaffronWidget* widget, SDL_Renderer *renderer) { + if (!widget) return; + + if (widget->draw) { + widget->draw(widget, renderer); + } + + for (int i = 0; i < widget->child_count; i++) { + saffron_widget_draw(widget->children[i], renderer); + } +} + +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_window.c b/src/saffron_window.c index 10adfce..4bdae76 100644 --- a/src/saffron_window.c +++ b/src/saffron_window.c @@ -1,3 +1,4 @@ +#include <SDL3/SDL_events.h> #include <stdlib.h> #include <SDL3/SDL.h> #include <SDL3_ttf/SDL_ttf.h> @@ -22,3 +23,26 @@ void saffron_window_free(SaffronWindow* window) { saffron_widget_free(window->root); free(window); } + +void saffron_window_main(SaffronWindow *window) { + if (!window) return; + + bool running = true; + SDL_Event event; + + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT) { + running = false; + } + // TODO: send events to widgets and stuff + } + + SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255); + SDL_RenderClear(window->renderer); + saffron_widget_draw(window->root, window->renderer); + SDL_RenderPresent(window->renderer); + + SDL_Delay(16); // around 60fps or so + } +} diff --git a/tests/test_main.c b/tests/test_main.c index e24d17f..c171192 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -1,52 +1,50 @@ -/* this doesnt really test saffron's api right now - * because i need to actually implement stuff - * but its good refactor for now -*/ #include <SDL3/SDL.h> -#include <SDL3/SDL_main.h> -#include <SDL3_ttf/SDL_ttf.h> - -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(); +#include <saffron_api.h> +#include <SDL3/SDL_rect.h> +#include <SDL3/SDL_render.h> +#include <saffron.h> + +/* PLEASE don't do this in production + * when wrappers are implemented, + * 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); + SDL_FRect rect = {self->x, self->y, self->w, self->h}; + SDL_RenderFillRect(renderer, &rect); + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderRect(renderer, &rect); +} + +int main(void) { + saffron_init(); + + SaffronWindow* window = saffron_window_new("saffron test", 800, 600); + + /* i guess IM THE LUNATIC NOW + * DEAL WITH IT */ + SaffronWidget* test = saffron_widget_new(); + test->x = 100; + test->y = 100; + test->w = 200; + test->h = 150; + test->draw = my_test_draw; + + /* lunatic method 2 */ + SaffronWidget* test2 = saffron_widget_new(); + test2->x = 150; + test2->y = 500; + test2->w = 300; + test2->h = 170; + test2->draw = my_test_draw; + + saffron_widget_add_child(window->root, test); + saffron_widget_add_child(window->root, test2); + + saffron_window_main(window); + + saffron_window_free(window); + saffron_quit(); return 0; } |
