-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
120 lines (105 loc) · 1.6 KB
/
menu.c
File metadata and controls
120 lines (105 loc) · 1.6 KB
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
/**
* menu.c
*/
#define _menu_C 1
#include <SDL/SDL_keysym.h>
#include "menu.h"
#include "gr.h"
#include "main.h"
static void
menu_newgame()
{
if (main_loadmap(1)) {
main_switchtogame();
}
}
static void
menu_restartmap()
{
if (main_restartmap()) {
main_switchtogame();
}
}
static void
menu_nextmap()
{
if (main_nextmap()) {
main_switchtogame();
}
}
static void
menu_prevmap()
{
if (main_prevmap()) {
main_switchtogame();
}
}
static struct {
const char *name;
void (*action)(void);
} items[] = {
{ "New game", menu_newgame },
{ "Restart map", menu_restartmap},
{ "Select map", main_switchtomapsel },
{ "Next map", menu_nextmap },
{ "Previous map", menu_prevmap },
{ "Exit game", postQuit },
{ NULL, NULL }
};
static int curitem = 0;
static void
menu_drawitem(int i)
{
grSetColor(0xffff);
grSetPos(16, i * 16);
grprintf("%s", items[i].name);
if (i == curitem) {
grSetColor(grRGB(0xff, 0xff, 0));
} else {
grSetColor(0);
}
grOctagon(8, i * 16 + 10, 4);
}
void
menu_keyboard(int key)
{
int oci = curitem;
switch (key) {
case SDLK_UP:
--curitem;
break;
case SDLK_DOWN:
++curitem;
break;
case SDLK_RETURN:
if (items[curitem].action != NULL) {
items[curitem].action();
}
break;
case SDLK_ESCAPE:
main_switchtogame();
break;
}
if (curitem < 0) {
curitem = 0;
} else if (items[curitem].name == NULL) {
--curitem;
}
if (curitem != oci) {
grBegin();
menu_drawitem(oci);
menu_drawitem(curitem);
grEnd();
}
}
void
menu_draw()
{
int i;
grBegin();
grClear();
for (i = 0; items[i].name != NULL; ++i) {
menu_drawitem(i);
}
grEnd();
}