Removed remaining SDL_mixer includes
[neverball] / ball / st_title.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 "vec3.h"
17 #include "demo.h"
18 #include "game.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "st_shared.h"
22
23 #include "st_title.h"
24 #include "st_help.h"
25 #include "st_demo.h"
26 #include "st_conf.h"
27 #include "st_set.h"
28 #include "st_name.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 static float real_time = 0.0f;
33 static float demo_time = 0.0f;
34 static int   mode      = 0;
35
36 static int play_id = 0;
37
38 #define TITLE_PLAY 1
39 #define TITLE_HELP 2
40 #define TITLE_DEMO 3
41 #define TITLE_CONF 4
42 #define TITLE_EXIT 5
43
44 static int title_action(int i)
45 {
46     static const char keyphrase[] = "CHEAT";
47     static char queue[sizeof (keyphrase)] = "";
48
49     size_t queue_len = strlen(queue);
50
51     char player[MAXNAM];
52
53     audio_play(AUD_MENU, 1.0f);
54
55     switch (i)
56     {
57     case TITLE_PLAY:
58         config_get_s(CONFIG_PLAYER, player, MAXNAM);
59
60         if (strlen(player) == 0)
61             return goto_name(&st_set, &st_title);
62         else
63             return goto_state(&st_set);
64
65         break;
66
67     case TITLE_HELP: return goto_state(&st_help); break;
68     case TITLE_DEMO: return goto_state(&st_demo); break;
69     case TITLE_CONF: return goto_state(&st_conf); break;
70     case TITLE_EXIT: return 0;                    break;
71
72     default:
73
74         /* Let the queue fill up. */
75
76         if (queue_len < sizeof (queue) - 1)
77         {
78             queue[queue_len]     = (char) i;
79             queue[queue_len + 1] = '\0';
80         }
81
82         /* Advance the queue before adding the new element. */
83
84         else
85         {
86             int k;
87
88             for (k = 1; k < queue_len; k++)
89                 queue[k - 1] = queue[k];
90
91             queue[queue_len - 1] = (char) i;
92         }
93
94         if (strcmp(queue, keyphrase) == 0)
95         {
96             config_set_cheat();
97             gui_set_label(play_id, sgettext("menu^Cheat"));
98             gui_pulse(play_id, 1.2f);
99         }
100         else if (config_cheat())
101         {
102             config_clr_cheat();
103             gui_set_label(play_id, sgettext("menu^Play"));
104             gui_pulse(play_id, 1.2f);
105         }
106
107         break;
108     }
109     return 1;
110 }
111
112 static struct level title_level;
113
114 static int title_enter(void)
115 {
116     int id, jd, kd;
117
118     /* Build the title GUI. */
119
120     if ((id = gui_vstack(0)))
121     {
122         gui_label(id, "Neverball", GUI_LRG, GUI_ALL, 0, 0);
123
124         gui_space(id);
125
126         if ((jd = gui_harray(id)))
127         {
128             gui_filler(jd);
129
130             if ((kd = gui_varray(jd)))
131             {
132                 if (config_cheat())
133                     play_id = gui_start(kd, sgettext("menu^Cheat"),
134                                         GUI_MED, TITLE_PLAY, 1);
135                 else
136                     play_id = gui_start(kd, sgettext("menu^Play"),
137                                         GUI_MED, TITLE_PLAY, 1);
138
139                 gui_state(kd, sgettext("menu^Replay"),  GUI_MED, TITLE_DEMO, 0);
140                 gui_state(kd, sgettext("menu^Help"),    GUI_MED, TITLE_HELP, 0);
141                 gui_state(kd, sgettext("menu^Options"), GUI_MED, TITLE_CONF, 0);
142                 gui_state(kd, sgettext("menu^Exit"),    GUI_MED, TITLE_EXIT, 0);
143             }
144
145             gui_filler(jd);
146         }
147         gui_layout(id, 0, 0);
148     }
149
150     /* Start the title screen music. */
151
152     audio_music_fade_to(0.5f, "bgm/title.ogg");
153
154     /* Initialize the title level for display. */
155     level_load("map-medium/title.sol", &title_level);
156     game_init(&title_level, 0, 0);
157
158     real_time = 0.0f;
159     demo_time = 0.0f;
160     mode = 0;
161
162     SDL_EnableUNICODE(1);
163
164     return id;
165 }
166
167 static void title_leave(int id)
168 {
169     SDL_EnableUNICODE(0);
170     demo_replay_stop(0);
171     gui_delete(id);
172 }
173
174 static void title_timer(int id, float dt)
175 {
176     static const char *demo = NULL;
177     float t;
178
179     real_time += dt;
180
181     switch (mode)
182     {
183     case 0: /* Mode 0: Pan across title level. */
184
185         if (real_time <= 20.0f)
186             game_set_fly(fcosf(V_PI * real_time / 20.0f));
187         else
188         {
189             game_fade(+1.0f);
190             real_time = 0.0f;
191             mode = 1;
192         }
193         break;
194
195     case 1: /* Mode 1: Fade out.  Load demo level. */
196
197         if (real_time > 1.0f)
198         {
199             if ((demo = demo_pick()))
200             {
201                 demo_replay_init(demo, NULL);
202                 demo_time = 0.0f;
203                 real_time = 0.0f;
204                 mode = 2;
205             }
206             else
207             {
208                 game_fade(-1.0f);
209                 real_time = 0.0f;
210                 mode = 0;
211             }
212         }
213         break;
214
215     case 2: /* Mode 2: Run demo. */
216
217         while (demo_time < real_time)
218             if (demo_replay_step(&t))
219                 demo_time += t;
220             else
221             {
222                 demo_replay_stop(0);
223                 game_fade(+1.0f);
224                 real_time = 0.0f;
225                 mode = 3;
226             }
227         break;
228
229     case 3: /* Mode 3: Fade out.  Load title level. */
230
231         if (real_time > 1.0f)
232         {
233             game_init(&title_level, 0, 0);
234             real_time = 0.0f;
235             mode = 0;
236         }
237         break;
238     }
239
240     gui_timer(id, dt);
241     game_step_fade(dt);
242 }
243
244 static int title_keybd(int c, int d)
245 {
246     if (d && (c & 0xFF80) == 0 && c > ' ')
247         return title_action(c);
248     return 1;
249 }
250
251 static int title_buttn(int b, int d)
252 {
253     if (d)
254     {
255         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
256             return title_action(gui_token(gui_click()));
257         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
258             return 0;
259     }
260     return 1;
261 }
262
263 /*---------------------------------------------------------------------------*/
264
265 struct state st_title = {
266     title_enter,
267     title_leave,
268     shared_paint,
269     title_timer,
270     shared_point,
271     shared_stick,
272     shared_click,
273     title_keybd,
274     title_buttn,
275     1, 0
276 };
277