Renamed functions: grow_set -> grow_init, grow_ball -> grow_step.
[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
17 #include "gui.h"
18 #include "config.h"
19 #include "game.h"
20 #include "levels.h"
21 #include "level.h"
22 #include "audio.h"
23 #include "hud.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         Mix_ResumeMusic();
66         config_set_grab(0);
67         return goto_state(st_continue);
68
69     case PAUSE_RESTART:
70         level_same();
71         clear_pause();
72         Mix_ResumeMusic();
73         config_set_grab(1);
74         return goto_state(&st_play_ready);
75
76     case PAUSE_EXIT:
77         level_stat(GAME_NONE, curr_clock(), curr_coins());
78         level_stop();
79         clear_pause();
80         return goto_state(&st_over);
81     }
82
83     return 1;
84 }
85
86 /*---------------------------------------------------------------------------*/
87
88 static int pause_enter(void)
89 {
90     int id, jd, title_id;
91
92     config_clr_grab();
93
94     Mix_PauseMusic();
95
96     /* Build the pause GUI. */
97
98     if ((id = gui_vstack(0)))
99     {
100         title_id = gui_label(id, _("Paused"), GUI_LRG, GUI_ALL, 0, 0);
101
102         gui_space(id);
103
104         if ((jd = gui_harray(id)))
105         {
106             gui_state(jd, _("Quit"), GUI_SML, PAUSE_EXIT, 0);
107
108             if (curr_lg()->mode != MODE_CHALLENGE)
109                 gui_state(jd, _("Restart"), GUI_SML, PAUSE_RESTART, 0);
110             else
111             {
112                 int ld = gui_state(jd, _("Restart"), GUI_SML, 0, 0);
113                 gui_set_color(ld, gui_gry, gui_gry);
114             }
115
116             gui_start(jd, _("Continue"), GUI_SML, PAUSE_CONTINUE, 1);
117         }
118
119         gui_pulse(title_id, 1.2f);
120         gui_layout(id, 0, 0);
121     }
122
123     hud_update(0);
124
125     return id;
126 }
127
128 static void pause_paint(int id, float st)
129 {
130     shared_paint(id, st);
131     hud_paint();
132 }
133
134 static void pause_timer(int id, float dt)
135 {
136     gui_timer(id, dt);
137     hud_timer (dt);
138 }
139
140 static int pause_keybd(int c, int d)
141 {
142     if (d)
143     {
144         if (config_tst_d(CONFIG_KEY_PAUSE, c))
145             return pause_action(PAUSE_CONTINUE);
146
147         if (config_tst_d(CONFIG_KEY_RESTART, c)
148             && curr_lg()->mode != MODE_CHALLENGE)
149             return pause_action(PAUSE_RESTART);
150     }
151     return 1;
152 }
153
154 static int pause_buttn(int b, int d)
155 {
156     if (d)
157     {
158         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
159             return pause_action(gui_token(gui_click()));
160
161         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
162         {
163             if (SDL_GetModState() & KMOD_SHIFT)
164                 return pause_action(PAUSE_EXIT);
165             else
166                 return pause_action(PAUSE_CONTINUE);
167         }
168     }
169     return 1;
170 }
171
172 /*---------------------------------------------------------------------------*/
173
174 struct state st_pause = {
175     pause_enter,
176     shared_leave,
177     pause_paint,
178     pause_timer,
179     shared_point,
180     shared_stick,
181     shared_click,
182     pause_keybd,
183     pause_buttn,
184     1, 0
185 };