-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
228 lines (189 loc) · 4.63 KB
/
menu.cpp
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
/* menu.cpp
* Draws and deals with the menus... pretty simple
* Licensed under the Open Software License version 2.0
* coded by ReKleSS [[email protected]]
*/
#ifdef WIN32
#include <windows.h>
#endif
#include <SDL.h>
#include <GL/gl.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include "ss.h"
#include "text.h"
#include "menu.h"
#include "levels.h"
#include "game.h"
#define DIR_DOWN 0
#define DIR_UP 1
extern GLuint textures[12];
extern bool keys[256];
extern int state;
extern GLint shipList;
extern Text text;
extern Uint32 g_time;
extern long currentTime;
extern Level lvlLevel;
void Menu::addEntry(const char *name)
{
menuEntry *current = &head;
while (current->next != NULL){
current = current->next;
}
current->next = (menuEntry *)malloc(sizeof(struct menuEntry));
current = current->next;
current->filename = strdup(name);
current->next = NULL;
elements++;
return;
}
Menu::Menu(){
return;
}
void Menu::init()
{
head.next = NULL;
selected = 0;
memset(&whacked, 0, sizeof(bool) * 256);
char fullpath[256];
strcpy(fullpath, basePath);
char nextfile[32];
strcat(fullpath, "levels/level1.lvl");
FILE *f;
addEntry("level1.lvl");
while ((f = fopen(fullpath, "r"))){
fscanf(f, "%*i %*s %s", nextfile); //%\*? means discard
printf("%s\n", nextfile);
if (strcmp(nextfile, "end") == 0){
fclose(f);
break;
}
addEntry(nextfile);
strcpy(fullpath, basePath);
strcat(fullpath, "levels/");
strcat(fullpath, nextfile);
fclose(f);
}
return;
}
void Menu::runFrame()
{
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -0.84f);
glBindTexture(GL_TEXTURE_2D, textures[10]);
glBegin(GL_QUADS); //draw the background image
glTexCoord2f(0, 1);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1, 1);
glVertex2f(1.0f, -1.0f);
glTexCoord2f(1, 0);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex2f(-1.0f, 1.0f);
glEnd();
glTranslatef(0.0f, 0.0f, 0.1f);
glEnable(GL_BLEND);
glPushMatrix();
glTranslatef(-0.8f, 0.4f, 0.0f);
glScalef(0.1f, 0.1f, 0.1f);
glBindTexture(GL_TEXTURE_2D, textures[14]);
if (selected == 0)
glColor3f(1.0f, 0.1f, 0.1f);
else
glColor3f(1.0f, 1.0f, 1.0f);
text.drawf("Play");
glTranslatef(0.0f, -1.0f, 0.0f);
if (selected == 1)
glColor3f(1.0f, 0.1f, 0.1f);
else
glColor3f(1.0f, 1.0f, 1.0f);
text.drawf("Quit");
glTranslatef(6.0f, 3.0f, 0.0f);
int index = 2;
if (head.next){ //iterate through the list, draw the items as necessary
menuEntry *current = head.next;
glScalef(0.8f, 0.8f, 1.0f);
while (current != NULL){
if (selected == index)
glColor3f(1.0f, 0.1f, 0.1f);
else
glColor3f(1.0f, 1.0f, 1.0f);
text.drawf(current->filename);
glTranslatef(0.0f, -1.0f, 0.0f);
current = current->next;
index++;
}
}
glPopMatrix();
//handle keystrokes
if (keys[KEY_DOWN] && !whacked[KEY_DOWN]) {
selected++;
if (selected > elements+1)
selected = 0;
whacked[KEY_DOWN] = true;
} else if (!keys[KEY_DOWN] && whacked[KEY_DOWN])
whacked[KEY_DOWN] = false;
if (keys[KEY_UP] && !whacked[KEY_UP]) {
selected--;
if (selected < 0)
selected = elements + 1;
whacked[KEY_UP] = true;
} else if (!keys[KEY_UP] && whacked[KEY_UP])
whacked[KEY_UP] = false;
if (keys[KEY_LEFT] && !whacked[KEY_LEFT]){
selected = 0;
whacked[KEY_LEFT] = true;
} else if (!keys[KEY_LEFT] && whacked[KEY_LEFT])
whacked[KEY_LEFT] = false;
if (keys[KEY_RIGHT] && !whacked[KEY_RIGHT]) {
selected = 2;
whacked[KEY_RIGHT] = true;
} else if (!keys[KEY_RIGHT] && whacked[KEY_RIGHT])
whacked[KEY_RIGHT] = false;
if (keys[KEY_JUMP]) { //we got a space; figure out wtf the user wants
switch(selected){
case 0:
state = GAME;
memset(&keys, 0, sizeof(bool) * 256); //zero the key array
g_time = SDL_GetTicks();
currentTime = SDL_GetTicks();
return;
case 1:
SDL_Event quitEvent;
quitEvent.type = SDL_QUIT;
SDL_PushEvent(&quitEvent);
default: //levels
menuEntry *current = head.next;
int i=0;
for (i = 0;i<selected - 2;i++)
current = current->next;
char fullpath[64];
strcpy(fullpath, "levels/");
strcat(fullpath, current->filename);
lvlLevel.clear();
game_Reset();
lvlLevel.initialize(fullpath);
state = GAME;
currentTime = SDL_GetTicks();
g_time = SDL_GetTicks();
}
}
glDisable(GL_BLEND);
glColor3f(0.8f, 0.8f, 0.8f);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTranslatef(4.0f, -4.0f, -5.0f);
glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glCallList(shipList);
SDL_GL_SwapBuffers();
}
Menu::~Menu()
{
return;
}