changeset 142:fea0cc899931

core: drawable_stack_update returns a bool (similar to action_stack_update) While here, give the opportunity to do something on the end of the drawable with the help of the new end field. While here, add a test and fix some documentation.
author David Demelier <markand@malikania.fr>
date Wed, 14 Oct 2020 14:48:07 +0200
parents 4eeeccf2b732
children 4f4795cba52f
files libcore/core/action.c libcore/core/action.h libcore/core/drawable.c libcore/core/drawable.h tests/CMakeLists.txt tests/test-action.c
diffstat 6 files changed, 77 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/libcore/core/action.c	Wed Oct 14 10:29:53 2020 +0200
+++ b/libcore/core/action.c	Wed Oct 14 14:48:07 2020 +0200
@@ -131,11 +131,7 @@
 	 * We process all actions again in case the user modified the stack
 	 * within their update function.
 	 */
-	ACTION_FOREACH(st, act)
-		if (act)
-			return false;
-
-	return true;
+	return action_stack_completed(st);
 }
 
 void
--- a/libcore/core/action.h	Wed Oct 14 10:29:53 2020 +0200
+++ b/libcore/core/action.h	Wed Oct 14 14:48:07 2020 +0200
@@ -230,7 +230,7 @@
  *
  * \pre st != NULL
  * \param st the stack
- * \return true if there is at least one action in the stack
+ * \return false if there is at least one action in the stack
  */
 bool
 action_stack_completed(const struct action_stack *st);
--- a/libcore/core/drawable.c	Wed Oct 14 10:29:53 2020 +0200
+++ b/libcore/core/drawable.c	Wed Oct 14 14:48:07 2020 +0200
@@ -46,8 +46,19 @@
 }
 
 void
+drawable_end(struct drawable *dw)
+{
+	assert(dw);
+
+	if (dw->end)
+		dw->end(dw);
+}
+
+void
 drawable_finish(struct drawable *dw)
 {
+	assert(dw);
+
 	if (dw->finish)
 		dw->finish(dw);
 }
@@ -76,7 +87,7 @@
 	return false;
 }
 
-void
+bool
 drawable_stack_update(struct drawable_stack *st, unsigned int ticks)
 {
 	assert(st);
@@ -85,10 +96,17 @@
 		struct drawable *dw = st->objects[i];
 
 		if (dw && drawable_update(dw, ticks)) {
+			drawable_end(dw);
 			drawable_finish(dw);
 			st->objects[i] = NULL;
 		}
 	}
+
+	/*
+	 * We process the array again in case a drawable added a new drawable
+	 * within the update function.
+	 */
+	return drawable_stack_completed(st);
 }
 
 void
@@ -103,14 +121,32 @@
 			drawable_draw(dw);
 }
 
+bool
+drawable_stack_completed(const struct drawable_stack *st)
+{
+	assert(st);
+
+	struct drawable *dw;
+
+	DRAWABLE_FOREACH(st, dw)
+		if (dw)
+			return false;
+
+	return true;
+}
+
 void
 drawable_stack_finish(struct drawable_stack *st)
 {
-	for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) {
-		struct drawable *dw = st->objects[i];
+	assert(st);
+
+	struct drawable *dw;
 
-		if (dw && dw->finish)
-			dw->finish(dw);
+	DRAWABLE_FOREACH(st, dw) {
+		if (dw) {
+			drawable_end(dw);
+			drawable_finish(dw);
+		}
 	}
 
 	memset(st, 0, sizeof (*st));
--- a/libcore/core/drawable.h	Wed Oct 14 10:29:53 2020 +0200
+++ b/libcore/core/drawable.h	Wed Oct 14 14:48:07 2020 +0200
@@ -56,6 +56,15 @@
 	 * \param dw the drawable object
 	 */
 	void (*draw)(struct drawable *dw);
+
+	/**
+	 * (RW)
+	 *
+	 * Called immediately after the drawable ended.
+	 *
+	 * \param act this action
+	 */
+	void (*end)(struct drawable *act);
 	
 	/**
 	 * Destroy the drawable if necessary.
@@ -87,6 +96,15 @@
 drawable_draw(struct drawable *dw);
 
 /**
+ * Shortcut for dw->end (if not NULL).
+ *
+ * \pre dw != NULL
+ * \param dw the drawable object
+ */
+void
+drawable_end(struct drawable *dw);
+
+/**
  * Shortcut for dw->finish (if not NULL).
  *
  * \pre dw != NULL
@@ -139,8 +157,9 @@
  * \pre st != NULL
  * \param st the drawable stack
  * \param ticks the number of ticks since last frame
+ * \return true if all drawable were rendered
  */
-void
+bool
 drawable_stack_update(struct drawable_stack *st, unsigned int ticks);
 
 /**
@@ -153,6 +172,16 @@
 drawable_stack_draw(struct drawable_stack *st);
 
 /**
+ * Tells if there is any pending drawable in the stack.
+ *
+ * \pre st != NULL
+ * \param st the stack
+ * \return false if there is at least one drawable in the stack
+ */
+bool
+drawable_stack_completed(const struct drawable_stack *st);
+
+/**
  * Clear all drawable objects into the stack.
  */
 void
--- a/tests/CMakeLists.txt	Wed Oct 14 10:29:53 2020 +0200
+++ b/tests/CMakeLists.txt	Wed Oct 14 14:48:07 2020 +0200
@@ -36,3 +36,4 @@
 )
 molko_define_test(TARGET rbuf SOURCES test-rbuf.c)
 molko_define_test(TARGET save SOURCES test-save.c)
+molko_define_test(TARGET drawable SOURCES test-drawable.c)
--- a/tests/test-action.c	Wed Oct 14 10:29:53 2020 +0200
+++ b/tests/test-action.c	Wed Oct 14 14:48:07 2020 +0200
@@ -136,7 +136,7 @@
 GREATEST_TEST
 basics_draw(void)
 {
-	struct invokes inv;
+	struct invokes inv = {0};
 	struct action act = INIT(&inv, my_update_true);
 
 	action_draw(&act);
@@ -152,7 +152,7 @@
 GREATEST_TEST
 basics_end(void)
 {
-	struct invokes inv;
+	struct invokes inv = {0};
 	struct action act = INIT(&inv, my_update_true);
 
 	action_end(&act);
@@ -168,7 +168,7 @@
 GREATEST_TEST
 basics_finish(void)
 {
-	struct invokes inv;
+	struct invokes inv = {0};
 	struct action act = INIT(&inv, my_update_true);
 
 	action_finish(&act);