comparison examples/example-notify/example-notify.c @ 415:a5b98db4fd87

misc: fix .hgignore too greedy
author David Demelier <markand@malikania.fr>
date Sun, 09 Oct 2022 13:53:33 +0200
parents
children 8f59201dc76b
comparison
equal deleted inserted replaced
414:6947c1fefe5c 415:a5b98db4fd87
1 /*
2 * example-notify -- show how notifications work
3 *
4 * Copyright (c) 2020-2022 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 <assert.h>
20
21 #include <core/core.h>
22 #include <core/event.h>
23 #include <core/game.h>
24 #include <core/image.h>
25 #include <core/painter.h>
26 #include <core/panic.h>
27 #include <core/state.h>
28 #include <core/texture.h>
29 #include <core/util.h>
30 #include <core/window.h>
31
32 #include <ui/notify.h>
33 #include <ui/label.h>
34 #include <ui/ui.h>
35
36 /* Sword by Icongeek26 (https://www.flaticon.com). */
37 #include <assets/images/sword.h>
38
39 #define W 1280
40 #define H 720
41
42 static struct label help = {
43 .text = "Keys: <Space> to generate a notification.",
44 .x = 10,
45 .y = 10,
46 .flags = LABEL_FLAGS_SHADOW
47 };
48 static struct texture icon;
49 static struct state *states[1];
50
51 static void
52 init(void)
53 {
54 if (core_init("fr.malikania", "example-notify") < 0 || ui_init() < 0)
55 panic();
56 if (window_open("Example - Notify", W, H) < 0)
57 panic();
58 if (image_openmem(&icon, assets_sword, sizeof (assets_sword)) < 0)
59 panic();
60 }
61
62 static void
63 handle(struct state *st, const union event *ev)
64 {
65 (void)st;
66
67 switch (ev->type) {
68 case EVENT_QUIT:
69 game_quit();
70 break;
71 case EVENT_KEYDOWN:
72 if (ev->key.key == KEY_SPACE)
73 notify(&icon, "Quest", "Quest finished.");
74 break;
75 default:
76 break;
77 }
78 }
79
80 static void
81 update(struct state *st, unsigned int ticks)
82 {
83 (void)st;
84
85 notify_update(ticks);
86 }
87
88 static void
89 draw(struct state *st)
90 {
91 (void)st;
92
93 painter_set_color(0xffffffff);
94 painter_clear();
95 label_draw(&help);
96 notify_draw();
97 painter_present();
98 }
99
100 static void
101 run(void)
102 {
103 struct state state = {
104 .handle = handle,
105 .update = update,
106 .draw = draw
107 };
108
109 game_init(states, UTIL_SIZE(states));
110 game_push(&state);
111 game_loop();
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 return 0;
133 }