Mercurial > molko
comparison examples/example-debug.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 | aab824406d3d |
children |
comparison
equal
deleted
inserted
replaced
206:4e2bf083759b | 207:133926e08d6e |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 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 | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <core/clock.h> | |
20 #include <core/core.h> | 19 #include <core/core.h> |
21 #include <core/event.h> | 20 #include <core/event.h> |
22 #include <core/sys.h> | 21 #include <core/game.h> |
23 #include <core/window.h> | 22 #include <core/window.h> |
24 #include <core/painter.h> | 23 #include <core/painter.h> |
25 #include <core/panic.h> | 24 #include <core/panic.h> |
26 #include <core/trace.h> | 25 #include <core/state.h> |
27 #include <core/util.h> | 26 #include <core/util.h> |
28 | 27 |
29 #include <ui/debug.h> | 28 #include <ui/debug.h> |
30 #include <ui/theme.h> | 29 #include <ui/theme.h> |
31 #include <ui/ui.h> | 30 #include <ui/ui.h> |
32 | 31 |
33 #define W 1280 | 32 #define W 1280 |
34 #define H 720 | 33 #define H 720 |
34 | |
35 static int mouse_x; | |
36 static int mouse_y; | |
35 | 37 |
36 static void | 38 static void |
37 init(void) | 39 init(void) |
38 { | 40 { |
39 if (!core_init() || !ui_init()) | 41 if (!core_init() || !ui_init()) |
43 | 45 |
44 debug_options.enable = true; | 46 debug_options.enable = true; |
45 } | 47 } |
46 | 48 |
47 static void | 49 static void |
50 handle(struct state *st, const union event *ev) | |
51 { | |
52 (void)st; | |
53 | |
54 switch (ev->type) { | |
55 case EVENT_MOUSE: | |
56 mouse_x = ev->mouse.x; | |
57 mouse_y = ev->mouse.y; | |
58 break; | |
59 case EVENT_QUIT: | |
60 game_quit(); | |
61 break; | |
62 default: | |
63 break; | |
64 } | |
65 } | |
66 | |
67 static void | |
68 draw(struct state *st) | |
69 { | |
70 (void)st; | |
71 | |
72 struct debug_report report = {0}; | |
73 | |
74 painter_set_color(0x4f8fbaff); | |
75 painter_clear(); | |
76 debugf(&report, "Game running."); | |
77 debugf(&report, "mouse: %d, %d", mouse_x, mouse_y); | |
78 painter_present(); | |
79 } | |
80 | |
81 static void | |
48 run(void) | 82 run(void) |
49 { | 83 { |
50 struct clock clock = {0}; | 84 struct state state = { |
51 int x = 0, y = 0; | 85 .handle = handle, |
86 .draw = draw | |
87 }; | |
52 | 88 |
53 clock_start(&clock); | 89 game_switch(&state, true); |
54 | 90 game_loop(); |
55 for (;;) { | |
56 struct debug_report report = {0}; | |
57 union event ev; | |
58 unsigned int elapsed = clock_elapsed(&clock); | |
59 | |
60 clock_start(&clock); | |
61 | |
62 while (event_poll(&ev)) { | |
63 switch (ev.type) { | |
64 case EVENT_MOUSE: | |
65 x = ev.mouse.x; | |
66 y = ev.mouse.y; | |
67 break; | |
68 case EVENT_QUIT: | |
69 return; | |
70 default: | |
71 break; | |
72 } | |
73 } | |
74 | |
75 painter_set_color(0x4f8fbaff); | |
76 painter_clear(); | |
77 debugf(&report, "Game running."); | |
78 debugf(&report, "mouse: %d, %d", x, y); | |
79 painter_present(); | |
80 | |
81 if ((elapsed = clock_elapsed(&clock)) < 20) | |
82 delay(20 - elapsed); | |
83 } | |
84 } | 91 } |
85 | 92 |
86 static void | 93 static void |
87 quit(void) | 94 quit(void) |
88 { | 95 { |