comparison examples/example-animation.c @ 183:604fad63bd9c

core: fix animation delay
author David Demelier <markand@malikania.fr>
date Mon, 02 Nov 2020 13:42:51 +0100
parents
children 65a07c7d8ff4
comparison
equal deleted inserted replaced
182:f6497ec74b49 183:604fad63bd9c
1 /*
2 * example-animation.c -- example on how to use animations
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <core/animation.h>
20 #include <core/clock.h>
21 #include <core/core.h>
22 #include <core/event.h>
23 #include <core/image.h>
24 #include <core/sys.h>
25 #include <core/window.h>
26 #include <core/painter.h>
27 #include <core/panic.h>
28 #include <core/sprite.h>
29 #include <core/texture.h>
30 #include <core/util.h>
31
32 #include <ui/label.h>
33 #include <ui/ui.h>
34
35 #include <assets/sprites/numbers.h>
36
37 #define W 1280
38 #define H 720
39
40 static struct texture numbers;
41
42 static struct label label = {
43 .text = "Keys: <Space> start or reset the animation.",
44 .x = 10,
45 .y = 10,
46 .flags = LABEL_FLAGS_SHADOW
47 };
48
49 static void
50 init(void)
51 {
52 if (!core_init() || !ui_init())
53 panic();
54 if (!window_open("Example - Animation", W, H))
55 panic();
56 if (!image_openmem(&numbers, sprites_numbers, sizeof (sprites_numbers)))
57 panic();
58 }
59
60 static void
61 run(void)
62 {
63 struct clock clock = {0};
64 struct animation animation;
65 struct sprite sprite;
66 bool completed = true;
67
68 clock_start(&clock);
69 sprite_init(&sprite, &numbers, 128, 128);
70 animation_init(&animation, &sprite, 1000);
71
72 for (;;) {
73 union event ev;
74 unsigned int elapsed = clock_elapsed(&clock);
75
76 clock_start(&clock);
77
78 while (event_poll(&ev)) {
79 switch (ev.type) {
80 case EVENT_KEYDOWN:
81 switch (ev.key.key) {
82 case KEY_SPACE:
83 animation_start(&animation);
84 completed = animation_completed(&animation);
85 break;
86 default:
87 break;
88 }
89 break;
90 case EVENT_QUIT:
91 return;
92 default:
93 break;
94 }
95 }
96
97 if (!completed)
98 completed = animation_update(&animation, elapsed);
99
100 painter_set_color(0x4f8fbaff);
101 painter_clear();
102 label_draw(&label);
103
104 if (!completed)
105 animation_draw(&animation, (window.w - sprite.cellw) / 2, (window.h - sprite.cellh) / 2);
106
107 painter_present();
108
109 if ((elapsed = clock_elapsed(&clock)) < 20)
110 delay(20 - elapsed);
111 }
112 }
113
114 static void
115 quit(void)
116 {
117 window_finish();
118 ui_finish();
119 core_finish();
120 }
121
122 int
123 main(int argc, char **argv)
124 {
125 (void)argc;
126 (void)argv;
127
128 init();
129 run();
130 quit();
131 }
132