Merge 'progress' branch.
[neverball] / ball / st_pause.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 "config.h"
17 #include "game.h"
18 #include "progress.h"
19 #include "level.h"
20 #include "audio.h"
21 #include "hud.h"
22
23 #include "st_play.h"
24 #include "st_over.h"
25 #include "st_shared.h"
26 #include "st_pause.h"
27
28 #define PAUSE_CONTINUE 1
29 #define PAUSE_RESTART  2
30 #define PAUSE_EXIT     3
31
32 /*---------------------------------------------------------------------------*/
33
34 static struct state *st_continue;
35 static int paused;
36
37 int goto_pause(void)
38 {
39     st_continue = curr_state();
40     paused = 1;
41     return goto_state(&st_pause);
42 }
43
44 int is_paused(void)
45 {
46     return paused;
47 }
48
49 void clear_pause(void)
50 {
51     paused = 0;
52 }
53
54 /*---------------------------------------------------------------------------*/
55
56 static int pause_action(int i)
57 {
58     audio_play(AUD_MENU, 1.0f);
59
60     switch(i)
61     {
62     case PAUSE_CONTINUE:
63         SDL_PauseAudio(0);
64         config_set_grab(0);
65         return goto_state(st_continue);
66
67     case PAUSE_RESTART:
68         if (progress_same())
69         {
70             clear_pause();
71             SDL_PauseAudio(0);
72             config_set_grab(1);
73             return goto_state(&st_play_ready);
74         }
75         break;
76
77     case PAUSE_EXIT:
78         progress_exit(GAME_NONE);
79         clear_pause();
80         SDL_PauseAudio(0);
81         audio_music_stop();
82         return goto_state(&st_over);
83     }
84
85     return 1;
86 }
87
88 /*---------------------------------------------------------------------------*/
89
90 static int pause_enter(void)
91 {
92     int id, jd, title_id;
93
94     config_clr_grab();
95     SDL_PauseAudio(1);
96
97     /* Build the pause GUI. */
98
99     if ((id = gui_vstack(0)))
100     {
101         title_id = gui_label(id, _("Paused"), GUI_LRG, GUI_ALL, 0, 0);
102
103         gui_space(id);
104
105         if ((jd = gui_harray(id)))
106         {
107             gui_state(jd, _("Quit"), GUI_SML, PAUSE_EXIT, 0);
108
109             if (progress_same_avail())
110                 gui_state(jd, _("Restart"), GUI_SML, PAUSE_RESTART, 0);
111
112             gui_start(jd, _("Continue"), GUI_SML, PAUSE_CONTINUE, 1);
113         }
114
115         gui_pulse(title_id, 1.2f);
116         gui_layout(id, 0, 0);
117     }
118
119     hud_update(0);
120
121     return id;
122 }
123
124 static void pause_paint(int id, float t)
125 {
126     shared_paint(id, t);
127     hud_paint();
128 }
129
130 static void pause_timer(int id, float dt)
131 {
132     gui_timer(id, dt);
133     hud_timer (dt);
134 }
135
136 static int pause_keybd(int c, int d)
137 {
138     if (d)
139     {
140         if (config_tst_d(CONFIG_KEY_PAUSE, c))
141             return pause_action(PAUSE_CONTINUE);
142
143         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
144             return pause_action(PAUSE_RESTART);
145     }
146     return 1;
147 }
148
149 static int pause_buttn(int b, int d)
150 {
151     if (d)
152     {
153         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
154             return pause_action(gui_token(gui_click()));
155
156         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
157         {
158             if (SDL_GetModState() & KMOD_SHIFT)
159                 return pause_action(PAUSE_EXIT);
160             else
161                 return pause_action(PAUSE_CONTINUE);
162         }
163     }
164     return 1;
165 }
166
167 /*---------------------------------------------------------------------------*/
168
169 struct state st_pause = {
170     pause_enter,
171     shared_leave,
172     pause_paint,
173     pause_timer,
174     shared_point,
175     shared_stick,
176     shared_angle,
177     shared_click,
178     pause_keybd,
179     pause_buttn,
180     1, 0
181 };