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