Import from neverball-1.4.0.tar.gz
[neverball-archive.git] / ball / st_save.c
blob61757a784152a40b348cfa39e79c7a7150dd3efd
1 /*
2 * Copyright (C) 2003 Robert Kooima
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.
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.
15 #include <string.h>
17 #include "gui.h"
18 #include "game.h"
19 #include "util.h"
20 #include "demo.h"
21 #include "level.h"
22 #include "audio.h"
23 #include "config.h"
25 #include "st_save.h"
26 #include "st_over.h"
27 #include "st_done.h"
28 #include "st_level.h"
29 #include "st_title.h"
31 /*---------------------------------------------------------------------------*/
33 #define SAVE_SAVE 2
34 #define SAVE_CANCEL 3
36 static int file_id;
37 static char filename[MAXNAM];
39 static int save_action(int i)
41 size_t l = strlen(filename);
43 audio_play(AUD_MENU, 1.0f);
45 switch (i)
47 case SAVE_SAVE:
48 if (demo_exists(filename))
49 return goto_state(&st_clobber);
50 else
52 if (level_exit(filename, 1))
53 return goto_state(&st_level);
54 else
55 return goto_state(&st_done);
58 case SAVE_CANCEL:
59 if (level_exit(NULL, 1))
60 return goto_state(&st_level);
61 else
62 return goto_state(&st_done);
64 case GUI_CL:
65 gui_keyboard_lock();
66 break;
68 case GUI_BS:
69 if (l > 0)
71 filename[l - 1] = 0;
72 gui_set_label(file_id, filename);
74 break;
76 default:
77 if (l < MAXNAM - 1)
79 filename[l + 0] = gui_keyboard_char((char) i);
80 filename[l + 1] = 0;
81 gui_set_label(file_id, filename);
84 return 1;
87 static int save_enter(void)
89 int id, jd;
91 demo_unique(filename);
93 if ((id = gui_vstack(0)))
95 gui_space(id);
96 if ((jd = gui_hstack(id)))
98 gui_filler(jd);
99 gui_count(jd, curr_level(), GUI_LRG, GUI_NE | GUI_SE);
100 gui_label(jd, "Level ", GUI_LRG, GUI_NW | GUI_SW, 0, 0);
101 gui_filler(jd);
103 gui_space(id);
105 gui_space(id);
106 file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
107 gui_space(id);
109 if ((jd = gui_harray(id)))
111 gui_start(jd, "Cancel", GUI_SML, SAVE_CANCEL, 0);
112 gui_start(jd, "Save", GUI_SML, SAVE_SAVE, 0);
114 gui_keyboard(id);
116 gui_layout(id, 0, 0);
119 return id;
122 static void save_leave(int id)
124 gui_delete(id);
127 static void save_paint(int id, float st)
129 game_draw(0, st);
130 gui_paint(id);
133 static void save_timer(int id, float dt)
135 gui_timer(id, dt);
136 audio_timer(dt);
139 static void save_point(int id, int x, int y, int dx, int dy)
141 gui_pulse(gui_point(id, x, y), 1.2f);
144 static void save_stick(int id, int a, int v)
146 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
147 gui_pulse(gui_stick(id, v, 0), 1.2f);
148 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
149 gui_pulse(gui_stick(id, 0, v), 1.2f);
152 static int save_click(int b, int d)
154 if (b <= 0 && d == 1)
155 return save_action(gui_token(gui_click()));
156 return 1;
159 static int save_keybd(int c, int d)
161 if (d && c == SDLK_ESCAPE)
162 goto_state(&st_over);
163 return 1;
166 static int save_buttn(int b, int d)
168 if (d)
170 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
171 return save_click(0, 1);
172 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
173 return goto_state(&st_over);
175 return 1;
178 /*---------------------------------------------------------------------------*/
180 static int clobber_action(int i)
182 audio_play(AUD_MENU, 1.0f);
184 if (i == SAVE_SAVE)
186 if (level_exit(filename, 1))
187 return goto_state(&st_level);
188 else
189 return goto_state(&st_title);
191 return goto_state(&st_save);
194 static int clobber_enter(void)
196 int id, jd, kd;
198 if ((id = gui_vstack(0)))
200 kd = gui_label(id, "Overwrite?", GUI_MED, GUI_ALL, gui_red, gui_red);
202 gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
204 if ((jd = gui_harray(id)))
206 gui_state(jd, "Yes", GUI_SML, SAVE_SAVE, 0);
207 gui_start(jd, "No", GUI_SML, SAVE_CANCEL, 1);
210 gui_pulse(kd, 1.2f);
211 gui_layout(id, 0, 0);
214 return id;
217 static void clobber_leave(int id)
219 gui_delete(id);
222 static void clobber_paint(int id, float st)
224 game_draw(0, st);
225 gui_paint(id);
228 static void clobber_timer(int id, float dt)
230 gui_timer(id, dt);
231 audio_timer(dt);
234 static int clobber_keybd(int c, int d)
236 return (d && c == SDLK_ESCAPE) ? clobber_action(SAVE_CANCEL) : 1;
239 static void clobber_point(int id, int x, int y, int dx, int dy)
241 gui_pulse(gui_point(id, x, y), 1.2f);
244 static void clobber_stick(int id, int a, int v)
246 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
247 gui_pulse(gui_stick(id, v, 0), 1.2f);
248 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
249 gui_pulse(gui_stick(id, 0, v), 1.2f);
252 static int clobber_click(int b, int d)
254 if (d && b < 0)
255 return clobber_action(gui_token(gui_click()));
256 return 1;
259 static int clobber_buttn(int b, int d)
261 if (d)
263 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
264 return clobber_action(gui_token(gui_click()));
265 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
266 return clobber_action(SAVE_CANCEL);
268 return 1;
271 /*---------------------------------------------------------------------------*/
273 struct state st_save = {
274 save_enter,
275 save_leave,
276 save_paint,
277 save_timer,
278 save_point,
279 save_stick,
280 save_click,
281 save_keybd,
282 save_buttn,
283 1, 0
286 struct state st_clobber = {
287 clobber_enter,
288 clobber_leave,
289 clobber_paint,
290 clobber_timer,
291 clobber_point,
292 clobber_stick,
293 clobber_click,
294 clobber_keybd,
295 clobber_buttn,
296 1, 0