886ec9b5d45bd0918866be5c767f3912f3e3247f
[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 "level.h"
20 #include "audio.h"
21 #include "config.h"
22
23 #include "st_set.h"
24 #include "st_over.h"
25 #include "st_level.h"
26 #include "st_title.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 #define START_BACK 0
31
32 static int shot_id;
33
34 /*---------------------------------------------------------------------------*/
35
36 /* Create a level selector button based upon its existence and status. */
37
38 static void gui_level(int id, char *text, int i)
39 {
40     int o = level_opened(i);
41     int e = level_exists(i);
42
43     if      (o) gui_state(id, text, GUI_SML, i, 0);
44     else if (e) gui_label(id, text, GUI_SML, GUI_ALL, gui_yel, gui_red);
45     else        gui_label(id, text, GUI_SML, GUI_ALL, gui_blk, gui_blk);
46 }
47
48 /*---------------------------------------------------------------------------*/
49
50 static int start_action(int i)
51 {
52     audio_play(AUD_MENU, 1.0f);
53
54     if (i == START_BACK)
55         return goto_state(&st_set);
56
57     if (level_opened(i))
58     {
59         if (level_play(USER_REPLAY_FILE, i))
60             return goto_state(&st_level);
61         else
62         {
63             set_free();
64             return goto_state(&st_title);
65         }
66     }
67     return 1;
68 }
69
70 static int start_enter(void)
71 {
72     int w = config_get_d(CONFIG_WIDTH);
73     int h = config_get_d(CONFIG_HEIGHT);
74
75     int id, jd, kd, ld;
76
77     if ((id = gui_vstack(0)))
78     {
79         if ((jd = gui_harray(id)))
80         {
81             gui_label(jd, "Level", GUI_SML, GUI_ALL, gui_yel, gui_red);
82             gui_filler(jd);
83             gui_filler(jd);
84             gui_start(jd, "Back",  GUI_SML, START_BACK, 0);
85         }
86
87         if ((jd = gui_harray(id)))
88         {
89             shot_id = gui_image(jd, "shot-rlk/easy.jpg", 7 * w / 16, 7 * h / 16);
90
91             if ((kd = gui_varray(jd)))
92             {
93                 if ((ld = gui_harray(kd)))
94                 {
95                     gui_level(ld, "05",  5);
96                     gui_level(ld, "04",  4);
97                     gui_level(ld, "03",  3);
98                     gui_level(ld, "02",  2);
99                     gui_level(ld, "01",  1);
100                 }
101                 if ((ld = gui_harray(kd)))
102                 {
103                     gui_level(ld, "10", 10);
104                     gui_level(ld, "09",  9);
105                     gui_level(ld, "08",  8);
106                     gui_level(ld, "07",  7);
107                     gui_level(ld, "06",  6);
108                 }
109                 if ((ld = gui_harray(kd)))
110                 {
111                     gui_level(ld, "15", 15);
112                     gui_level(ld, "14", 14);
113                     gui_level(ld, "13", 13);
114                     gui_level(ld, "12", 12);
115                     gui_level(ld, "11", 11);
116                 }
117                 if ((ld = gui_harray(kd)))
118                 {
119                     gui_level(ld, "20", 20);
120                     gui_level(ld, "19", 19);
121                     gui_level(ld, "18", 18);
122                     gui_level(ld, "17", 17);
123                     gui_level(ld, "16", 16);
124                 }
125                 if ((ld = gui_harray(kd)))
126                 {
127                     gui_level(ld, "25", 25);
128                     gui_level(ld, "24", 24);
129                     gui_level(ld, "23", 23);
130                     gui_level(ld, "22", 22);
131                     gui_level(ld, "21", 21);
132                 }
133             }
134         }
135         gui_space(id);
136
137         if ((jd = gui_harray(id)))
138         {
139             gui_most_coins(jd, 3, 4);
140             gui_best_times(jd, 3, 4);
141         }
142
143         gui_layout(id, 0, 0);
144         set_most_coins(0, 3);
145         set_best_times(0, 3);
146     }
147
148     return id;
149 }
150
151 static void start_leave(int id)
152 {
153     gui_delete(id);
154 }
155
156 static void start_paint(int id, float st)
157 {
158     game_draw(0, st);
159     config_pop_matrix();
160     gui_paint(id);
161 }
162
163 static void start_timer(int id, float dt)
164 {
165     gui_timer(id, dt);
166     audio_timer(dt);
167 }
168
169 static void start_point(int id, int x, int y, int dx, int dy)
170 {
171     int jd;
172
173     if ((jd = gui_point(id, x, y)))
174     {
175         int i = gui_token(jd);
176
177         gui_set_image(shot_id, level_shot(i));
178
179         set_most_coins(i, 3);
180         set_best_times(i, 3);
181
182         gui_pulse(jd, 1.2f);
183     }
184 }
185
186 static void start_stick(int id, int a, int v)
187 {
188     int jd;
189
190     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
191     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
192
193     if ((jd = gui_stick(id, x, y)))
194     {
195         int i = gui_token(jd);
196
197         gui_set_image(shot_id, level_shot(i));
198
199         set_most_coins(i, 3);
200         set_best_times(i, 3);
201
202         gui_pulse(jd, 1.2f);
203     }
204 }
205
206 static int start_click(int b, int d)
207 {
208     if (d && b < 0)
209         return start_action(gui_token(gui_click()));
210     return 1;
211 }
212
213 static int start_keybd(int c, int d)
214 {
215     if (d && c == SDLK_ESCAPE)
216         return goto_state(&st_title);
217
218     if (d && c == SDLK_F12)
219     {
220         int n = curr_count();
221         int i;
222
223         /* Iterate over all levels, taking a screenshot of each. */
224
225         for (i = 1; i < n; i++)
226             if (level_exists(i))
227                 level_snap(i);
228     }
229
230     return 1;
231 }
232
233 static int start_buttn(int b, int d)
234 {
235     if (d)
236     {
237         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
238             return start_action(gui_token(gui_click()));
239         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
240             return goto_state(&st_title);
241         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
242             return goto_state(&st_title);
243     }
244     return 1;
245 }
246
247 /*---------------------------------------------------------------------------*/
248
249 struct state st_start = {
250     start_enter,
251     start_leave,
252     start_paint,
253     start_timer,
254     start_point,
255     start_stick,
256     start_click,
257     start_keybd,
258     start_buttn,
259     1, 0
260 };