Mercurial > molko
comparison src/examples/example-animation/main.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | examples/example-animation/main.c@d01e83210ca2 |
children |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * example-animation.c -- example on how to use animations | |
3 * | |
4 * Copyright (c) 2020-2021 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/core.h> | |
21 #include <core/event.h> | |
22 #include <core/game.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/state.h> | |
30 #include <core/texture.h> | |
31 #include <core/util.h> | |
32 | |
33 #include <ui/label.h> | |
34 #include <ui/ui.h> | |
35 | |
36 #define W 1280 | |
37 #define H 720 | |
38 #define PATH(r) util_pathf("%s/mlk-adventure/%s", sys_dir(SYS_DIR_DATA), r) | |
39 | |
40 static struct label label = { | |
41 .text = "Keys: <Space> start or reset the animation.", | |
42 .x = 10, | |
43 .y = 10, | |
44 .flags = LABEL_FLAGS_SHADOW | |
45 }; | |
46 | |
47 static struct texture numbers; | |
48 static struct animation animation; | |
49 static struct sprite sprite; | |
50 static int completed = 1; | |
51 | |
52 static void | |
53 init(void) | |
54 { | |
55 if (core_init("fr.malikania", "animation") < 0 || ui_init() < 0) | |
56 panic(); | |
57 if (window_open("Example - Animation", W, H) < 0) | |
58 panic(); | |
59 if (image_open(&numbers, PATH("sprites/numbers.png")) < 0) | |
60 panic(); | |
61 } | |
62 | |
63 static void | |
64 handle(struct state *st, const union event *ev) | |
65 { | |
66 (void)st; | |
67 | |
68 switch (ev->type) { | |
69 case EVENT_KEYDOWN: | |
70 switch (ev->key.key) { | |
71 case KEY_SPACE: | |
72 animation_start(&animation); | |
73 completed = animation_completed(&animation); | |
74 break; | |
75 default: | |
76 break; | |
77 } | |
78 break; | |
79 case EVENT_QUIT: | |
80 game_quit(); | |
81 break; | |
82 default: | |
83 break; | |
84 } | |
85 } | |
86 | |
87 static void | |
88 update(struct state *st, unsigned int ticks) | |
89 { | |
90 (void)st; | |
91 | |
92 if (!completed) | |
93 completed = animation_update(&animation, ticks); | |
94 } | |
95 | |
96 static void | |
97 draw(struct state *st) | |
98 { | |
99 (void)st; | |
100 | |
101 painter_set_color(0x4f8fbaff); | |
102 painter_clear(); | |
103 label_draw(&label); | |
104 | |
105 if (!completed) | |
106 animation_draw(&animation, (window.w - sprite.cellw) / 2, (window.h - sprite.cellh) / 2); | |
107 | |
108 painter_present(); | |
109 } | |
110 | |
111 static void | |
112 run(void) | |
113 { | |
114 struct state state = { | |
115 .handle = handle, | |
116 .update = update, | |
117 .draw = draw | |
118 }; | |
119 | |
120 sprite_init(&sprite, &numbers, 48, 48); | |
121 animation_init(&animation, &sprite, 1000); | |
122 | |
123 game_push(&state); | |
124 game_loop(); | |
125 } | |
126 | |
127 static void | |
128 quit(void) | |
129 { | |
130 window_finish(); | |
131 ui_finish(); | |
132 core_finish(); | |
133 } | |
134 | |
135 int | |
136 main(int argc, char **argv) | |
137 { | |
138 (void)argc; | |
139 (void)argv; | |
140 | |
141 init(); | |
142 run(); | |
143 quit(); | |
144 } | |
145 |