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