Mercurial > molko
diff examples/example-animation.c @ 207:133926e08d6e
examples: use game_loop for all
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 11 Nov 2020 16:09:43 +0100 |
parents | 65a07c7d8ff4 |
children |
line wrap: on
line diff
--- a/examples/example-animation.c Tue Nov 10 17:32:12 2020 +0100 +++ b/examples/example-animation.c Wed Nov 11 16:09:43 2020 +0100 @@ -17,15 +17,16 @@ */ #include <core/animation.h> -#include <core/clock.h> #include <core/core.h> #include <core/event.h> +#include <core/game.h> #include <core/image.h> #include <core/sys.h> #include <core/window.h> #include <core/painter.h> #include <core/panic.h> #include <core/sprite.h> +#include <core/state.h> #include <core/texture.h> #include <core/util.h> @@ -37,8 +38,6 @@ #define W 1280 #define H 720 -static struct texture numbers; - static struct label label = { .text = "Keys: <Space> start or reset the animation.", .x = 10, @@ -46,6 +45,11 @@ .flags = LABEL_FLAGS_SHADOW }; +static struct texture numbers; +static struct animation animation; +static struct sprite sprite; +static bool completed = true; + static void init(void) { @@ -58,57 +62,67 @@ } static void +handle(struct state *st, const union event *ev) +{ + (void)st; + + switch (ev->type) { + case EVENT_KEYDOWN: + switch (ev->key.key) { + case KEY_SPACE: + animation_start(&animation); + completed = animation_completed(&animation); + break; + default: + break; + } + break; + case EVENT_QUIT: + game_quit(); + break; + default: + break; + } +} + +static void +update(struct state *st, unsigned int ticks) +{ + (void)st; + + if (!completed) + completed = animation_update(&animation, ticks); +} + +static void +draw(struct state *st) +{ + (void)st; + + painter_set_color(0x4f8fbaff); + painter_clear(); + label_draw(&label); + + if (!completed) + animation_draw(&animation, (window.w - sprite.cellw) / 2, (window.h - sprite.cellh) / 2); + + painter_present(); +} + +static void run(void) { - struct clock clock = {0}; - struct animation animation; - struct sprite sprite; - bool completed = true; + struct state state = { + .handle = handle, + .update = update, + .draw = draw + }; - clock_start(&clock); sprite_init(&sprite, &numbers, 48, 48); animation_init(&animation, &sprite, 1000); - for (;;) { - union event ev; - unsigned int elapsed = clock_elapsed(&clock); - - clock_start(&clock); - - while (event_poll(&ev)) { - switch (ev.type) { - case EVENT_KEYDOWN: - switch (ev.key.key) { - case KEY_SPACE: - animation_start(&animation); - completed = animation_completed(&animation); - break; - default: - break; - } - break; - case EVENT_QUIT: - return; - default: - break; - } - } - - if (!completed) - completed = animation_update(&animation, elapsed); - - painter_set_color(0x4f8fbaff); - painter_clear(); - label_draw(&label); - - if (!completed) - animation_draw(&animation, (window.w - sprite.cellw) / 2, (window.h - sprite.cellh) / 2); - - painter_present(); - - if ((elapsed = clock_elapsed(&clock)) < 20) - delay(20 - elapsed); - } + game_switch(&state, true); + game_loop(); } static void