aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_widget.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/saffron_widget.c')
-rw-r--r--src/saffron_widget.c31
1 files changed, 27 insertions, 4 deletions
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++;
+}
+