Mercurial > molko
changeset 369:1e06f871dc63
core: split action and action_stack
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 24 Oct 2021 17:33:12 +0200 |
parents | 15bdac29ba4b |
children | ea7a24c814e4 |
files | examples/example-action/main.c examples/example-ui/main.c src/libmlk-core/CMakeLists.txt src/libmlk-core/core/action-stack.c src/libmlk-core/core/action-stack.h src/libmlk-core/core/action.c src/libmlk-core/core/action.h src/libmlk-rpg/rpg/battle.h src/libmlk-rpg/rpg/map.h tests/test-action.c |
diffstat | 10 files changed, 194 insertions(+), 136 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/example-action/main.c Sun Oct 24 17:07:20 2021 +0200 +++ b/examples/example-action/main.c Sun Oct 24 17:33:12 2021 +0200 @@ -19,6 +19,7 @@ #include <assert.h> #include <core/action.h> +#include <core/action-stack.h> #include <core/core.h> #include <core/event.h> #include <core/game.h>
--- a/examples/example-ui/main.c Sun Oct 24 17:07:20 2021 +0200 +++ b/examples/example-ui/main.c Sun Oct 24 17:33:12 2021 +0200 @@ -17,6 +17,7 @@ */ #include <core/action.h> +#include <core/action-stack.h> #include <core/core.h> #include <core/event.h> #include <core/game.h>
--- a/src/libmlk-core/CMakeLists.txt Sun Oct 24 17:07:20 2021 +0200 +++ b/src/libmlk-core/CMakeLists.txt Sun Oct 24 17:33:12 2021 +0200 @@ -22,6 +22,8 @@ SOURCES ${libmlk-core_SOURCE_DIR}/core/action.c ${libmlk-core_SOURCE_DIR}/core/action.h + ${libmlk-core_SOURCE_DIR}/core/action-stack.c + ${libmlk-core_SOURCE_DIR}/core/action-stack.h ${libmlk-core_SOURCE_DIR}/core/alloc.c ${libmlk-core_SOURCE_DIR}/core/alloc.h ${libmlk-core_SOURCE_DIR}/core/animation.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/libmlk-core/core/action-stack.c Sun Oct 24 17:33:12 2021 +0200 @@ -0,0 +1,130 @@ +/* + * action-stack.h -- convenient stack of actions + * + * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> +#include <string.h> + +#include "action.h" +#include "action-stack.h" + +#define ACTION_FOREACH(st, iter) \ + for (size_t i = 0; i < ACTION_STACK_MAX && ((iter) = (st)->actions[i], 1); ++i) + +void +action_stack_init(struct action_stack *st) +{ + assert(st); + + memset(st, 0, sizeof (*st)); +} + +int +action_stack_add(struct action_stack *st, struct action *act) +{ + assert(st); + assert(act); + + for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { + if (!st->actions[i]) { + st->actions[i] = act; + return 0; + } + } + + return -1; +} + +void +action_stack_handle(struct action_stack *st, const union event *ev) +{ + assert(st); + assert(ev); + + struct action *act; + + ACTION_FOREACH(st, act) + if (act) + action_handle(act, ev); +} + +int +action_stack_update(struct action_stack *st, unsigned int ticks) +{ + assert(st); + + struct action *act; + + for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { + act = st->actions[i]; + + if (act && action_update(act, ticks)) { + action_end(act); + action_finish(act); + st->actions[i] = NULL; + } + } + + /* + * We process all actions again in case the user modified the stack + * within their update function. + */ + return action_stack_completed(st); +} + +void +action_stack_draw(const struct action_stack *st) +{ + assert(st); + + struct action *act; + + ACTION_FOREACH(st, act) + if (act) + action_draw(act); +} + +int +action_stack_completed(const struct action_stack *st) +{ + assert(st); + + struct action *act; + + ACTION_FOREACH(st, act) + if (act) + return 0; + + return 1; +} + +void +action_stack_finish(struct action_stack *st) +{ + assert(st); + + struct action *act; + + ACTION_FOREACH(st, act) { + if (act) { + action_end(act); + action_finish(act); + } + } + + memset(st, 0, sizeof (*st)); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/libmlk-core/core/action-stack.h Sun Oct 24 17:33:12 2021 +0200 @@ -0,0 +1,57 @@ +/* + * action-stack.h -- convenient stack of actions + * + * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MLK_CORE_ACTION_STACK_H +#define MLK_CORE_ACTION_STACK_H + +#include "core.h" + +#define ACTION_STACK_MAX (128) + +union event; + +struct action_stack { + struct action *actions[ACTION_STACK_MAX]; +}; + +CORE_BEGIN_DECLS + +void +action_stack_init(struct action_stack *); + +int +action_stack_add(struct action_stack *, struct action *); + +void +action_stack_handle(struct action_stack *, const union event *); + +int +action_stack_update(struct action_stack *, unsigned int); + +void +action_stack_draw(const struct action_stack *); + +int +action_stack_completed(const struct action_stack *); + +void +action_stack_finish(struct action_stack *); + +CORE_END_DECLS + +#endif /* !MLK_CORE_ACTION_STACK_H */
--- a/src/libmlk-core/core/action.c Sun Oct 24 17:07:20 2021 +0200 +++ b/src/libmlk-core/core/action.c Sun Oct 24 17:33:12 2021 +0200 @@ -17,14 +17,9 @@ */ #include <assert.h> -#include <stddef.h> -#include <string.h> #include "action.h" -#define ACTION_FOREACH(st, iter) \ - for (size_t i = 0; i < ACTION_STACK_MAX && ((iter) = (st)->actions[i], 1); ++i) - void action_handle(struct action *act, const union event *ev) { @@ -73,107 +68,3 @@ if (act->finish) act->finish(act); } - -void -action_stack_init(struct action_stack *st) -{ - assert(st); - - memset(st, 0, sizeof (*st)); -} - -int -action_stack_add(struct action_stack *st, struct action *act) -{ - assert(st); - assert(act); - - for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { - if (!st->actions[i]) { - st->actions[i] = act; - return 0; - } - } - - return -1; -} - -void -action_stack_handle(struct action_stack *st, const union event *ev) -{ - assert(st); - assert(ev); - - struct action *act; - - ACTION_FOREACH(st, act) - if (act) - action_handle(act, ev); -} - -int -action_stack_update(struct action_stack *st, unsigned int ticks) -{ - assert(st); - - struct action *act; - - for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { - act = st->actions[i]; - - if (act && action_update(act, ticks)) { - action_end(act); - action_finish(act); - st->actions[i] = NULL; - } - } - - /* - * We process all actions again in case the user modified the stack - * within their update function. - */ - return action_stack_completed(st); -} - -void -action_stack_draw(const struct action_stack *st) -{ - assert(st); - - struct action *act; - - ACTION_FOREACH(st, act) - if (act) - action_draw(act); -} - -int -action_stack_completed(const struct action_stack *st) -{ - assert(st); - - struct action *act; - - ACTION_FOREACH(st, act) - if (act) - return 0; - - return 1; -} - -void -action_stack_finish(struct action_stack *st) -{ - assert(st); - - struct action *act; - - ACTION_FOREACH(st, act) { - if (act) { - action_end(act); - action_finish(act); - } - } - - memset(st, 0, sizeof (*st)); -}
--- a/src/libmlk-core/core/action.h Sun Oct 24 17:07:20 2021 +0200 +++ b/src/libmlk-core/core/action.h Sun Oct 24 17:33:12 2021 +0200 @@ -21,8 +21,6 @@ #include "core.h" -#define ACTION_STACK_MAX (128) - union event; struct action { @@ -34,10 +32,6 @@ void (*finish)(struct action *); }; -struct action_stack { - struct action *actions[ACTION_STACK_MAX]; -}; - CORE_BEGIN_DECLS void @@ -55,27 +49,6 @@ void action_finish(struct action *); -void -action_stack_init(struct action_stack *); - -int -action_stack_add(struct action_stack *, struct action *); - -void -action_stack_handle(struct action_stack *, const union event *); - -int -action_stack_update(struct action_stack *, unsigned int); - -void -action_stack_draw(const struct action_stack *); - -int -action_stack_completed(const struct action_stack *); - -void -action_stack_finish(struct action_stack *); - CORE_END_DECLS #endif /* !MLK_CORE_ACTION_H */
--- a/src/libmlk-rpg/rpg/battle.h Sun Oct 24 17:07:20 2021 +0200 +++ b/src/libmlk-rpg/rpg/battle.h Sun Oct 24 17:33:12 2021 +0200 @@ -20,6 +20,7 @@ #define MLK_RPG_BATTLE_H #include <core/action.h> +#include <core/action-stack.h> #include <core/core.h> #include <core/drawable.h> #include <core/drawable-stack.h>