-
Notifications
You must be signed in to change notification settings - Fork 7
/
load_game.c
238 lines (228 loc) · 5.2 KB
/
load_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
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
load_game: the "Load Game" screen
*/
#include <stdio.h>
#include "load_game.h"
#include "ui.h"
#include "globals.h"
#include "saving.h"
#define NAMEBUFLEN 256
atg_element *load_game_box;
char *LB_btext;
atg_element *LB_file, *LB_text, *LB_full, *LB_exit, *LB_load;
int load_game_create(void)
{
load_game_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, (atg_colour){31, 31, 47, ATG_ALPHA_OPAQUE});
if(!load_game_box)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
LB_file=atg_create_element_filepicker("Load Game", NULL, (atg_colour){239, 239, 255, ATG_ALPHA_OPAQUE}, (atg_colour){31, 31, 47, ATG_ALPHA_OPAQUE});
if(!LB_file)
{
fprintf(stderr, "atg_create_element_filepicker failed\n");
return(1);
}
LB_file->h=mainsizey-30;
LB_file->w=mainsizex;
if(atg_ebox_pack(load_game_box, LB_file))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *LB_hbox=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!LB_hbox)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
else if(atg_ebox_pack(load_game_box, LB_hbox))
{
perror("atg_ebox_pack");
return(1);
}
else
{
LB_load=atg_create_element_button("Load", (atg_colour){239, 239, 239, ATG_ALPHA_OPAQUE}, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!LB_load)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
LB_load->w=34;
if(atg_ebox_pack(LB_hbox, LB_load))
{
perror("atg_ebox_pack");
return(1);
}
LB_text=atg_create_element_button_empty((atg_colour){239, 255, 239, ATG_ALPHA_OPAQUE}, (atg_colour){39, 39, 55, ATG_ALPHA_OPAQUE});
if(!LB_text)
{
fprintf(stderr, "atg_create_element_button_empty failed\n");
return(1);
}
LB_text->h=24;
LB_text->w=mainsizex-64;
if(atg_ebox_pack(LB_hbox, LB_text))
{
perror("atg_ebox_pack");
return(1);
}
if(!(LB_btext=malloc(NAMEBUFLEN)))
{
perror("malloc");
return(1);
}
atg_element *label=atg_create_element_label_nocopy(LB_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(LB_text, label))
{
perror("atg_ebox_pack");
return(1);
}
LB_btext[0]=0;
LB_full=atg_create_element_image(fullbtn);
if(!LB_full)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
LB_full->w=16;
LB_full->clickable=true;
if(atg_ebox_pack(LB_hbox, LB_full))
{
perror("atg_ebox_pack");
return(1);
}
LB_exit=atg_create_element_image(exitbtn);
if(!LB_exit)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
LB_exit->clickable=true;
if(atg_ebox_pack(LB_hbox, LB_exit))
{
perror("atg_ebox_pack");
return(1);
}
}
return(0);
}
screen_id load_game_screen(atg_canvas *canvas, game *state)
{
atg_event e;
atg_filepicker *lbf=LB_file->elemdata;
if(!lbf)
{
fprintf(stderr, "Error: LB_file->elemdata==NULL\n");
return(SCRN_MAINMENU);
}
free(lbf->value);
lbf->value=NULL;
while(1)
{
LB_file->w=mainsizex;
LB_file->h=mainsizey-30;
LB_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_MAINMENU);
case SDL_VIDEORESIZE:
mainsizex=canvas->surface->w;
mainsizey=canvas->surface->h;
break;
}
break;
case ATG_EV_CLICK:;
atg_ev_click c=e.event.click;
if(c.e==LB_full)
{
fullscreen=!fullscreen;
atg_setopts_canvas(canvas, fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE);
}
else if (c.e==LB_exit)
return(SCRN_MAINMENU);
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==LB_load)
{
if(!lbf->curdir)
{
fprintf(stderr, "Error: lbf->curdir==NULL\n");
}
else if(!lbf->value)
{
fprintf(stderr, "Select a file first!\n");
}
else
{
char *file=malloc(strlen(lbf->curdir)+strlen(lbf->value)+1);
sprintf(file, "%s%s", lbf->curdir, lbf->value);
fprintf(stderr, "Loading game state from '%s'...\n", file);
int rc=loadgame(file, state);
free(file);
if(!rc)
{
fprintf(stderr, "Game loaded\n");
return(SCRN_CONTROL);
}
else
{
fprintf(stderr, "Failed to load from save file\n");
}
}
}
else if(trigger.e==LB_text)
{
LB_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==LB_file)
{
if(lbf->value)
snprintf(LB_btext, NAMEBUFLEN, "%s", lbf->value);
else
LB_btext[0]=0;
}
break;
default:
break;
}
}
SDL_Delay(50);
}
}
void load_game_free(void)
{
atg_free_element(load_game_box);
}