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