annotate src/adventure/splashscreen_state.c @ 59:52792b863ff7

misc: separate core from game
author David Demelier <markand@malikania.fr>
date Tue, 21 Jan 2020 12:42:33 +0100
parents
children 8f95462ac5f8
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 /*
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * splashscreen_state.c -- splash screen state
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
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include "error.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include "font.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include "game.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include "image.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include "map.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include "map_state.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include "painter.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 #include "splashscreen_state.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 #include "state.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 #include "sys.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 #include "texture.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 #include "window.h"
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
31
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 #define DELAY 3000
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
33
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 static unsigned int elapsed;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 static struct font *font;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 static struct texture *text;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 static int x;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 static int y;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
39
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 static void
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 enter(void)
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 if (!(font = font_openf(sys_datapath("fonts/knights-quest.ttf"), 160)))
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 error_fatal();
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 if (!(text = font_render(font, "Molko's Adventure", 0x000000ff)))
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 error_fatal();
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
47
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 /* Compute position. */
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 const unsigned int w = texture_width(text);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 const unsigned int h = texture_height(text);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
51
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 x = (window_width() / 2) - (w / 2);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 y = (window_height() / 2) - (h / 2) - 100;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
55
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 static void
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 leave(void)
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 font_close(font);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
61
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 static void
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 handle(const union event *event)
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 (void)event;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
67
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 static void
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 update(unsigned int ticks)
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
71 elapsed += ticks;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
72
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 /* TODO: change this once map is done. */
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 if (elapsed >= DELAY) {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 /* TODO: this will be removed too. */
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 static struct texture *image;
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
77
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 if (!map_open(&map_state_data.map.map, sys_datapath("maps/test.map")))
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 error_fatal();
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 if (!(image = image_openf(sys_datapath("sprites/test-walk.png"))))
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 error_fatal();
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
82
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 sprite_init(&map_state_data.player.sprite, image, 48, 48);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 game_switch(&map_state, false);
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 }
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
87
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 static void
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 draw(void)
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 painter_set_color(0xffffffff);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 painter_clear();
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 texture_draw(text, x, y);
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 painter_present();
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 struct state splashscreen_state = {
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 .enter = enter,
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 .leave = leave,
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 .handle = handle,
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 .update = update,
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 .draw = draw
52792b863ff7 misc: separate core from game
David Demelier <markand@malikania.fr>
parents:
diff changeset
103 };