Mercurial > molko
changeset 389:ef4d4a51aeb7
core: game don't use fixed size states
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 16 Feb 2022 08:44:21 +0100 |
parents | 133248093ed3 |
children | ae2dcf40c1eb |
files | src/libmlk-core-js/core/js-game.c src/libmlk-core/core/game.c src/libmlk-core/core/game.h tests/test-state.c |
diffstat | 4 files changed, 34 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/src/libmlk-core-js/core/js-game.c Wed Feb 16 08:44:06 2022 +0100 +++ b/src/libmlk-core-js/core/js-game.c Wed Feb 16 08:44:21 2022 +0100 @@ -20,6 +20,7 @@ #include <core/game.h> #include <core/state.h> +#include <core/util.h> #include "js-game.h" #include "js-state.h" @@ -27,13 +28,14 @@ /* * TODO: determine if it's worth it to add handle, update and draw functions. */ +static struct state *states[16]; static duk_ret_t Game_push(duk_context *ctx) { struct js_state *state = js_state_require(ctx, 0); - if (game.state == &game.states[GAME_STATE_MAX]) { + if (game.state == &game.states[game.statesz - 1]) { state_finish(&state->st); return duk_error(ctx, DUK_ERR_RANGE_ERROR, "too many states"); } @@ -99,4 +101,6 @@ duk_push_object(ctx); duk_put_function_list(ctx, -1, functions); duk_put_global_string(ctx, "Game"); + + game_init(states, UTIL_SIZE(states)); }
--- a/src/libmlk-core/core/game.c Wed Feb 16 08:44:06 2022 +0100 +++ b/src/libmlk-core/core/game.c Wed Feb 16 08:44:21 2022 +0100 @@ -17,54 +17,58 @@ */ #include <assert.h> -#include <stddef.h> #include <string.h> #include "clock.h" #include "event.h" #include "game.h" #include "state.h" -#include "painter.h" #include "util.h" #include "window.h" -struct game game = { - .state = &game.states[0], -}; +struct game game = {0}; void -game_init(void) +game_init(struct state **states, size_t statesz) { + assert(states); + assert(statesz); + memset(&game, 0, sizeof (game)); - game.state = &game.states[0]; + game.states = states; + game.statesz = statesz; + + for (size_t i = 0; i < game.statesz; ++i) + game.states[i] = NULL; } void game_push(struct state *state) { assert(state); - assert(game.state != &game.states[GAME_STATE_MAX]); + assert(!game.state || game.state != &game.states[game.statesz - 1]); - if (*game.state) { + if (!game.state) { + game.state = &game.states[0]; + state_start(*game.state = state); + } else { state_suspend(*game.state); state_start(*(++game.state) = state); - } else - state_start((*game.state) = state); + } } void game_pop(void) { - if (!*game.state) - return; + assert(game.state); state_end(*game.state); state_finish(*game.state); - *game.state = NULL; - - if (game.state != &game.states[0]) + if (game.state == game.states) + game.state = NULL; + else state_resume(*--game.state); }
--- a/src/libmlk-core/core/game.h Wed Feb 16 08:44:06 2022 +0100 +++ b/src/libmlk-core/core/game.h Wed Feb 16 08:44:21 2022 +0100 @@ -19,18 +19,19 @@ #ifndef MLK_CORE_GAME_H #define MLK_CORE_GAME_H +#include <stddef.h> + #include "core.h" #include "inhibit.h" -#define GAME_STATE_MAX (32) - struct state; union event; struct game { enum inhibit inhibit; - struct state *states[GAME_STATE_MAX + 1]; + struct state **states; + size_t statesz; struct state **state; }; @@ -39,7 +40,7 @@ CORE_BEGIN_DECLS void -game_init(void); +game_init(struct state **, size_t); void game_push(struct state *);
--- a/tests/test-state.c Wed Feb 16 08:44:06 2022 +0100 +++ b/tests/test-state.c Wed Feb 16 08:44:21 2022 +0100 @@ -23,6 +23,7 @@ #include <core/event.h> #include <core/game.h> #include <core/state.h> +#include <core/util.h> struct invokes { unsigned int start; @@ -35,9 +36,11 @@ unsigned int finish; }; +static struct state *states[16]; + RX_SET_UP(setup) { - game_init(); + game_init(states, UTIL_SIZE(states)); return RX_SUCCESS; }