Mercurial > molko
comparison src/libmlk-rpg/rpg/battle-state-opening.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-rpg/rpg/battle-state-opening.c@d01e83210ca2 |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * battle-state-opening.c -- battle state (opening) | |
3 * | |
4 * Copyright (c) 2020-2021 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 | |
22 #include <core/alloc.h> | |
23 #include <core/painter.h> | |
24 #include <core/panic.h> | |
25 #include <core/window.h> | |
26 | |
27 #include "battle.h" | |
28 #include "battle-state.h" | |
29 | |
30 #define DELAY (1000U) | |
31 | |
32 struct opening { | |
33 struct battle_state self; | |
34 unsigned int elapsed; | |
35 }; | |
36 | |
37 static int | |
38 update(struct battle_state *st, struct battle *bt, unsigned int ticks) | |
39 { | |
40 assert(bt); | |
41 | |
42 struct opening *opening = st->data; | |
43 | |
44 opening->elapsed += ticks; | |
45 | |
46 /* | |
47 * Those function will effectively change state accordingly to the | |
48 * order of playing. | |
49 */ | |
50 if (opening->elapsed >= DELAY) | |
51 battle_state_check(bt); | |
52 | |
53 return 0; | |
54 } | |
55 | |
56 static void | |
57 draw(const struct battle_state *st, const struct battle *bt) | |
58 { | |
59 (void)bt; | |
60 | |
61 assert(bt); | |
62 | |
63 const struct opening *opening = st->data; | |
64 const unsigned int w = window.w; | |
65 const unsigned int h = window.h / 2; | |
66 const unsigned int ch = opening->elapsed * h / DELAY; | |
67 | |
68 /* Draw some bezels opening. */ | |
69 painter_set_color(0x000000ff); | |
70 painter_draw_rectangle(0, 0, w, h - ch); | |
71 painter_draw_rectangle(0, h + ch, w, h - ch); | |
72 } | |
73 | |
74 static void | |
75 finish(struct battle_state *st, struct battle *bt) | |
76 { | |
77 (void)bt; | |
78 | |
79 free(st->data); | |
80 } | |
81 | |
82 void | |
83 battle_state_opening(struct battle *bt) | |
84 { | |
85 assert(bt); | |
86 | |
87 struct opening *opening; | |
88 | |
89 if (!(opening = alloc_new0(sizeof (*opening)))) | |
90 panic(); | |
91 | |
92 opening->self.data = opening; | |
93 opening->self.update = update; | |
94 opening->self.draw = draw; | |
95 opening->self.finish = finish; | |
96 | |
97 battle_switch(bt, &opening->self); | |
98 } |