-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
425 lines (370 loc) · 10.7 KB
/
main.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
CS 349 A1 Skeleton Code - Snake
- - - - - - - - - - - - - - - - - - - - - -
Commands to compile and run:
g++ -o snake snake.cpp -L/usr/X11R6/lib -lX11 -lstdc++
./snake
Note: the -L option and -lstdc++ may not be needed on some machines.
*/
#include <iostream>
#include <list>
#include <cstdlib>
#include <sys/time.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sstream>
#include <string>
/*
* Header files for X functions
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "displayable.h"
#include "fruit.h"
#include "snake.h"
#include "splash.h"
#include "score.h"
using namespace std;
/*
* Global game state variables
*/
const int Border = 1;
const int BufferSize = 10;
const int width = 800;
const int height = 600;
const int DEFAULT_FPS = 30;
const int DEFAULT_SNAKE_SPEED = 5;
const int SLEEP_NUMERATOR_US = 1000000;
const int splashTextWidth = 8;
const int splashTextHeight = 25;
const string TITLE("Snake v1.0");
const string AUTHOR_NAME("QiYang Huang");
const string USER_ID("Q43HUANG");
const string GAME_DESC[] = {
"Use the arrow keys to control your snake ;)",
"Don't run into obstacles such as walls or some shit"
};
const string GAME_CONTROLS[] = {
"Press ENTER anytime to begin a new game",
"Press q anytime to quit the game"
};
const string SPLASH_SCREEN_TEXT[] = {
TITLE,
AUTHOR_NAME,
USER_ID,
GAME_DESC[0],
GAME_DESC[1],
GAME_CONTROLS[0],
GAME_CONTROLS[1]
};
const string PAUSE_TEXT = "Game Paused, Press ESCAPE to continue, ENTER to restart.";
const int SPLASH_SCREEN_TEXT_ROWS = 7;
int FPS = DEFAULT_FPS;
int speed = DEFAULT_SNAKE_SPEED;
SplashText *sp[SPLASH_SCREEN_TEXT_ROWS];
/*
* Function to put out a message on error exits.
*/
void error( string str ) {
cerr << str << endl;
exit(0);
}
list<Displayable *> dList; // list of Displayables
Score *score = new Score(width / 2, height - 40, "Score: 0");
Fruit *fruit = new Fruit(width, height);
Snake *snake = new Snake(0, 450, fruit, score);
SplashText pauseText((width - (8 * PAUSE_TEXT.length())) / 2, height / 2, PAUSE_TEXT);
/*
* Initialize X and create a window
*/
void initX(int argc, char *argv[], XInfo &xInfo) {
XSizeHints hints;
unsigned long white, black;
/*
* Display opening uses the DISPLAY environment variable.
* It can go wrong if DISPLAY isn't set, or you don't have permission.
*/
xInfo.display = XOpenDisplay( "" );
if ( !xInfo.display ) {
error( "Can't open display." );
}
/*
* Find out some things about the display you're using.
*/
xInfo.screen = DefaultScreen( xInfo.display );
white = XWhitePixel( xInfo.display, xInfo.screen );
black = XBlackPixel( xInfo.display, xInfo.screen );
hints.x = 100;
hints.y = 100;
hints.width = 800;
hints.height = 600;
hints.flags = PPosition | PSize;
xInfo.window = XCreateSimpleWindow(
xInfo.display, // display where window appears
DefaultRootWindow( xInfo.display ), // window's parent in window tree
hints.x, hints.y, // upper left corner location
hints.width, hints.height, // size of the window
Border, // width of window's border
black, // window border colour
white ); // window background colour
XSetStandardProperties(
xInfo.display, // display containing the window
xInfo.window, // window whose properties are set
TITLE.c_str(), // window's title
"Snake", // icon's title
None, // pixmap for the icon
argv, argc, // applications command line args
&hints ); // size hints for the window
/*
* Create Graphics Contexts
*/
//for snake
int i = 0;
xInfo.gc[i] = XCreateGC(xInfo.display, xInfo.window, 0, 0);
XSetForeground(xInfo.display, xInfo.gc[i], WhitePixel(xInfo.display, xInfo.screen));
XSetBackground(xInfo.display, xInfo.gc[i], BlackPixel(xInfo.display, xInfo.screen));
XSetFillStyle(xInfo.display, xInfo.gc[i], FillSolid);
XSetLineAttributes(xInfo.display, xInfo.gc[i],
1, LineSolid, CapButt, JoinRound);
i = 1;
xInfo.gc[i] = XCreateGC(xInfo.display, xInfo.window, 0, 0);
XSetForeground(xInfo.display, xInfo.gc[i], BlackPixel(xInfo.display, xInfo.screen));
XSetBackground(xInfo.display, xInfo.gc[i], WhitePixel(xInfo.display, xInfo.screen));
XSetFillStyle(xInfo.display, xInfo.gc[i], FillSolid);
XSetLineAttributes(xInfo.display, xInfo.gc[i],
1, LineSolid, CapButt, JoinRound);
XSelectInput(xInfo.display, xInfo.window,
ButtonPressMask | KeyPressMask |
PointerMotionMask |
EnterWindowMask | LeaveWindowMask |
StructureNotifyMask); // for resize events
//XWindowAttributes windowInfo;
//XGetWindowAttributes(xInfo.display, xInfo.window, &windowInfo);
int depth = DefaultDepth(xInfo.display, DefaultScreen(xInfo.display));
xInfo.pixmap = XCreatePixmap(xInfo.display, xInfo.window, width, height, depth);
XSetWindowBackgroundPixmap(xInfo.display, xInfo.window, None);
/*
* Put the window on the screen.
*/
XCopyArea(xInfo.display, xInfo.pixmap, xInfo.window, xInfo.gc[1], 0, 0, width, height, 0, 0);
XMapRaised( xInfo.display, xInfo.window );
XFlush(xInfo.display);
}
void unPaintAll(XInfo &xInfo) {
list<Displayable *>::const_iterator begin = dList.begin();
list<Displayable *>::const_iterator end = dList.end();
while( begin != end ) {
Displayable *d = *begin;
//if (d != NULL) {
d->unPaint(xInfo);
//}
begin++;
}
dList.clear();
}
/*
* Function to repaint a display list
*/
void repaint( XInfo &xInfo) {
list<Displayable *>::const_iterator begin = dList.begin();
list<Displayable *>::const_iterator end = dList.end();
while( begin != end ) {
Displayable *d = *begin;
//if (d != NULL) {
d->paint(xInfo);
//}
begin++;
}
XCopyArea(xInfo.display, xInfo.pixmap, xInfo.window, xInfo.gc[1], 0, 0, width, height, 0, 0);
//XFreePixmap(xInfo.display, xInfo.pixmap);
XFlush( xInfo.display );
}
bool handleKeyPress(XInfo &xInfo, XEvent &event, bool &isInEventLoop, bool &isInPause, bool &hasLost) {
KeySym key;
char text[BufferSize];
/*
* Exit when 'q' is typed.
* This is a simplified approach that does NOT use localization.
*/
int i = XLookupString(
(XKeyEvent *)&event, // the keyboard event
text, // buffer when text will be written
BufferSize, // size of the text buffer
&key, // workstation-independent key symbol
NULL ); // pointer to a composeStatus structure (unused)
//if ( i == 1) {
//printf("Got key press -- %c\n", text[0]);
if (text[0] == 'q') {
error("Terminating normally.");
}
if (text[0] == 'n') {
hasLost = false;
isInPause = false;
isInEventLoop = false;
unPaintAll(xInfo);
cout << "Returning to main menu" << endl;
for (int k = 0; k < SPLASH_SCREEN_TEXT_ROWS; ++k) {
dList.push_back(sp[k]);
}
repaint(xInfo);
}
if ((key == XK_Return)) {
//newGame(xInfo, isInGame);
isInPause = false;
hasLost = false;
return true;
}
if ((key == XK_Escape) && (isInEventLoop)) {
if (isInPause) {
dList.remove(&pauseText);
pauseText.unPaint(xInfo);
isInPause = !isInPause;
repaint(xInfo);
} else {
dList.push_front(&pauseText);
isInPause = !isInPause;
repaint(xInfo);
}
}
if ((key == XK_Left) && (isInEventLoop)) {
snake->turnLeft();
}
if ((key == XK_Right) && (isInEventLoop)) {
snake->turnRight();
}
//}
return false;
}
void handleAnimation(XInfo &xInfo, bool &hasLost) {
/*
* ADD YOUR OWN LOGIC
* This method handles animation for different objects on the screen and readies the next frame before the screen is re-painted.
*/
if (snake->move(xInfo)) {
repaint(xInfo);
} else {
hasLost = true;
}
}
// get microseconds
unsigned long now() {
timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
void newGame(XInfo &xInfo, bool &isInGame) {
cout << "Starting game." << endl;
unPaintAll(xInfo);
free(snake);
free(fruit);
free(score);
score = new Score(width / 2, height - 40, "Score: 0");
fruit = new Fruit(width, height);
snake = new Snake(0, 450, fruit, score);
snake->setSpeed(speed);
dList.push_front(snake);
dList.push_front(fruit);
dList.push_front(score);
isInGame = true;
}
void eventLoop(XInfo &xInfo) {
// Add stuff to paint to the display list
string gameOverText = "Game Over. Press n to return to main screen, ENTER for a new game, or q to quit.";
SplashText gameOver((width - (splashTextWidth * gameOverText.length())) / 2, height / 2, gameOverText);
for (int k = 0; k < SPLASH_SCREEN_TEXT_ROWS; ++k) {
string str = SPLASH_SCREEN_TEXT[k];
sp[k] = new SplashText((width - (splashTextWidth * SPLASH_SCREEN_TEXT[k].length())) / 2, (height / 2 - (splashTextHeight * SPLASH_SCREEN_TEXT_ROWS)) + (k * splashTextHeight), str);
dList.push_back(sp[k]);
repaint(xInfo);
}
bool isInGame = false;
bool isPaused = false;
bool hasLost = false;
XEvent event;
unsigned long lastRepaint = 0;
int inside = 0;
repaint(xInfo);
while( true ) {
/*
* This is NOT a performant event loop!
* It needs help!
*/
if ((!isInGame) && (!isPaused) && (!hasLost)) {
for (int k = 0; k < SPLASH_SCREEN_TEXT_ROWS; ++k) {
string str = SPLASH_SCREEN_TEXT[k];
dList.push_back(sp[k]);
repaint(xInfo);
}
}
while (XPending(xInfo.display) > 0) {
XNextEvent( xInfo.display, &event );
//cout << "event.type=" << event.type << "\n";
switch( event.type ) {
case KeyPress:
if (handleKeyPress(xInfo, event, isInGame, isPaused, hasLost)) {
newGame(xInfo, isInGame);
}
break;
case EnterNotify:
inside = 1;
break;
case LeaveNotify:
inside = 0;
break;
}
}
usleep(SLEEP_NUMERATOR_US/FPS);
if (!isPaused && isInGame) {
handleAnimation(xInfo, hasLost);
}
if (hasLost) {
unPaintAll(xInfo);
isInGame = false;
dList.push_back(&gameOver);
dList.push_back(score);
repaint(xInfo);
//cout << "GAME OVER" << endl;
}
}
for (int k = 0; k < SPLASH_SCREEN_TEXT_ROWS; ++k) {
free(sp[k]);
}
free(snake);
free(fruit);
free(score);
}
/*
* Start executing here.
* First initialize window.
* Next loop responding to events.
* Exit forcing window manager to clean up - cheesy, but easy.
*/
int main ( int argc, char *argv[] ) {
XInfo xInfo;
switch (argc) {
case 3:
{
stringstream ss(argv[1]);
ss >> FPS;
ss.clear();
ss.str(argv[2]);
ss >> speed;
if (speed < 1 || speed > 10) error("Speed argument must be in the range of 1-10.");
if (FPS < 1 || FPS > 100) error("FPS argument must be in the range of 1-100.");
break;
}
default:
{
error("There needs to be a framerate argument as well as a speed argument supplied.");
break;
}
}
snake->setSpeed(speed);
cout << "Speed is: " << snake->getSpeed() << endl;
cout << "FPS is: " << FPS << endl;
initX(argc, argv, xInfo);
//splashScreen(xInfo);
eventLoop(xInfo);
XCloseDisplay(xInfo.display);
}