share/lang: eliminate redundant lang_init param
[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 "progress.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "st_shared.h"
22
23 #include "game_common.h"
24
25 #include "st_set.h"
26 #include "st_over.h"
27 #include "st_level.h"
28 #include "st_start.h"
29 #include "st_title.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 #define START_BACK        -1
34 #define START_CHALLENGE   -2
35 #define START_OPEN_GOALS  -3
36 #define START_LOCK_GOALS  -4
37
38 static int shot_id;
39 static int file_id;
40 static int challenge_id;
41
42 /*---------------------------------------------------------------------------*/
43
44 /* Create a level selector button based upon its existence and status. */
45
46 static void gui_level(int id, int i)
47 {
48     const GLfloat *fore = 0, *back = 0;
49
50     int jd;
51
52     if (!level_exists(i))
53     {
54         gui_label(id, " ", GUI_SML, GUI_ALL, gui_blk, gui_blk);
55         return;
56     }
57
58     if (level_opened(i))
59     {
60         fore = level_bonus(i)     ? gui_grn : gui_wht;
61         back = level_completed(i) ? fore    : gui_yel;
62     }
63
64     jd = gui_label(id, level_name(i), GUI_SML, GUI_ALL, back, fore);
65
66     if (level_opened(i) || config_cheat())
67         gui_active(jd, i, 0);
68 }
69
70 static void start_over_level(int i)
71 {
72     if (level_opened(i) || config_cheat())
73     {
74         gui_set_image(shot_id, level_shot(i));
75
76         set_score_board(&get_level(i)->score.most_coins,  -1,
77                         &get_level(i)->score.best_times,  -1,
78                         &get_level(i)->score.fast_unlock, -1);
79
80         if (file_id)
81             gui_set_label(file_id, level_file(i));
82     }
83 }
84
85 static void start_over(int id, int pulse)
86 {
87     int i;
88
89     if (id == 0)
90         return;
91
92     if (pulse)
93         gui_pulse(id, 1.2f);
94
95     i = gui_token(id);
96
97     if (i == START_CHALLENGE || i == START_BACK)
98     {
99         gui_set_image(shot_id, set_shot(curr_set()));
100
101         set_score_board(set_coin_score(curr_set()), -1,
102                         set_time_score(curr_set()), -1,
103                         NULL, -1);
104     }
105
106     if (i >= 0 && !GUI_ISMSK(i))
107         start_over_level(i);
108 }
109
110 /*---------------------------------------------------------------------------*/
111
112 static int start_action(int i)
113 {
114     audio_play(AUD_MENU, 1.0f);
115
116     switch (i)
117     {
118     case START_BACK:
119         return goto_state(&st_set);
120
121     case START_CHALLENGE:
122         if (config_cheat())
123         {
124             progress_init(curr_mode() == MODE_CHALLENGE ?
125                           MODE_NORMAL : MODE_CHALLENGE);
126             gui_toggle(challenge_id);
127             return 1;
128         }
129         else
130         {
131             progress_init(MODE_CHALLENGE);
132             return start_action(0);
133         }
134         break;
135
136     case GUI_MOST_COINS:
137     case GUI_BEST_TIMES:
138     case GUI_FAST_UNLOCK:
139         gui_score_set(i);
140         return goto_state(&st_start);
141
142     case START_OPEN_GOALS:
143         config_set_d(CONFIG_LOCK_GOALS, 0);
144         return goto_state(&st_start);
145
146     case START_LOCK_GOALS:
147         config_set_d(CONFIG_LOCK_GOALS, 1);
148         return goto_state(&st_start);
149
150     default:
151         if (progress_play(i))
152             return goto_state(&st_level);
153         break;
154     }
155
156     return 1;
157 }
158
159 static int start_enter(void)
160 {
161     int w = config_get_d(CONFIG_WIDTH);
162     int h = config_get_d(CONFIG_HEIGHT);
163     int i, j;
164
165     int id, jd, kd, ld;
166
167     progress_init(MODE_NORMAL);
168
169     if ((id = gui_vstack(0)))
170     {
171         if ((jd = gui_hstack(id)))
172         {
173
174             gui_label(jd, set_name(curr_set()), GUI_SML, GUI_ALL,
175                       gui_yel, gui_red);
176             gui_filler(jd);
177             gui_start(jd, _("Back"),  GUI_SML, START_BACK, 0);
178         }
179
180         if ((jd = gui_harray(id)))
181         {
182             if (config_cheat())
183             {
184                 if ((kd = gui_vstack(jd)))
185                 {
186                     shot_id = gui_image(kd, set_shot(curr_set()),
187                                         7 * w / 16, 7 * h / 16);
188                     file_id = gui_label(kd, " ", GUI_SML, GUI_ALL,
189                                         gui_yel, gui_red);
190                 }
191             }
192             else
193             {
194                 shot_id = gui_image(jd, set_shot(curr_set()),
195                                     7 * w / 16, 7 * h / 16);
196             }
197
198             if ((kd = gui_varray(jd)))
199             {
200                 for (i = 0; i < 5; i++)
201                     if ((ld = gui_harray(kd)))
202                         for (j = 4; j >= 0; j--)
203                             gui_level(ld, i * 5 + j);
204
205                 challenge_id = gui_state(kd, _("Challenge"),
206                                          GUI_SML, START_CHALLENGE,
207                                          curr_mode() == MODE_CHALLENGE);
208             }
209         }
210         gui_space(id);
211
212         if ((jd = gui_hstack(id)))
213             gui_score_board(jd, GUI_MOST_COINS |
214                                 GUI_BEST_TIMES |
215                                 GUI_FAST_UNLOCK, 0, 0);
216
217         gui_space(id);
218
219         if ((jd = gui_hstack(id)))
220         {
221             gui_filler(jd);
222
223             if ((kd = gui_harray(jd)))
224             {
225                 /* TODO, replace the whitespace hack with something sane. */
226
227                 gui_state(kd,
228                           /* Translators: adjust the amount of whitespace here
229                            * as necessary for the buttons to look good. */
230                           _("   No   "), GUI_SML, START_OPEN_GOALS,
231                           config_get_d(CONFIG_LOCK_GOALS) == 0);
232
233                 gui_state(kd, _("Yes"), GUI_SML, START_LOCK_GOALS,
234                           config_get_d(CONFIG_LOCK_GOALS) == 1);
235             }
236
237             gui_space(jd);
238
239             gui_label(jd, _("Lock Goals of Completed Levels?"),
240                       GUI_SML, GUI_ALL, 0, 0);
241
242             gui_filler(jd);
243         }
244
245         gui_layout(id, 0, 0);
246
247         set_score_board(NULL, -1, NULL, -1, NULL, -1);
248     }
249
250     audio_music_fade_to(0.5f, "bgm/inter.ogg");
251
252     return id;
253 }
254
255 static void start_point(int id, int x, int y, int dx, int dy)
256 {
257     start_over(gui_point(id, x, y), 1);
258 }
259
260 static void start_stick(int id, int a, int v)
261 {
262     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
263     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
264
265     start_over(gui_stick(id, x, y), 1);
266 }
267
268 static int start_keybd(int c, int d)
269 {
270     if (d)
271     {
272         if (c == SDLK_c && config_cheat())
273         {
274             set_cheat();
275             return goto_state(&st_start);
276         }
277         else if (c == SDLK_F12 && config_cheat())
278         {
279             int i;
280
281             /* Iterate over all levels, taking a screenshot of each. */
282
283             for (i = 0; i < MAXLVL; i++)
284                 if (level_exists(i))
285                     level_snap(i);
286         }
287         else if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
288         {
289             int active = gui_click();
290
291             if (start_action(gui_score_next(gui_score_get())))
292             {
293                 /* HACK ALERT
294                  *
295                  * This assumes that 'active' is a valid widget ID even after
296                  * the above start_action has recreated the entire widget
297                  * hierarchy.  Maybe it is.  Maybe it isn't.
298                  */
299                 gui_focus(active);
300                 start_over(active, 0);
301
302                 return 1;
303             }
304             else
305                 return 0;
306         }
307     }
308
309     return 1;
310 }
311
312 static int start_buttn(int b, int d)
313 {
314     if (d)
315     {
316         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
317             return start_action(gui_token(gui_click()));
318         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
319             return start_action(START_BACK);
320     }
321     return 1;
322 }
323
324 /*---------------------------------------------------------------------------*/
325
326 struct state st_start = {
327     start_enter,
328     shared_leave,
329     shared_paint,
330     shared_timer,
331     start_point,
332     start_stick,
333     shared_angle,
334     shared_click,
335     start_keybd,
336     start_buttn,
337     1, 0
338 };