comparison libmlk-adventure/adventure/state/splashscreen.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents libadventure/adventure/state/splashscreen.c@befa2e855d3b
children 16be1ad3ddba
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
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/alloc.h>
24 #include <core/font.h>
25 #include <core/game.h>
26 #include <core/image.h>
27 #include <core/painter.h>
28 #include <core/panic.h>
29 #include <core/state.h>
30 #include <core/sys.h>
31 #include <core/texture.h>
32 #include <core/window.h>
33
34 #include <ui/align.h>
35
36 #include <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 = alloc_new0(sizeof (*splash));
58 splash->next = next;
59
60 if (!font_openmem(&font, fonts_cubic, sizeof (fonts_cubic), 80))
61 panic();
62
63 font.style = FONT_STYLE_ANTIALIASED;
64
65 if (!font_render(&font, &splash->tex, "malikania", 0x19332dff))
66 panic();
67
68 align(ALIGN_CENTER, &splash->x, &splash->y, splash->tex.w, splash->tex.h,
69 0, 0, window.w, window.h);
70 font_finish(&font);
71
72 return splash;
73 }
74
75 static void
76 update(struct state *state, unsigned int ticks)
77 {
78 struct splashscreen *splash = state->data;
79
80 splash->elapsed += ticks;
81
82 if (splash->elapsed >= DELAY)
83 game_switch(splash->next, false);
84 }
85
86 static void
87 draw(struct state *state)
88 {
89 struct splashscreen *splash = state->data;
90
91 painter_set_color(0xffffffff);
92 painter_clear();
93 texture_draw(&splash->tex, splash->x, splash->y);
94 painter_present();
95 }
96
97 static void
98 finish(struct state *state)
99 {
100 struct splashscreen *splash = state->data;
101
102 texture_finish(&splash->tex);
103
104 free(splash);
105 memset(state, 0, sizeof (*state));
106 }
107
108 void
109 splashscreen_state(struct state *state, struct state *next)
110 {
111 assert(state);
112 assert(next);
113
114 memset(state, 0, sizeof (*state));
115 state->data = init(next);
116 state->update = update;
117 state->draw = draw;
118 state->finish = finish;
119 }