Formatting stuff. Convert tabs to spaces, shorten lines to 80, etc. First
[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 #define TITLE_PLAY 1
37 #define TITLE_HELP 2
38 #define TITLE_DEMO 3
39 #define TITLE_CONF 4
40 #define TITLE_EXIT 5
41
42 static int title_action(int i)
43 {
44     char player[MAXNAM];
45     audio_play(AUD_MENU, 1.0f);
46
47     switch (i)
48     {
49     case TITLE_PLAY: 
50         config_get_s(CONFIG_PLAYER, player, MAXNAM);
51         if (player[0] == '\0')
52             return goto_name(&st_set, &st_title);
53         else
54             return goto_state(&st_set);
55     case TITLE_HELP: return goto_state(&st_help);
56     case TITLE_DEMO: return goto_state(&st_demo);
57     case TITLE_CONF: return goto_state(&st_conf);
58     case TITLE_EXIT: return 0;
59     }
60     return 1;
61 }
62
63 static struct level title_level;
64
65 static int title_enter(void)
66 {
67     int id, jd, kd;
68
69     /* Build the title GUI. */
70
71     if ((id = gui_vstack(0)))
72     {
73         gui_label(id, "Neverball", GUI_LRG, GUI_ALL, 0, 0);
74         gui_space(id);
75
76         if ((jd = gui_harray(id)))
77         {
78             gui_filler(jd);
79
80             if ((kd = gui_varray(jd)))
81             {
82                 gui_start(kd, sgettext("menu^Play"),    GUI_MED, TITLE_PLAY, 1);
83                 gui_state(kd, sgettext("menu^Replay"),  GUI_MED, TITLE_DEMO, 0);
84                 gui_state(kd, sgettext("menu^Help"),    GUI_MED, TITLE_HELP, 0);
85                 gui_state(kd, sgettext("menu^Options"), GUI_MED, TITLE_CONF, 0);
86                 gui_state(kd, sgettext("menu^Exit"),    GUI_MED, TITLE_EXIT, 0);
87             }
88
89             gui_filler(jd);
90         }
91         gui_layout(id, 0, 0);
92     }
93
94     /* Start the title screen music. */
95
96     audio_music_fade_to(0.5f, "bgm/title.ogg");
97
98     /* Initialize the first level of the first set for display. */
99     level_load(config_data("map-rlk/title.sol"), &title_level);
100     game_init(&title_level, 0, 0);
101
102     real_time = 0.0f;
103     demo_time = 0.0f;
104     mode = 0;
105
106     return id;
107 }
108
109 static void title_leave(int id)
110 {
111     demo_replay_stop(0);
112     gui_delete(id);
113 }
114
115 static void title_timer(int id, float dt)
116 {
117     static const char *demo = NULL;
118     float t;
119
120     real_time += dt;
121
122     switch (mode)
123     {
124     case 0: /* Mode 0: Pan across title level. */
125
126         if (real_time <= 20.0f)
127             game_set_fly(fcosf(V_PI * real_time / 20.0f));
128         else
129         {
130             game_fade(+1.0f);
131             real_time = 0.0f;
132             mode = 1;
133         }
134         break;
135
136     case 1: /* Mode 1: Fade out.  Load demo level. */
137
138         if (real_time > 1.0f)
139         {
140             if ((demo = demo_pick()))
141             {
142                 demo_replay_init(demo, NULL);
143                 demo_time = 0.0f;
144                 real_time = 0.0f;
145                 mode = 2;
146             }
147             else
148             {
149                 game_fade(-1.0f);
150                 real_time = 0.0f;
151                 mode = 0;
152             }
153         }
154         break;
155
156     case 2: /* Mode 2: Run demo. */
157
158         while (demo_time < real_time)
159             if (demo_replay_step(&t))
160                 demo_time += t;
161             else
162             { 
163                 demo_replay_stop(0);
164                 game_fade(+1.0f);
165                 real_time = 0.0f;
166                 mode = 3;
167             }
168         break;
169
170     case 3: /* Mode 3: Fade out.  Load title level. */
171
172         if (real_time > 1.0f)
173         {
174             game_init(&title_level, 0, 0);
175             real_time = 0.0f;
176             mode = 0;
177         }
178         break;
179     }
180
181     gui_timer(id, dt);
182     audio_timer(dt);
183     game_step_fade(dt);
184 }
185
186 static int title_keybd(int c, int d)
187 {
188     if (d && c == SDLK_c && ALLOW_CHEAT)
189         config_tgl_d(CONFIG_CHEAT);
190     return 1; 
191 }
192         
193 static int title_buttn(int b, int d)
194 {
195     if (d)
196     {
197         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
198             return title_action(gui_token(gui_click()));
199         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
200             return 0;
201     }
202     return 1;
203 }
204
205 /*---------------------------------------------------------------------------*/
206
207 struct state st_title = {
208     title_enter,
209     title_leave,
210     shared_paint,
211     title_timer,
212     shared_point,
213     shared_stick,
214     shared_click,
215     title_keybd,
216     title_buttn,
217     1, 0
218 };
219