Removed remaining SDL_mixer includes
[neverball] / ball / st_start.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "gui.h"
16 #include "set.h"
17 #include "util.h"
18 #include "game.h"
19 #include "levels.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "st_shared.h"
23
24 #include "st_set.h"
25 #include "st_over.h"
26 #include "st_level.h"
27 #include "st_start.h"
28 #include "st_title.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 #define START_BACK      -1
33 #define START_PRACTICE  -2
34 #define START_NORMAL    -3
35 #define START_CHALLENGE -4
36
37 static int shot_id;
38 static int status_id;
39
40 /*---------------------------------------------------------------------------*/
41
42 /* Create a level selector button based upon its existence and status. */
43
44 static void gui_level(int id, int i)
45 {
46     const GLfloat *fore, *back;
47
48     const struct set *s = curr_set();
49     const struct level *l;
50
51     int jd;
52
53     if (!set_level_exists(s, i))
54     {
55         gui_space(id);
56         return;
57     }
58
59     l = get_level(i);
60
61     if (!l->is_locked)
62     {
63         fore = l->is_bonus     ? gui_grn : gui_wht;
64         back = l->is_completed ? fore    : gui_yel;
65     }
66     else
67         fore = back = gui_gry;
68
69     jd = gui_label(id, l->repr, GUI_SML, GUI_ALL, back, fore);
70
71     gui_active(jd, i, 0);
72 }
73
74 static void start_over_level(int i)
75 {
76     const struct level *l = get_level(i);
77     if (!l->is_locked || config_cheat())
78     {
79         gui_set_image(shot_id, l->shot);
80
81         set_most_coins(&l->score.most_coins, -1);
82
83         if (curr_mode() == MODE_PRACTICE)
84         {
85             set_best_times(&l->score.best_times, -1, 0);
86             if (l->is_bonus)
87                 gui_set_label(status_id,
88                               _("Play this bonus level in practice mode"));
89             else
90                 gui_set_label(status_id,
91                               _("Play this level in practice mode"));
92         }
93         else
94         {
95             set_best_times(&l->score.unlock_goal, -1, 1);
96             if (l->is_bonus)
97                 gui_set_label(status_id,
98                               _("Play this bonus level in normal mode"));
99             else
100                 gui_set_label(status_id, _("Play this level in normal mode"));
101         }
102         if (config_cheat())
103         {
104             gui_set_label(status_id, l->file);
105         }
106         return;
107     }
108     else if (l->is_bonus)
109         gui_set_label(status_id,
110                       _("Play in challenge mode to unlock extra bonus levels"));
111     else
112         gui_set_label(status_id,
113                       _("Finish previous levels to unlock this level"));
114 }
115
116 static void start_over(int id)
117 {
118     int i;
119
120     gui_pulse(id, 1.2f);
121     if (id == 0)
122         return;
123
124     i = gui_token(id);
125
126
127     switch (i)
128     {
129     case START_CHALLENGE:
130         gui_set_image(shot_id, curr_set()->shot);
131         set_most_coins(&curr_set()->coin_score, -1);
132         set_best_times(&curr_set()->time_score, -1, 0);
133         gui_set_label(status_id, _("Challenge all levels from the first one"));
134         break;
135
136     case START_NORMAL:
137         gui_set_label(status_id, _("Collect coins and unlock next level"));
138         break;
139
140     case START_PRACTICE:
141         gui_set_label(status_id, _("Train yourself without time nor coin"));
142         break;
143     }
144
145     if (i >= 0)
146         start_over_level(i);
147 }
148
149 /*---------------------------------------------------------------------------*/
150
151 static int start_action(int i)
152 {
153     int mode = curr_mode();
154
155     audio_play(AUD_MENU, 1.0f);
156
157     switch (i)
158     {
159     case START_BACK:
160         return goto_state(&st_set);
161     case START_NORMAL:
162         mode_set(MODE_NORMAL);
163         return goto_state(&st_start);
164     case START_PRACTICE:
165         mode_set(MODE_PRACTICE);
166         return goto_state(&st_start);
167     }
168
169     if (i == START_CHALLENGE)
170     {
171         /* On cheat, start challenge mode where you want */
172         if (config_cheat())
173         {
174             mode_set(MODE_CHALLENGE);
175             return goto_state(&st_start);
176         }
177         i = 0;
178         mode = MODE_CHALLENGE;
179     }
180
181     if (i >= 0)
182     {
183         const struct level *l = get_level(i);
184
185         if (!l->is_locked || config_cheat())
186         {
187             if (level_play(l, mode))
188             {
189                 return goto_state(&st_level);
190             }
191             else
192             {
193                 level_stop();
194                 return 1;
195             }
196         }
197     }
198     return 1;
199 }
200
201 static int start_enter(void)
202 {
203     int w = config_get_d(CONFIG_WIDTH);
204     int h = config_get_d(CONFIG_HEIGHT);
205     int m = curr_mode();
206     int i, j;
207
208     int id, jd, kd, ld;
209
210     /* Deactivate cheat */
211
212     if (m == MODE_CHALLENGE && !config_cheat())
213     {
214         mode_set(MODE_NORMAL);
215         m = MODE_NORMAL;
216     }
217
218     if ((id = gui_vstack(0)))
219     {
220         if ((jd = gui_hstack(id)))
221         {
222
223             gui_label(jd, _(curr_set()->name), GUI_SML, GUI_ALL,
224                       gui_yel, gui_red);
225             gui_filler(jd);
226             gui_start(jd, _("Back"),  GUI_SML, START_BACK, 0);
227         }
228
229
230         if ((jd = gui_harray(id)))
231         {
232             shot_id = gui_image(jd, curr_set()->shot, 7 * w / 16, 7 * h / 16);
233
234             if ((kd = gui_varray(jd)))
235             {
236                 if ((ld = gui_harray(kd)))
237                 {
238                     gui_state(ld, _("Practice"), GUI_SML, START_PRACTICE,
239                               m == MODE_PRACTICE);
240                     gui_state(ld, _("Normal"),   GUI_SML, START_NORMAL,
241                               m == MODE_NORMAL);
242                 }
243                 for (i = 0; i < 5; i++)
244                     if ((ld = gui_harray(kd)))
245                         for (j = 4; j >= 0; j--)
246                             gui_level(ld, i * 5 + j);
247
248                 gui_state(kd, _("Challenge"), GUI_SML, START_CHALLENGE,
249                           m == MODE_CHALLENGE);
250             }
251         }
252         gui_space(id);
253
254         if ((jd = gui_harray(id)))
255         {
256             gui_most_coins(jd, 0);
257             gui_best_times(jd, 0);
258         }
259         gui_space(id);
260
261         status_id = gui_label(id, _("Choose a level to play"), GUI_SML, GUI_ALL,
262                               gui_yel, gui_wht);
263
264         gui_layout(id, 0, 0);
265
266         set_most_coins(NULL, -1);
267         set_best_times(NULL, -1, m != MODE_PRACTICE);
268     }
269
270     audio_music_fade_to(0.5f, "bgm/inter.ogg");
271
272     return id;
273 }
274
275 static void start_point(int id, int x, int y, int dx, int dy)
276 {
277     start_over(gui_point(id, x, y));
278 }
279
280 static void start_stick(int id, int a, int v)
281 {
282     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
283     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
284
285     start_over(gui_stick(id, x, y));
286 }
287
288 static int start_keybd(int c, int d)
289 {
290     if (d && c == SDLK_c && config_cheat())
291     {
292         set_cheat();
293         return goto_state(&st_start);
294     }
295
296     if (d && c == SDLK_F12)
297     {
298         int i;
299
300         /* Iterate over all levels, taking a screenshot of each. */
301
302         for (i = 0; i < MAXLVL; i++)
303             if (set_level_exists(curr_set(), i))
304                 level_snap(i);
305     }
306
307     return 1;
308 }
309
310 static int start_buttn(int b, int d)
311 {
312     if (d)
313     {
314         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
315             return start_action(gui_token(gui_click()));
316         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
317             return start_action(START_BACK);
318     }
319     return 1;
320 }
321
322 /*---------------------------------------------------------------------------*/
323
324 struct state st_start = {
325     start_enter,
326     shared_leave,
327     shared_paint,
328     shared_timer,
329     start_point,
330     start_stick,
331     shared_click,
332     start_keybd,
333     start_buttn,
334     1, 0
335 };