comparison src/libmlk-rpg/rpg/battle-state-closing.c @ 382:43d155668a55

rpg: expose battle state functions
author David Demelier <markand@malikania.fr>
date Sun, 13 Feb 2022 10:35:26 +0100
parents 460c78706989
children c458441ff472
comparison
equal deleted inserted replaced
381:f48367c5a92e 382:43d155668a55
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <assert.h> 19 #include <assert.h>
20 #include <stdlib.h> 20 #include <stdlib.h>
21 #include <string.h>
21 22
22 #include <core/alloc.h> 23 #include <core/alloc.h>
23 #include <core/music.h> 24 #include <core/music.h>
24 #include <core/painter.h> 25 #include <core/painter.h>
25 #include <core/panic.h> 26 #include <core/panic.h>
26 #include <core/texture.h> 27 #include <core/texture.h>
27 #include <core/window.h> 28 #include <core/window.h>
28 29
29 #include "battle.h" 30 #include "battle.h"
31 #include "battle-state-closing.h"
30 32
31 struct closing { 33 struct self {
32 struct battle_state self; 34 struct battle_state_closing data;
33 struct texture texture; 35 struct battle_state state;
34 unsigned int alpha;
35 unsigned int elapsed;
36 }; 36 };
37 37
38 static int 38 static int
39 update(struct battle_state *st, struct battle *bt, unsigned int ticks) 39 update(struct battle_state *st, struct battle *bt, unsigned int ticks)
40 { 40 {
41 (void)bt; 41 (void)bt;
42 42
43 struct closing *closing = st->data; 43 return battle_state_closing_update(st->data, ticks);
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 } 44 }
61 45
62 static void 46 static void
63 draw(const struct battle_state *st, const struct battle *bt) 47 draw(const struct battle_state *st, const struct battle *bt)
64 { 48 {
65 (void)bt; 49 (void)bt;
66 50
67 const struct closing *closing = st->data; 51 battle_state_closing_draw(st->data);
68
69 texture_draw(&closing->texture, 0, 0);
70 } 52 }
71 53
72 static void 54 static void
73 finish(struct battle_state *st, struct battle *bt) 55 finish(struct battle_state *st, struct battle *bt)
74 { 56 {
75 (void)bt; 57 (void)bt;
76 58
77 struct closing *closing = st->data; 59 battle_state_closing_finish(st->data);
60 free(st->data);
61 }
78 62
79 texture_finish(&closing->texture); 63 void
80 free(closing); 64 battle_state_closing_init(struct battle_state_closing *cls)
65 {
66 assert(cls);
67
68 if (texture_new(&cls->texture, window.w, window.h) < 0)
69 panic();
70
71 PAINTER_BEGIN(&cls->texture);
72 texture_set_blend_mode(&cls->texture, TEXTURE_BLEND_BLEND);
73 painter_set_color(0x000000ff);
74 painter_clear();
75 painter_draw_rectangle(0, 0, window.w, window.h);
76 PAINTER_END();
77
78 texture_set_alpha_mod(&cls->texture, 0);
79 }
80
81 int
82 battle_state_closing_update(struct battle_state_closing *cls, unsigned int ticks)
83 {
84 assert(cls);
85
86 cls->elapsed += ticks;
87
88 /* TODO: ??? */
89 if (cls->elapsed > 8) {
90 cls->elapsed = 0;
91
92 if (cls->alpha == 255) {
93 /* TODO: since OpenAL, no notion of global music. */
94 #if 0
95 music_stop(0);
96 #endif
97 return 1;
98 }
99
100 cls->alpha += 5;
101 texture_set_alpha_mod(&cls->texture, cls->alpha);
102 }
103
104 return 0;
105 }
106
107 void
108 battle_state_closing_draw(struct battle_state_closing *cls)
109 {
110 assert(cls);
111
112 texture_draw(&cls->texture, 0, 0);
113 }
114
115 void
116 battle_state_closing_finish(struct battle_state_closing *cls)
117 {
118 assert(cls);
119
120 texture_finish(&cls->texture);
121 memset(cls, 0, sizeof (*cls));
81 } 122 }
82 123
83 void 124 void
84 battle_state_closing(struct battle *bt) 125 battle_state_closing(struct battle *bt)
85 { 126 {
86 assert(bt); 127 assert(bt);
87 128
88 struct closing *closing; 129 struct self *self;
89 130
90 if (!(closing = alloc_new0(sizeof (*closing))) || 131 self = alloc_new0(sizeof (*self));
91 texture_new(&closing->texture, window.w, window.h) < 0) 132 self->state.data = self;
92 panic(); 133 self->state.update = update;
134 self->state.draw = draw;
135 self->state.finish = finish;
93 136
94 PAINTER_BEGIN(&closing->texture); 137 battle_state_closing_init(&self->data);
95 texture_set_blend_mode(&closing->texture, TEXTURE_BLEND_BLEND); 138 battle_switch(bt, &self->state);
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 } 139 }