-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup_difficulty.c
340 lines (330 loc) · 8.42 KB
/
setup_difficulty.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
setup_difficulty: screen for new game difficulty settings
*/
#include <stdio.h>
#include <unistd.h>
#include "setup_difficulty.h"
#include "ui.h"
#include "globals.h"
atg_element *setup_difficulty_box;
atg_element *SD_topbox, *SD_topbox_show;
atg_element *SD_full, *SD_exit, *SD_back, *SD_cont, *SD_row0;
atg_element *SD_diff[DIFFICULTY_CLASSES][3];
bool difficulty_show_only;
#define SD_BG_COLOUR (atg_colour){31, 31, 47, ATG_ALPHA_OPAQUE}
int setup_difficulty_create(void)
{
setup_difficulty_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, SD_BG_COLOUR);
if(!setup_difficulty_box)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
SD_topbox=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, SD_BG_COLOUR);
if(!SD_topbox)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(setup_difficulty_box, SD_topbox))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *SD_title=atg_create_element_label("Set Up New Game: Difficulty Settings", 12, (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE});
if(!SD_title)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
if(atg_ebox_pack(SD_topbox, SD_title))
{
perror("atg_ebox_pack");
return(1);
}
SD_full=atg_create_element_image(fullbtn);
if(!SD_full)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
SD_full->clickable=true;
if(atg_ebox_pack(SD_topbox, SD_full))
{
perror("atg_ebox_pack");
return(1);
}
SD_exit=atg_create_element_image(exitbtn);
if(!SD_exit)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
SD_exit->clickable=true;
if(atg_ebox_pack(SD_topbox, SD_exit))
{
perror("atg_ebox_pack");
return(1);
}
SD_topbox_show=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, SD_BG_COLOUR);
if(!SD_topbox_show)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(setup_difficulty_box, SD_topbox_show))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *SD_title_show=atg_create_element_label("Show Difficulty Settings", 12, (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE});
if(!SD_title_show)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
if(atg_ebox_pack(SD_topbox_show, SD_title_show))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *SD_headrow=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, SD_BG_COLOUR);
if(!SD_headrow)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(setup_difficulty_box, SD_headrow))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *head_name=atg_create_element_label("Category", 12, (atg_colour){247, 247, 247, ATG_ALPHA_OPAQUE});
if(!head_name)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
head_name->w=160;
if(atg_ebox_pack(SD_headrow, head_name))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *head_desc=atg_create_element_label("Description", 12, (atg_colour){247, 247, 247, ATG_ALPHA_OPAQUE});
if(!head_desc)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
head_desc->w=480;
if(atg_ebox_pack(SD_headrow, head_desc))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *head_level=atg_create_element_label("Difficulty setting", 12, (atg_colour){247, 247, 247, ATG_ALPHA_OPAQUE});
if(!head_level)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
head_level->w=48*3;
if(atg_ebox_pack(SD_headrow, head_level))
{
perror("atg_ebox_pack");
return(1);
}
char *level_names[3]={"Easy", "Medium", "Hard"};
atg_colour level_colours[3]={{127, 247, 127, ATG_ALPHA_OPAQUE}, {223, 223, 127, ATG_ALPHA_OPAQUE}, {247, 127, 127, ATG_ALPHA_OPAQUE}};
for(unsigned int d=0;d<DIFFICULTY_CLASSES;d++)
{
atg_element *SD_row=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, SD_BG_COLOUR);
if(!SD_row)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(setup_difficulty_box, SD_row))
{
perror("atg_ebox_pack");
return(1);
}
if(!d)
SD_row0=SD_row;
atg_element *row_name=atg_create_element_label(dclasses[d].name, 11, (atg_colour){239, 239, 223, ATG_ALPHA_OPAQUE});
if(!row_name)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
row_name->w=160;
if(atg_ebox_pack(SD_row, row_name))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *row_desc=atg_create_element_label(dclasses[d].desc, 11, (atg_colour){223, 223, 223, ATG_ALPHA_OPAQUE});
if(!row_desc)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
row_desc->w=480;
if(atg_ebox_pack(SD_row, row_desc))
{
perror("atg_ebox_pack");
return(1);
}
for(unsigned int i=0;i<3;i++)
{
if(!(SD_diff[d][i]=atg_create_element_toggle(level_names[i], false, level_colours[i], SD_BG_COLOUR)))
{
fprintf(stderr, "atg_create_element_toggle failed\n");
return(1);
}
SD_diff[d][i]->w=48;
if(atg_ebox_pack(SD_row, SD_diff[d][i]))
{
perror("atg_ebox_pack");
return(1);
}
}
}
atg_element *SD_nav=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, SD_BG_COLOUR);
if(!SD_nav)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(setup_difficulty_box, SD_nav))
{
perror("atg_ebox_pack");
return(1);
}
if(!(SD_back=atg_create_element_button("Go Back", (atg_colour){239, 195, 195, ATG_ALPHA_OPAQUE}, (atg_colour){71, 63, 63, ATG_ALPHA_OPAQUE})))
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(SD_nav, SD_back))
{
perror("atg_ebox_pack");
return(1);
}
if(!(SD_cont=atg_create_element_button("Continue", (atg_colour){239, 239, 239, ATG_ALPHA_OPAQUE}, (atg_colour){63, 63, 63, ATG_ALPHA_OPAQUE})))
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(SD_nav, SD_cont))
{
perror("atg_ebox_pack");
return(1);
}
return(0);
}
screen_id setup_difficulty_screen(atg_canvas *canvas, game *state)
{
atg_event e;
bool mixed_difficulty=false;
for(unsigned int d=1;d<DIFFICULTY_CLASSES;d++)
if(state->difficulty[d]!=state->difficulty[0])
{
mixed_difficulty=true;
break;
}
SD_back->hidden=difficulty_show_only;
SD_topbox->hidden=difficulty_show_only;
SD_row0->hidden=difficulty_show_only;
SD_topbox_show->hidden=!difficulty_show_only;
while(1)
{
for(unsigned int d=0;d<DIFFICULTY_CLASSES;d++)
{
unsigned int dv=state->difficulty[d];
for(unsigned int i=0;i<3;i++)
{
atg_toggle *t=SD_diff[d][i]->elemdata;
t->state=(dv==i)&&(d||!mixed_difficulty);
}
}
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(difficulty_show_only?SCRN_CONTROL: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==SD_full)
{
fullscreen=!fullscreen;
atg_setopts_canvas(canvas, fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE);
}
else if (c.e==SD_exit)
return(difficulty_show_only?SCRN_CONTROL:SCRN_MAINMENU);
else if(c.e)
{
fprintf(stderr, "Clicked on unknown clickable!\n");
}
break;
case ATG_EV_TOGGLE:;
atg_ev_toggle toggle=e.event.toggle;
if(!difficulty_show_only)
for(unsigned int d=0;d<DIFFICULTY_CLASSES;d++)
for(unsigned int i=0;i<3;i++)
if(toggle.e==SD_diff[d][i])
{
state->difficulty[d]=i;
mixed_difficulty=d;
if(!d)
for(unsigned int d=1;d<DIFFICULTY_CLASSES;d++)
state->difficulty[d]=i;
goto toggle_found;
}
fprintf(stderr, "Clicked on unknown toggle!\n");
toggle_found:
break;
case ATG_EV_TRIGGER:;
atg_ev_trigger trigger=e.event.trigger;
if(trigger.e==SD_cont)
{
return(difficulty_show_only?SCRN_CONTROL:SCRN_SETPTYPS);
}
else if(trigger.e==SD_back&&!difficulty_show_only)
{
return(SCRN_SETPGAME);
}
else if(!trigger.e)
{
// internal error
}
else
fprintf(stderr, "Clicked on unknown button!\n");
break;
default:
break;
}
}
SDL_Delay(50);
}
}
void setup_difficulty_free(void)
{
atg_free_element(setup_difficulty_box);
}