changeset 393:b172b10bbe1c

core: do the same for drawables
author David Demelier <markand@malikania.fr>
date Tue, 15 Feb 2022 21:02:29 +0100
parents e6d7ebad33cc
children 8273c40a5691
files examples/example-action/main.c examples/example-drawable/main.c src/libmlk-core/core/drawable-stack.c src/libmlk-core/core/drawable-stack.h tests/test-drawable.c
diffstat 5 files changed, 34 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-action/main.c	Tue Feb 15 20:47:47 2022 +0100
+++ b/examples/example-action/main.c	Tue Feb 15 21:02:29 2022 +0100
@@ -52,9 +52,11 @@
 #define MY      (100)
 
 /* This is a stack of "parallel" events. */
+static struct action *events_actions[32];
 static struct action_stack events;
 
 /* This is a stack of modal events. */
+static struct action *modal_actions[32];
 static struct action_stack modal;
 
 /* Maximum number of states. */
@@ -389,6 +391,9 @@
 		.draw = draw
 	};
 
+	action_stack_init(&modal, modal_actions, UTIL_SIZE(modal_actions));
+
+	action_stack_init(&events, events_actions, UTIL_SIZE(events_actions));
 	action_stack_add(&events, &chest.event);
 	action_stack_add(&events, &guide.event);
 
--- a/examples/example-drawable/main.c	Tue Feb 15 20:47:47 2022 +0100
+++ b/examples/example-drawable/main.c	Tue Feb 15 21:02:29 2022 +0100
@@ -54,6 +54,7 @@
 };
 
 static struct state *states[1];
+static struct drawable *drawables[64];
 static struct drawable_stack stack;
 
 /*
@@ -180,6 +181,8 @@
 		.draw = draw
 	};
 
+	drawable_stack_init(&stack, drawables, UTIL_SIZE(drawables));
+
 	game_init(states, UTIL_SIZE(states));
 	game_push(&state);
 	game_loop();
--- a/src/libmlk-core/core/drawable-stack.c	Tue Feb 15 20:47:47 2022 +0100
+++ b/src/libmlk-core/core/drawable-stack.c	Tue Feb 15 21:02:29 2022 +0100
@@ -23,14 +23,18 @@
 #include "drawable-stack.h"
 
 #define DRAWABLE_FOREACH(st, iter) \
-	for (size_t i = 0; i < DRAWABLE_STACK_MAX && ((iter) = (st)->objects[i], 1); ++i)
+	for (size_t i = 0; i < (st)->objectsz && ((iter) = (st)->objects[i], 1); ++i)
 
 void
-drawable_stack_init(struct drawable_stack *st)
+drawable_stack_init(struct drawable_stack *st, struct drawable **objects, size_t objectsz)
 {
 	assert(st);
 
-	memset(st, 0, sizeof (*st));
+	st->objects = objects;
+	st->objectsz = objectsz;
+
+	for (size_t i = 0; i < st->objectsz; ++i)
+		st->objects[i] = NULL;
 }
 
 int
@@ -39,7 +43,7 @@
 	assert(st);
 	assert(dw);
 
-	for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) {
+	for (size_t i = 0; i < st->objectsz; ++i) {
 		if (!st->objects[i]) {
 			st->objects[i] = dw;
 			return 0;
@@ -54,8 +58,10 @@
 {
 	assert(st);
 
-	for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) {
-		struct drawable *dw = st->objects[i];
+	struct drawable *dw;
+
+	for (size_t i = 0; i < st->objectsz; ++i) {
+		dw = st->objects[i];
 
 		if (dw && drawable_update(dw, ticks)) {
 			drawable_end(dw);
--- a/src/libmlk-core/core/drawable-stack.h	Tue Feb 15 20:47:47 2022 +0100
+++ b/src/libmlk-core/core/drawable-stack.h	Tue Feb 15 21:02:29 2022 +0100
@@ -19,18 +19,19 @@
 #ifndef MLK_CORE_DRAWABLE_STACK_H
 #define MLK_CORE_DRAWABLE_STACK_H
 
+#include <stddef.h>
+
 #include "core.h"
 
-#define DRAWABLE_STACK_MAX (128)
-
 struct drawable_stack {
-	struct drawable *objects[DRAWABLE_STACK_MAX];
+	struct drawable **objects;
+	size_t objectsz;
 };
 
 CORE_BEGIN_DECLS
 
 void
-drawable_stack_init(struct drawable_stack *);
+drawable_stack_init(struct drawable_stack *, struct drawable **, size_t);
 
 int
 drawable_stack_add(struct drawable_stack *, struct drawable *);
--- a/tests/test-drawable.c	Tue Feb 15 20:47:47 2022 +0100
+++ b/tests/test-drawable.c	Tue Feb 15 21:02:29 2022 +0100
@@ -141,13 +141,16 @@
 
 RX_TEST_CASE(stack, add)
 {
+	struct drawable *drawables[10];
 	struct drawable_stack st = {0};
 	struct drawable dw = {0};
 
+	drawable_stack_init(&st, drawables, 10);
+
 	RX_INT_REQUIRE_EQUAL(drawable_stack_add(&st, &dw), 0);
 
 	/* Now fill up. */
-	for (int i = 0; i < DRAWABLE_STACK_MAX - 1; ++i)
+	for (int i = 0; i < 9; ++i)
 		RX_INT_REQUIRE_EQUAL(drawable_stack_add(&st, &dw), 0);
 
 	/* This one should not fit in. */
@@ -169,8 +172,10 @@
 		{ .dw = INIT(&table[6], my_update_false)	},
 	};
 
+	struct drawable *drawables[10];
 	struct drawable_stack st = {0};
 
+	drawable_stack_init(&st, drawables, 10);
 	drawable_stack_add(&st, &table[0].dw);
 	drawable_stack_add(&st, &table[1].dw);
 	drawable_stack_add(&st, &table[2].dw);
@@ -251,8 +256,11 @@
 		{ .dw = INIT(&table[0], my_update_true)        },
 		{ .dw = INIT(&table[0], my_update_false)       },
 	};
+
+	struct drawable *drawables[10];
 	struct drawable_stack st = {0};
 
+	drawable_stack_init(&st, drawables, 10);
 	drawable_stack_add(&st, &table[0].dw);
 	drawable_stack_add(&st, &table[1].dw);
 	drawable_stack_finish(&st);
@@ -266,10 +274,6 @@
 	RX_REQUIRE(!table[0].inv.draw);
 	RX_REQUIRE(table[0].inv.end);
 	RX_REQUIRE(table[0].inv.finish);
-
-	/* They should also be NULL'ed. */
-	RX_PTR_REQUIRE_EQUAL(st.objects[0], NULL);
-	RX_PTR_REQUIRE_EQUAL(st.objects[1], NULL);
 }
 
 int