Mercurial > molko
changeset 66:9435a53adab4
core: implement wait action, closes #2466
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 27 Jan 2020 13:03:39 +0100 |
parents | 80a913d25aa9 |
children | 7187c0d9b9c0 |
files | Makefile src/core/wait.c src/core/wait.h |
diffstat | 3 files changed, 146 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Mon Jan 27 12:24:20 2020 +0100 +++ b/Makefile Mon Jan 27 13:03:39 2020 +0100 @@ -42,6 +42,7 @@ src/core/texture.c \ src/core/util.c \ src/core/walksprite.c \ + src/core/wait.c \ src/core/window.c CORE_OBJS= ${CORE_SRCS:.c=.o} CORE_DEPS= ${CORE_SRCS:.c=.d}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/wait.c Mon Jan 27 13:03:39 2020 +0100 @@ -0,0 +1,73 @@ +/* + * wait.c -- wait action + * + * Copyright (c) 2020 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 <stdlib.h> +#include <string.h> + +#include "action.h" +#include "wait.h" +#include "util.h" + +static bool +action_update(struct action *a, unsigned int ticks) +{ + assert(a); + + return wait_update(a->data, ticks); +} + +static void +action_finish(struct action *a) +{ + assert(a); + + free(a->data); +} + +void +wait_start(struct wait *w) +{ + assert(w); + w->elapsed = 0u; +} + +bool +wait_update(struct wait *w, unsigned int ticks) +{ + assert(w); + + w->elapsed += ticks; + + if (w->elapsed >= w->delay) + w->elapsed = w->delay; + + return w->elapsed >= w->delay; +} + +void +wait_action(const struct wait *w, struct action *a) +{ + assert(w); + assert(a); + + memset(a, 0, sizeof (struct action)); + a->data = ememdup(w, sizeof (struct wait)); + a->update = action_update; + a->finish = action_finish; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/wait.h Mon Jan 27 13:03:39 2020 +0100 @@ -0,0 +1,72 @@ +/* + * wait.h -- wait action + * + * Copyright (c) 2020 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 MOLKO_WAIT_H +#define MOLKO_WAIT_H + +/** + * \file wait.h + * \brief Wait action. + */ + +#include <stdbool.h> + +struct action; + +/** + * \brief Wait action. + */ +struct wait { + unsigned int delay; /*!< (RW) Time to wait */ + unsigned int elapsed; /*!< (RO) Elapsed time */ +}; + +/** + * Start the wait action. + * + * This function is equivalent to w->elapsed = 0; + * + * \pre w != NULL + * \param w the wait object + */ +void +wait_start(struct wait *w); + +/** + * Update the wait object. + * + * \pre w != NULL + * \param w the wait object + * \param ticks the number of milliseconds since last frame + * \return true if complete + */ +bool +wait_update(struct wait *w, unsigned int ticks); + +/** + * Create an action from the wait object. + * + * \pre w != NULL + * \pre a != NULL + * \param w the wait object to copy from + * \param a the action to fill + */ +void +wait_action(const struct wait *w, struct action *a); + +#endif /* !MOLKO_WAIT_H */