comparison src/libmlk-rpg/rpg/battle-state-closing.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-closing.c@d01e83210ca2
children 460c78706989
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * battle-state-closing.c -- battle state (closing)
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/music.h>
24 #include <core/painter.h>
25 #include <core/panic.h>
26 #include <core/texture.h>
27 #include <core/window.h>
28
29 #include "battle.h"
30
31 struct closing {
32 struct battle_state self;
33 struct texture texture;
34 unsigned int alpha;
35 unsigned int elapsed;
36 };
37
38 static int
39 update(struct battle_state *st, struct battle *bt, unsigned int ticks)
40 {
41 (void)bt;
42
43 struct closing *closing = st->data;
44
45 closing->elapsed += ticks;
46
47 if (closing->elapsed > 8) {
48 closing->elapsed = 0;
49
50 if (closing->alpha == 255) {
51 music_stop(0);
52 return 1;
53 }
54
55 closing->alpha += 5;
56 texture_set_alpha_mod(&closing->texture, closing->alpha);
57 }
58
59 return 0;
60 }
61
62 static void
63 draw(const struct battle_state *st, const struct battle *bt)
64 {
65 (void)bt;
66
67 const struct closing *closing = st->data;
68
69 texture_draw(&closing->texture, 0, 0);
70 }
71
72 static void
73 finish(struct battle_state *st, struct battle *bt)
74 {
75 (void)bt;
76
77 struct closing *closing = st->data;
78
79 texture_finish(&closing->texture);
80 free(closing);
81 }
82
83 void
84 battle_state_closing(struct battle *bt)
85 {
86 assert(bt);
87
88 struct closing *closing;
89
90 if (!(closing = alloc_new0(sizeof (*closing))) ||
91 texture_new(&closing->texture, window.w, window.h) < 0)
92 panic();
93
94 PAINTER_BEGIN(&closing->texture);
95 texture_set_blend_mode(&closing->texture, TEXTURE_BLEND_BLEND);
96 painter_set_color(0x000000ff);
97 painter_clear();
98 painter_draw_rectangle(0, 0, window.w, window.h);
99 PAINTER_END();
100
101 texture_set_alpha_mod(&closing->texture, 0);
102
103 closing->self.data = closing;
104 closing->self.update = update;
105 closing->self.draw = draw;
106 closing->self.finish = finish;
107
108 battle_switch(bt, &closing->self);
109 }