-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsave_game.c
294 lines (284 loc) · 6.49 KB
/
save_game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
save_game: the "Save Game" screen
*/
#include <stdio.h>
#include "save_game.h"
#include "ui.h"
#include "globals.h"
#include "saving.h"
#define NAMEBUFLEN 256
atg_element *save_game_box;
char *SA_btext;
atg_element *SA_file, *SA_text, *SA_full, *SA_exit, *SA_save;
int save_game_create(void)
{
save_game_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, (atg_colour){31, 31, 47, ATG_ALPHA_OPAQUE});
if(!save_game_box)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
SA_file=atg_create_element_filepicker("Save Game", NULL, (atg_colour){239, 239, 255, ATG_ALPHA_OPAQUE}, (atg_colour){31, 31, 47, ATG_ALPHA_OPAQUE});
if(!SA_file)
{
fprintf(stderr, "atg_create_element_filepicker failed\n");
return(1);
}
SA_file->h=mainsizey-30;
SA_file->w=mainsizex;
if(atg_ebox_pack(save_game_box, SA_file))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *SA_hbox=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!SA_hbox)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
else if(atg_ebox_pack(save_game_box, SA_hbox))
{
perror("atg_ebox_pack");
return(1);
}
else
{
SA_save=atg_create_element_button("Save", (atg_colour){239, 239, 239, ATG_ALPHA_OPAQUE}, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!SA_save)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
SA_save->w=34;
if(atg_ebox_pack(SA_hbox, SA_save))
{
perror("atg_ebox_pack");
return(1);
}
SA_text=atg_create_element_button_empty((atg_colour){239, 255, 239, ATG_ALPHA_OPAQUE}, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!SA_text)
{
fprintf(stderr, "atg_create_element_button_empty failed\n");
return(1);
}
SA_text->h=24;
SA_text->w=mainsizex-64;
if(atg_ebox_pack(SA_hbox, SA_text))
{
perror("atg_ebox_pack");
return(1);
}
if(!(SA_btext=malloc(NAMEBUFLEN)))
{
perror("malloc");
return(1);
}
atg_element *label=atg_create_element_label_nocopy(SA_btext, 12, (atg_colour){239, 255, 239, ATG_ALPHA_OPAQUE});
if(!label)
{
fprintf(stderr, "atg_create_element_label_nocopy failed\n");
return(1);
}
if(atg_ebox_pack(SA_text, label))
{
perror("atg_ebox_pack");
return(1);
}
SA_btext[0]=0;
SA_full=atg_create_element_image(fullbtn);
if(!SA_full)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
SA_full->w=16;
SA_full->clickable=true;
if(atg_ebox_pack(SA_hbox, SA_full))
{
perror("atg_ebox_pack");
return(1);
}
SA_exit=atg_create_element_image(exitbtn);
if(!SA_exit)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
SA_exit->clickable=true;
if(atg_ebox_pack(SA_hbox, SA_exit))
{
perror("atg_ebox_pack");
return(1);
}
}
return(0);
}
screen_id save_game_screen(atg_canvas *canvas, game *state)
{
atg_event e;
atg_filepicker *f=SA_file->elemdata;
if(!f)
{
fprintf(stderr, "Error: SA_file->elemdata==NULL\n");
return(SCRN_CONTROL);
}
while(1)
{
SA_file->w=mainsizex;
SA_file->h=mainsizey-30;
SA_text->w=mainsizex-64;
atg_flip(canvas);
while(atg_poll_event(&e, canvas))
{
switch(e.type)
{
case ATG_EV_RAW:;
SDL_Event s=e.event.raw;
switch(s.type)
{
case SDL_QUIT:
return(SCRN_CONTROL);
case SDL_VIDEORESIZE:
mainsizex=canvas->surface->w;
mainsizey=canvas->surface->h;
break;
case SDL_KEYDOWN:;
switch(s.key.keysym.sym)
{
case SDLK_BACKSPACE:
if(f->value)
{
size_t l=strlen(f->value);
if(l)
f->value[l-1]=0;
if(SA_btext)
snprintf(SA_btext, NAMEBUFLEN, "%s", f->value);
}
break;
case SDLK_RETURN:
goto do_save;
default:
if((s.key.keysym.unicode&0xFF80)==0) // TODO handle UTF-8 in filenames?
{
char what=s.key.keysym.unicode&0x7F;
if(what)
{
if(f->value)
{
size_t l=strlen(f->value);
char *n=realloc(f->value, l+2);
if(n)
{
(f->value=n)[l]=what;
n[l+1]=0;
if(SA_btext)
snprintf(SA_btext, NAMEBUFLEN, "%s", f->value);
}
else
perror("realloc");
}
else
{
f->value=malloc(2);
if(f->value)
{
f->value[0]=what;
f->value[1]=0;
if(SA_btext)
snprintf(SA_btext, NAMEBUFLEN, "%s", f->value);
}
else
perror("malloc");
}
}
}
break;
}
break;
}
break;
case ATG_EV_CLICK:;
atg_ev_click c=e.event.click;
if(c.e==SA_full)
{
fullscreen=!fullscreen;
atg_setopts_canvas(canvas, fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE);
}
else if (c.e==SA_exit)
return(SCRN_CONTROL);
else if(c.e)
{
fprintf(stderr, "Clicked on unknown clickable!\n");
}
break;
case ATG_EV_TRIGGER:;
atg_ev_trigger trigger=e.event.trigger;
if(trigger.e==SA_save)
{
do_save:;
if(!f->curdir)
{
fprintf(stderr, "Error: f->curdir==NULL\n");
}
else if(!f->value)
{
fprintf(stderr, "Select/enter a filename first!\n");
}
else
{
char *file=malloc(strlen(f->curdir)+strlen(f->value)+1);
sprintf(file, "%s%s", f->curdir, f->value);
fprintf(stderr, "Saving game state to '%s'...\n", file);
if(!savegame(file, *state))
{
fprintf(stderr, "Game saved\n");
return(SCRN_CONTROL);
}
else
{
fprintf(stderr, "Failed to save to file\n");
}
}
}
else if(trigger.e==SA_text)
{
free(f->value);
f->value=NULL;
if(SA_btext)
SA_btext[0]=0;
}
else if(!trigger.e)
{
// internal error
}
else
fprintf(stderr, "Clicked on unknown button!\n");
break;
case ATG_EV_VALUE:;
atg_ev_value value=e.event.value;
if(value.e==SA_file)
{
if(SA_btext)
{
if(f->value)
snprintf(SA_btext, NAMEBUFLEN, "%s", f->value);
else
SA_btext[0]=0;
}
}
break;
default:
break;
}
}
SDL_Delay(50);
}
}
void save_game_free(void)
{
atg_free_element(save_game_box);
}