comparison libadventure/adventure/state/splashscreen.c @ 162:629f55f3961e

core: rework states
author David Demelier <markand@malikania.fr>
date Sun, 18 Oct 2020 12:01:59 +0200
parents libadventure/adventure/splashscreen_state.c@c577c15df07f
children 6992085d47fd
comparison
equal deleted inserted replaced
161:31d7f23c0588 162:629f55f3961e
1 /*
2 * splashscreen.c -- splash screen state
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 <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <core/font.h>
24 #include <core/game.h>
25 #include <core/image.h>
26 #include <core/painter.h>
27 #include <core/panic.h>
28 #include <core/state.h>
29 #include <core/sys.h>
30 #include <core/texture.h>
31 #include <core/util.h>
32 #include <core/window.h>
33
34 #include <ui/align.h>
35
36 #include <adventure/assets/fonts/cubic.h>
37
38 #include "splashscreen.h"
39 #include "mainmenu.h"
40
41 #define DELAY 3000
42
43 struct splashscreen {
44 struct texture tex;
45 int x;
46 int y;
47 struct state *next;
48 unsigned int elapsed;
49 };
50
51 static struct splashscreen *
52 init(struct state *next)
53 {
54 struct splashscreen *splash;
55 struct font font;
56
57 splash = ecalloc(1, sizeof (*splash));
58 splash->next = next;
59
60 if (!font_openmem(&font, fonts_cubic, sizeof (fonts_cubic), 80))
61 panic();
62
63 font.color = 0x19332dff;
64 font.style = FONT_STYLE_ANTIALIASED;
65
66 if (!font_render(&font, &splash->tex, "malikania"))
67 panic();
68
69 align(ALIGN_CENTER, &splash->x, &splash->y, splash->tex.w, splash->tex.h,
70 0, 0, window.w, window.h);
71 font_finish(&font);
72
73 return splash;
74 }
75
76 static void
77 update(struct state *state, unsigned int ticks)
78 {
79 struct splashscreen *splash = state->data;
80
81 splash->elapsed += ticks;
82
83 if (splash->elapsed >= DELAY)
84 game_switch(splash->next, false);
85 }
86
87 static void
88 draw(struct state *state)
89 {
90 struct splashscreen *splash = state->data;
91
92 painter_set_color(0xffffffff);
93 painter_clear();
94 texture_draw(&splash->tex, splash->x, splash->y);
95 }
96
97 static void
98 finish(struct state *state)
99 {
100 struct splashscreen *splash = state->data;
101
102 if (!splash)
103 return;
104
105 texture_finish(&splash->tex);
106
107 free(splash);
108 memset(state, 0, sizeof (*state));
109 }
110
111 void
112 splashscreen_state(struct state *state, struct state *next)
113 {
114 assert(state);
115 assert(next);
116
117 memset(state, 0, sizeof (*state));
118 state->data = init(next);
119 state->update = update;
120 state->draw = draw;
121 state->finish = finish;
122 }