diff libcore/core/game.c @ 162:629f55f3961e

core: rework states
author David Demelier <markand@malikania.fr>
date Sun, 18 Oct 2020 12:01:59 +0200
parents b386d25832c8
children eb3148c1e54d
line wrap: on
line diff
--- a/libcore/core/game.c	Sat Oct 17 10:12:41 2020 +0200
+++ b/libcore/core/game.c	Sun Oct 18 12:01:59 2020 +0200
@@ -32,47 +32,63 @@
 	assert(state);
 
 	if (quick) {
+		if (game.state_next)
+			state_finish(game.state_next);
+
 		game.state_next = NULL;
 		game.state = state;
-		game.state->enter();
+		state_start(game.state);
 	} else
 		game.state_next = state;
 }
 
 void
-game_handle(const union event *event)
+game_handle(const union event *ev)
 {
-	assert(event);
+	assert(ev);
 
 	if (game.state && !(game.inhibit & INHIBIT_STATE_INPUT))
-		game.state->handle(event);
+		state_handle(game.state, ev);
 }
 
 void
 game_update(unsigned int ticks)
 {
-	if (!(game.inhibit & INHIBIT_STATE_UPDATE)) {
-		/* Change state if any. */
-		if (game.state_next) {
-			/* Inform the current state we're gonna leave it. */
-			if (game.state)
-				game.state->leave();
+	if (game.inhibit & INHIBIT_STATE_UPDATE)
+		return;
+
+	/* Change state if any. */
+	if (game.state_next) {
+		struct state *previous;
+
+		/* Inform the current state we're gonna leave it. */
+		if ((previous = game.state))
+			state_end(previous);
 
-			game.state = game.state_next;
-			game.state->enter();
-			game.state_next = NULL;
-		}
+		/* Change the state and tell we're starting it. */
+		if ((game.state = game.state_next))
+			state_start(game.state);
+
+		game.state_next = NULL;
 
-		if (game.state)
-			game.state->update(ticks);
+		/*
+		 * Only call finish at the end of the process because
+		 * the user may still use resources from it during the
+		 * transition.
+		 */
+		if (previous)
+			state_finish(previous);
 	}
+
+	if (game.state)
+		state_update(game.state, ticks);
 }
 
 void
 game_draw(void)
 {
 	if (game.state && !(game.inhibit & INHIBIT_STATE_DRAW))
-		game.state->draw();
+		state_draw(game.state);
 
 	painter_present();
 }
@@ -80,8 +96,15 @@
 void
 game_quit(void)
 {
-	if (game.state && game.state->leave)
-		game.state->leave();
-
-	game.state = NULL;
+	/* Close the next state if any. */
+	if (game.state_next) {
+		state_finish(game.state_next);
+		game.state_next = NULL;
+	}
+	
+	if (game.state) {
+		state_end(game.state);
+		state_finish(game.state);
+		game.state = NULL;
+	}
 }