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