annotate libadventure/adventure/state/splashscreen.c @ 227:befa2e855d3b

core: reinterface the alloc module
author David Demelier <markand@malikania.fr>
date Thu, 19 Nov 2020 10:48:46 +0100
parents ddfe0a211169
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
2 * splashscreen.c -- splash screen state
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
19 #include <assert.h>
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
20 #include <stdlib.h>
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
21 #include <string.h>
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
22
182
f6497ec74b49 core: add alloc module, closes #2512
David Demelier <markand@malikania.fr>
parents: 174
diff changeset
23 #include <core/alloc.h>
121
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
24 #include <core/font.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
25 #include <core/game.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
26 #include <core/image.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
27 #include <core/painter.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
28 #include <core/panic.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
29 #include <core/state.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
30 #include <core/sys.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
31 #include <core/texture.h>
789b23e01f52 misc: reorganize hierarchy, closes #2490
David Demelier <markand@malikania.fr>
parents: 97
diff changeset
32 #include <core/window.h>
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
33
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
34 #include <ui/align.h>
148
c577c15df07f misc: split libraries, closes #2496
David Demelier <markand@malikania.fr>
parents: 134
diff changeset
35
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
36 #include <adventure/assets/fonts/cubic.h>
131
c679e08b32b2 molko: fix startup
David Demelier <markand@malikania.fr>
parents: 121
diff changeset
37
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
38 #include "splashscreen.h"
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
39 #include "mainmenu.h"
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
40
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
41 #define DELAY 3000
80
05ffbcdee585 adventure: create main menu, closes #2482 @4h
David Demelier <markand@malikania.fr>
parents: 79
diff changeset
42
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
43 struct splashscreen {
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
44 struct texture tex;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
45 int x;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
46 int y;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
47 struct state *next;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
48 unsigned int elapsed;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
49 };
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
50
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
51 static struct splashscreen *
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
52 init(struct state *next)
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 {
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
54 struct splashscreen *splash;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
55 struct font font;
80
05ffbcdee585 adventure: create main menu, closes #2482 @4h
David Demelier <markand@malikania.fr>
parents: 79
diff changeset
56
227
befa2e855d3b core: reinterface the alloc module
David Demelier <markand@malikania.fr>
parents: 212
diff changeset
57 splash = alloc_new0(sizeof (*splash));
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
58 splash->next = next;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
59
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
60 if (!font_openmem(&font, fonts_cubic, sizeof (fonts_cubic), 80))
95
e82eca4f8606 core: simplify error/panic routines
David Demelier <markand@malikania.fr>
parents: 94
diff changeset
61 panic();
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
62
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
63 font.style = FONT_STYLE_ANTIALIASED;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
64
174
6992085d47fd ui: major theme overhaul, closes #2509 @1h
David Demelier <markand@malikania.fr>
parents: 162
diff changeset
65 if (!font_render(&font, &splash->tex, "malikania", 0x19332dff))
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
66 panic();
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
67
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
68 align(ALIGN_CENTER, &splash->x, &splash->y, splash->tex.w, splash->tex.h,
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
69 0, 0, window.w, window.h);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
70 font_finish(&font);
80
05ffbcdee585 adventure: create main menu, closes #2482 @4h
David Demelier <markand@malikania.fr>
parents: 79
diff changeset
71
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
72 return splash;
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
74
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 static void
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
76 update(struct state *state, unsigned int ticks)
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 {
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
78 struct splashscreen *splash = state->data;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
79
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
80 splash->elapsed += ticks;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
81
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
82 if (splash->elapsed >= DELAY)
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
83 game_switch(splash->next, false);
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
85
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 static void
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
87 draw(struct state *state)
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 {
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
89 struct splashscreen *splash = state->data;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
90
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
91 painter_set_color(0xffffffff);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
92 painter_clear();
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
93 texture_draw(&splash->tex, splash->x, splash->y);
212
ddfe0a211169 rpg: experiment with map teleport
David Demelier <markand@malikania.fr>
parents: 197
diff changeset
94 painter_present();
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
96
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 static void
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
98 finish(struct state *state)
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 {
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
100 struct splashscreen *splash = state->data;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
101
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
102 texture_finish(&splash->tex);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
103
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
104 free(splash);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
105 memset(state, 0, sizeof (*state));
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
106 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
107
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
108 void
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
109 splashscreen_state(struct state *state, struct state *next)
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 {
162
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
111 assert(state);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
112 assert(next);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
113
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
114 memset(state, 0, sizeof (*state));
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
115 state->data = init(next);
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
116 state->update = update;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
117 state->draw = draw;
629f55f3961e core: rework states
David Demelier <markand@malikania.fr>
parents: 148
diff changeset
118 state->finish = finish;
59
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 }