share/video: move some non-config code from share/config here
[neverball] / ball / st_fall_out.c
1 /*
2  * Copyright (C) 2007 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 "util.h"
17 #include "progress.h"
18 #include "audio.h"
19 #include "config.h"
20 #include "video.h"
21 #include "demo.h"
22
23 #include "game_common.h"
24 #include "game_server.h"
25 #include "game_client.h"
26
27 #include "st_fall_out.h"
28 #include "st_save.h"
29 #include "st_over.h"
30 #include "st_start.h"
31 #include "st_level.h"
32 #include "st_shared.h"
33 #include "st_play.h"
34
35 /*---------------------------------------------------------------------------*/
36
37 #define FALL_OUT_NEXT 1
38 #define FALL_OUT_SAME 2
39 #define FALL_OUT_SAVE 3
40 #define FALL_OUT_BACK 4
41 #define FALL_OUT_OVER 5
42
43 static int resume;
44
45 static int fall_out_action(int i)
46 {
47     audio_play(AUD_MENU, 1.0f);
48
49     switch (i)
50     {
51     case FALL_OUT_BACK:
52         /* Fall through. */
53
54     case FALL_OUT_OVER:
55         progress_stop();
56         return goto_state(&st_over);
57
58     case FALL_OUT_SAVE:
59         resume = 1;
60
61         progress_stop();
62         return goto_save(&st_fall_out, &st_fall_out);
63
64     case FALL_OUT_NEXT:
65         if (progress_next())
66             return goto_state(&st_level);
67         break;
68
69     case FALL_OUT_SAME:
70         if (progress_same())
71             return goto_state(&st_level);
72         break;
73     }
74
75     return 1;
76 }
77
78 static int fall_out_enter(void)
79 {
80     int id, jd, kd;
81
82     /* Reset hack. */
83     resume = 0;
84
85     if ((id = gui_vstack(0)))
86     {
87         kd = gui_label(id, _("Fall-out!"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
88
89         gui_space(id);
90
91         if ((jd = gui_harray(id)))
92         {
93             if (progress_dead())
94                 gui_start(jd, _("Exit"), GUI_SML, FALL_OUT_OVER, 0);
95
96             if (progress_next_avail())
97                 gui_start(jd, _("Next Level"),  GUI_SML, FALL_OUT_NEXT, 0);
98
99             if (progress_same_avail())
100                 gui_start(jd, _("Retry Level"), GUI_SML, FALL_OUT_SAME, 0);
101
102             if (demo_saved())
103                 gui_state(jd, _("Save Replay"), GUI_SML, FALL_OUT_SAVE, 0);
104         }
105
106         gui_space(id);
107
108         gui_pulse(kd, 1.2f);
109         gui_layout(id, 0, 0);
110     }
111
112     audio_music_fade_out(2.0f);
113     /* audio_play(AUD_FALL, 1.0f); */
114
115     config_clr_grab();
116
117     return id;
118 }
119
120 static void fall_out_timer(int id, float dt)
121 {
122     if (time_state() < 2.f)
123     {
124         game_server_step(dt);
125         game_client_step(demo_file());
126     }
127
128     gui_timer(id, dt);
129 }
130
131 static int fall_out_keybd(int c, int d)
132 {
133     if (d)
134     {
135         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
136         {
137             if (progress_same())
138                 goto_state(&st_play_ready);
139         }
140     }
141     return 1;
142 }
143
144 static int fall_out_buttn(int b, int d)
145 {
146     if (d)
147     {
148         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
149             return fall_out_action(gui_token(gui_click()));
150         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
151             return fall_out_action(FALL_OUT_BACK);
152     }
153     return 1;
154 }
155
156 static void fall_out_leave(int id)
157 {
158     /* HACK:  don't run animation if only "visiting" a state. */
159     st_fall_out.timer = resume ? shared_timer : fall_out_timer;
160
161     gui_delete(id);
162 }
163
164 /*---------------------------------------------------------------------------*/
165
166 struct state st_fall_out = {
167     fall_out_enter,
168     fall_out_leave,
169     shared_paint,
170     fall_out_timer,
171     shared_point,
172     shared_stick,
173     NULL,
174     shared_click,
175     fall_out_keybd,
176     fall_out_buttn,
177     1, 0
178 };
179