changeset 69:5da49274e5fb

core: add new action.end callback for user
author David Demelier <markand@malikania.fr>
date Mon, 27 Jan 2020 13:31:24 +0100
parents 767b90552ede
children 53b217afe122
files src/adventure/main.c src/core/action.h src/core/game.c src/core/script.c
diffstat 4 files changed, 54 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/adventure/main.c	Mon Jan 27 13:04:36 2020 +0100
+++ b/src/adventure/main.c	Mon Jan 27 13:31:24 2020 +0100
@@ -34,6 +34,7 @@
 #include "sprite.h"
 #include "sys.h"
 #include "util.h"
+#include "wait.h"
 #include "window.h"
 #include "script.h"
 
@@ -53,12 +54,22 @@
 }
 
 static void
+myresult(struct action *a)
+{
+	struct message *msg = a->data;
+
+	printf("selected: %d\n", msg->index);
+}
+
+static void
 run(void)
 {
 	union event ev;
 	struct clock clock;
 	struct font *font;
 	struct texture *frame;
+	struct script sc;
+	struct action ac;
 
 	if (!(font = font_openf(sys_datapath("fonts/DejaVuSans.ttf"), 15)))
 		error_fatal();
@@ -80,7 +91,23 @@
 	};
 
 	clock_start(&clock);
+	script_init(&sc);
+
+	/* Wait first. */
+	struct wait w = { .delay = 5000 };
+
+	wait_action(&w, &ac);
+	script_append(&sc, &ac);
+
 	message_start(&msg);
+	message_action(&msg, &ac);
+	ac.end = myresult;
+
+	script_append(&sc, &ac);
+	script_start(&sc);
+	script_action(&sc, &ac);
+
+	game_add_action(&ac);
 
 	for (;;) {
 		unsigned int elapsed = clock_elapsed(&clock);
@@ -92,21 +119,14 @@
 			if (ev.type == EVENT_QUIT)
 				return;
 
-			//game_handle(&ev);
-			if (msg.state)
-				message_handle(&msg, &ev);
+			game_handle(&ev);
 		}
 
-		if (msg.state)
-			message_update(&msg, elapsed);
-
 		painter_set_color(0xffffffff);
 		painter_clear();
 
-		if (msg.state)
-			message_draw(&msg);
-		//game_update(elapsed);
-		//game_draw();
+		game_update(elapsed);
+		game_draw();
 
 		painter_present();
 
--- a/src/core/action.h	Mon Jan 27 13:04:36 2020 +0100
+++ b/src/core/action.h	Mon Jan 27 13:31:24 2020 +0100
@@ -80,9 +80,20 @@
 	/**
 	 * (RW)
 	 *
+	 * Called when the action was completed.
+	 *
+	 * This callback is mostly provided to allow the user doing something
+	 * else once an action is complete. Predefined actions should not use
+	 * this callback by themselves.
+	 */
+	void (*end)(struct action *a);
+
+	/**
+	 * (RW)
+	 *
 	 * Close the action before removal.
 	 */
 	void (*finish)(struct action *);
 };
 
-#endif /* !ACTION_H*/
+#endif /* !ACTION_H */
--- a/src/core/game.c	Mon Jan 27 13:04:36 2020 +0100
+++ b/src/core/game.c	Mon Jan 27 13:31:24 2020 +0100
@@ -75,6 +75,8 @@
 			continue;
 
 		if (a->update(a, ticks)) {
+			if (a->end)
+				a->end(a);
 			if (a->finish)
 				a->finish(a);
 
--- a/src/core/script.c	Mon Jan 27 13:04:36 2020 +0100
+++ b/src/core/script.c	Mon Jan 27 13:31:24 2020 +0100
@@ -103,9 +103,16 @@
 {
 	assert(s);
 
-	if (s->iter && s->iter->action.update(&s->iter->action, ticks)) {
-		if (s->iter->action.finish)
-			s->iter->action.finish(&s->iter->action);
+	if (!s->iter)
+		return true;
+
+	struct action *a = &s->iter->action;
+
+	if (a->update(a, ticks)) {
+		if (a->end)
+			a->end(a);
+		if (a->finish)
+			a->finish(a);
 
 		s->iter = s->iter->next;
 	}