Mercurial > molko
comparison libmlk-adventure/adventure/state/mainmenu.c @ 259:16be1ad3ddba
adventure: start working on maps and teleport
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 06 Dec 2020 11:22:03 +0100 |
parents | 71b3b7036de7 |
children | bfde372bf152 |
comparison
equal
deleted
inserted
replaced
258:f978fa0137ce | 259:16be1ad3ddba |
---|---|
41 | 41 |
42 #include "adventure_p.h" | 42 #include "adventure_p.h" |
43 #include "mainmenu.h" | 43 #include "mainmenu.h" |
44 #include "molko.h" | 44 #include "molko.h" |
45 | 45 |
46 struct mainmenu { | 46 struct self { |
47 struct state state; | |
48 | |
47 struct { | 49 struct { |
48 struct texture tex; | 50 struct texture tex; |
49 int x; | 51 int x; |
50 int y; | 52 int y; |
51 } texts[4]; | 53 } texts[4]; |
54 }; | 56 }; |
55 | 57 |
56 static void | 58 static void |
57 new(void) | 59 new(void) |
58 { | 60 { |
61 molko_teleport("assets/maps/map-world.map", -1, -1); | |
62 } | |
63 | |
64 static void | |
65 resume(void) | |
66 { | |
59 /* TODO: implement here. */ | 67 /* TODO: implement here. */ |
60 if (!map_file_open(&molko.map_file, &molko.map, DIRECTORY "/maps/overworld.map")) | |
61 panic(); | |
62 | |
63 /* Put a sprite. */ | |
64 if (!image_open(&molko.map_player_texture, DIRECTORY "/sprites/john.png")) | |
65 panic(); | |
66 | |
67 sprite_init(&molko.map_player_sprite, &molko.map_player_texture, 48, 48); | |
68 molko.map.player_sprite = &molko.map_player_sprite; | |
69 | |
70 if (!map_init(&molko.map)) | |
71 panic(); | |
72 | |
73 game_switch(&molko.states[MOLKO_STATE_MAP], false); | |
74 } | |
75 | |
76 static void | |
77 resume(void) | |
78 { | |
79 /* TODO: implement here. */ | |
80 } | 68 } |
81 | 69 |
82 static void | 70 static void |
83 quit(void) | 71 quit(void) |
84 { | 72 { |
85 game_quit(); | 73 game_quit(); |
86 } | 74 } |
87 | 75 |
88 static void | 76 static void |
89 perform(struct mainmenu *main) | 77 perform(struct self *self) |
90 { | 78 { |
91 assert(main->itemsel < 3); | 79 assert(self->itemsel < 3); |
92 | 80 |
93 static void (*handlers[])(void) = { | 81 static void (*handlers[])(void) = { |
94 [0] = new, | 82 [0] = new, |
95 [1] = resume, | 83 [1] = resume, |
96 [2] = quit | 84 [2] = quit |
97 }; | 85 }; |
98 | 86 |
99 handlers[main->itemsel](); | 87 handlers[self->itemsel](); |
100 } | 88 } |
101 | 89 |
102 static void | 90 static void |
103 init_title(struct mainmenu *main, struct font *font) | 91 init_title(struct self *self, struct font *font) |
104 { | 92 { |
105 if (!font_render(font, &main->texts[3].tex, "Molko's Adventure", 0x000000ff)) | 93 if (!font_render(font, &self->texts[3].tex, "Molko's Adventure", 0x000000ff)) |
106 panic(); | 94 panic(); |
107 | 95 |
108 /* Align header. */ | 96 /* Align header. */ |
109 align(ALIGN_CENTER, &main->texts[3].x, NULL, main->texts[3].tex.w, main->texts[3].tex.h, | 97 align(ALIGN_CENTER, &self->texts[3].x, NULL, self->texts[3].tex.w, self->texts[3].tex.h, |
110 0, 0, window.w, window.h); | 98 0, 0, window.w, window.h); |
111 | 99 |
112 main->texts[3].y = main->texts[3].x; | 100 self->texts[3].y = self->texts[3].x; |
113 } | 101 } |
114 | 102 |
115 static void | 103 static void |
116 init_items(struct mainmenu *main, struct font *font) | 104 init_items(struct self *self, struct font *font) |
117 { | 105 { |
118 if (!font_render(font, &main->texts[0].tex, _("New"), 0x000000ff) || | 106 if (!font_render(font, &self->texts[0].tex, _("New"), 0x000000ff) || |
119 !font_render(font, &main->texts[1].tex, _("Continue"), 0x000000ff) || | 107 !font_render(font, &self->texts[1].tex, _("Continue"), 0x000000ff) || |
120 !font_render(font, &main->texts[2].tex, _("Quit"), 0x000000ff)) | 108 !font_render(font, &self->texts[2].tex, _("Quit"), 0x000000ff)) |
121 panic(); | 109 panic(); |
122 | 110 |
123 main->texts[0].x = (window.w / 2) - (main->texts[0].tex.w / 2); | 111 self->texts[0].x = (window.w / 2) - (self->texts[0].tex.w / 2); |
124 main->texts[0].y = window.h * 0.75; | 112 self->texts[0].y = window.h * 0.75; |
125 | 113 |
126 main->texts[1].x = main->texts[0].x; | 114 self->texts[1].x = self->texts[0].x; |
127 main->texts[1].y = main->texts[0].y + main->texts[0].tex.h; | 115 self->texts[1].y = self->texts[0].y + self->texts[0].tex.h; |
128 | 116 |
129 main->texts[2].x = main->texts[0].x; | 117 self->texts[2].x = self->texts[0].x; |
130 main->texts[2].y = main->texts[1].y + main->texts[1].tex.h; | 118 self->texts[2].y = self->texts[1].y + self->texts[1].tex.h; |
131 } | 119 } |
132 | 120 |
133 static void | 121 static void |
134 start(struct state *state) | 122 start(struct state *state) |
135 { | 123 { |
136 struct mainmenu *main; | 124 struct self *self = state->data; |
137 struct font fonts[2]; | 125 struct font fonts[2]; |
138 | |
139 /* Allocate the main menu data. */ | |
140 main = (state->data = alloc_new0(sizeof (*main))); | |
141 | 126 |
142 if (!font_openmem(&fonts[0], fonts_teutonic, sizeof (fonts_teutonic), 130) || | 127 if (!font_openmem(&fonts[0], fonts_teutonic, sizeof (fonts_teutonic), 130) || |
143 !font_openmem(&fonts[1], fonts_pirata_one, sizeof (fonts_pirata_one), 30)) | 128 !font_openmem(&fonts[1], fonts_pirata_one, sizeof (fonts_pirata_one), 30)) |
144 panic(); | 129 panic(); |
145 | 130 |
146 fonts[0].style = fonts[1].style = FONT_STYLE_ANTIALIASED; | 131 fonts[0].style = fonts[1].style = FONT_STYLE_ANTIALIASED; |
147 | 132 |
148 init_title(main, &fonts[0]); | 133 init_title(self, &fonts[0]); |
149 init_items(main, &fonts[1]); | 134 init_items(self, &fonts[1]); |
150 | 135 |
151 font_finish(&fonts[0]); | 136 font_finish(&fonts[0]); |
152 font_finish(&fonts[1]); | 137 font_finish(&fonts[1]); |
153 } | 138 } |
154 | 139 |
155 static void | 140 static void |
156 handle(struct state *state, const union event *event) | 141 handle(struct state *state, const union event *event) |
157 { | 142 { |
158 struct mainmenu *main = state->data; | 143 struct self *self = state->data; |
159 | 144 |
160 switch (event->type) { | 145 switch (event->type) { |
161 case EVENT_KEYDOWN: | 146 case EVENT_KEYDOWN: |
162 switch (event->key.key) { | 147 switch (event->key.key) { |
163 case KEY_UP: | 148 case KEY_UP: |
164 main->itemsel = main->itemsel == 0 ? 2 : main->itemsel - 1; | 149 self->itemsel = self->itemsel == 0 ? 2 : self->itemsel - 1; |
165 break; | 150 break; |
166 case KEY_DOWN: | 151 case KEY_DOWN: |
167 main->itemsel = (main->itemsel + 1) % 3; | 152 self->itemsel = (self->itemsel + 1) % 3; |
168 break; | 153 break; |
169 case KEY_ENTER: | 154 case KEY_ENTER: |
170 perform(main); | 155 perform(self); |
171 break; | 156 break; |
172 default: | 157 default: |
173 break; | 158 break; |
174 } | 159 } |
175 break; | 160 break; |
179 } | 164 } |
180 | 165 |
181 static void | 166 static void |
182 draw(struct state *state) | 167 draw(struct state *state) |
183 { | 168 { |
184 struct mainmenu *main = state->data; | 169 struct self *self = state->data; |
185 | 170 |
186 painter_set_color(0xffffffff); | 171 painter_set_color(0xffffffff); |
187 painter_clear(); | 172 painter_clear(); |
188 | 173 |
189 for (size_t i = 0; i < NELEM(main->texts); ++i) | 174 for (size_t i = 0; i < NELEM(self->texts); ++i) |
190 texture_draw(&main->texts[i].tex, main->texts[i].x, main->texts[i].y); | 175 texture_draw(&self->texts[i].tex, self->texts[i].x, self->texts[i].y); |
191 | 176 |
192 /* TODO: a sword here. */ | 177 /* TODO: a sword here. */ |
193 painter_set_color(0x000000ff); | 178 painter_set_color(0x000000ff); |
194 painter_draw_rectangle( | 179 painter_draw_rectangle( |
195 main->texts[main->itemsel].x - 30, | 180 self->texts[self->itemsel].x - 30, |
196 main->texts[main->itemsel].y + 11, 15, 15); | 181 self->texts[self->itemsel].y + 11, 15, 15); |
197 painter_present(); | 182 painter_present(); |
198 } | 183 } |
199 | 184 |
200 static void | 185 static void |
201 finish(struct state *state) | 186 finish(struct state *state) |
202 { | 187 { |
203 struct mainmenu *main = state->data; | 188 struct self *self = state->data; |
204 | 189 |
205 if (!main) | 190 for (size_t i = 0; i < NELEM(self->texts); ++i) |
206 return; | 191 texture_finish(&self->texts[i].tex); |
207 | 192 |
208 for (size_t i = 0; i < NELEM(main->texts); ++i) | 193 free(self); |
209 texture_finish(&main->texts[i].tex); | 194 } |
210 | 195 |
211 free(main); | 196 struct state * |
212 memset(state, 0, sizeof (*state)); | 197 mainmenu_state_new(void) |
213 } | 198 { |
214 | 199 struct self *self; |
215 void | 200 |
216 mainmenu_state(struct state *state) | 201 self = alloc_new0(sizeof (*self)); |
217 { | 202 self->state.data = self; |
218 assert(state); | 203 self->state.start = start; |
219 | 204 self->state.handle = handle; |
220 memset(state, 0, sizeof (*state)); | 205 self->state.draw = draw; |
221 state->start = start; | 206 self->state.finish = finish; |
222 state->handle = handle; | 207 |
223 state->draw = draw; | 208 return &self->state; |
224 state->finish = finish; | 209 } |
225 } |