-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_menu.c
190 lines (182 loc) · 4.48 KB
/
main_menu.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
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
main_menu: the main menu screen
*/
#include <stdio.h>
#include "main_menu.h"
#include "ui.h"
#include "globals.h"
#include "saving.h"
#include "setup_game.h"
#include "control.h"
atg_element *main_menu_box;
atg_element *MM_full, *MM_Exit, *MM_QuickStart, *MM_NewGame, *MM_LoadGame;
int main_menu_create(void)
{
if(!(main_menu_box=atg_create_element_box(ATG_BOX_PACK_VERTICAL, (atg_colour){0, 0, 0, ATG_ALPHA_OPAQUE})))
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
atg_element *MM_topbox=atg_create_element_box(ATG_BOX_PACK_HORIZONTAL, (atg_colour){0, 0, 0, ATG_ALPHA_OPAQUE});
if(!MM_topbox)
{
fprintf(stderr, "atg_create_element_box failed\n");
return(1);
}
if(atg_ebox_pack(main_menu_box, MM_topbox))
{
perror("atg_ebox_pack");
return(1);
}
atg_element *MM_title=atg_create_element_label("HARRIS: Main Menu", 12, (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE});
if(!MM_title)
{
fprintf(stderr, "atg_create_element_label failed\n");
return(1);
}
if(atg_ebox_pack(MM_topbox, MM_title))
{
perror("atg_ebox_pack");
return(1);
}
MM_full=atg_create_element_image(fullbtn);
if(!MM_full)
{
fprintf(stderr, "atg_create_element_image failed\n");
return(1);
}
MM_full->clickable=true;
if(atg_ebox_pack(MM_topbox, MM_full))
{
perror("atg_ebox_pack");
return(1);
}
MM_QuickStart=atg_create_element_button("Quick Start Game", (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE}, (atg_colour){47, 47, 47, ATG_ALPHA_OPAQUE});
if(!MM_QuickStart)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(main_menu_box, MM_QuickStart))
{
perror("atg_ebox_pack");
return(1);
}
MM_NewGame=atg_create_element_button("Set Up New Game", (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE}, (atg_colour){47, 47, 47, ATG_ALPHA_OPAQUE});
if(!MM_NewGame)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(main_menu_box, MM_NewGame))
{
perror("atg_ebox_pack");
return(1);
}
MM_LoadGame=atg_create_element_button("Load Game", (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE}, (atg_colour){47, 47, 47, ATG_ALPHA_OPAQUE});
if(!MM_LoadGame)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(main_menu_box, MM_LoadGame))
{
perror("atg_ebox_pack");
return(1);
}
MM_Exit=atg_create_element_button("Exit", (atg_colour){255, 255, 255, ATG_ALPHA_OPAQUE}, (atg_colour){47, 47, 47, ATG_ALPHA_OPAQUE});
if(!MM_Exit)
{
fprintf(stderr, "atg_create_element_button failed\n");
return(1);
}
if(atg_ebox_pack(main_menu_box, MM_Exit))
{
perror("atg_ebox_pack");
return(1);
}
MM_QuickStart->w=MM_NewGame->w=MM_LoadGame->w=MM_Exit->w=136;
return(0);
}
screen_id main_menu_screen(atg_canvas *canvas, game *state)
{
atg_resize_canvas(canvas, 136, 86);
atg_event e;
while(1)
{
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(NUM_SCREENS);
}
break;
case ATG_EV_CLICK:;
atg_ev_click c=e.event.click;
if(c.e==MM_full)
{
fullscreen=!fullscreen;
atg_setopts_canvas(canvas, fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE);
}
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==MM_Exit)
return(NUM_SCREENS);
else if(trigger.e==MM_QuickStart)
{
fprintf(stderr, "Loading game state from Quick Start file...\n");
if(!loadgame("save/qstart.sav", state))
{
fprintf(stderr, "Quick Start Game loaded\n");
// Disable the xbombers
for(unsigned int i=0;i<ntypes;i++)
state->btypes[i]=!types[i].extra;
game_preinit(state);
return(SCRN_CONTROL);
}
else
{
fprintf(stderr, "Failed to load Quick Start save file\n");
}
}
else if(trigger.e==MM_NewGame)
{
selstart=-1;
return(SCRN_SETPGAME);
}
else if(trigger.e==MM_LoadGame)
{
return(SCRN_LOADGAME);
}
else if(!trigger.e)
{
// internal error
}
else
fprintf(stderr, "Clicked on unknown button!\n");
break;
default:
break;
}
}
SDL_Delay(50);
}
}
void main_menu_free(void)
{
atg_free_element(main_menu_box);
}