-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (95 loc) · 2.55 KB
/
Copy pathmain.cpp
File metadata and controls
112 lines (95 loc) · 2.55 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
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <windows.h>
#include "game.h"
#include "menu.h"
#define COLUMNS 40
#define ROWS 40
extern short sDirection;
bool gameOver = false;
bool gameStarted = false;
int FPS = DIFFICULTY_MEDIUM;
int score = 0;
void timer_callback(int);
void display_callback();
void reshape_callback(int, int);
void keyboard_callback(int, int, int);
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
initGrid(COLUMNS, ROWS);
initMenu();
}
// No resetGame() function definition here, just using the one from game.cpp
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("SNAKE GAME");
glutDisplayFunc(display_callback);
glutReshapeFunc(reshape_callback);
glutTimerFunc(0, timer_callback, 0);
glutSpecialFunc(keyboard_callback);
glutMouseFunc(mouseCallback);
init();
glutMainLoop();
return 0;
}
void display_callback() {
glClear(GL_COLOR_BUFFER_BIT);
if (!gameStarted) {
// Display menu
drawMenu();
} else {
// Game is running
drawGrid();
drawSnake();
drawFood();
if (gameOver) {
char _score[10];
itoa(score, _score, 10);
char text[50] = "Your Score: ";
strcat(text, _score);
MessageBox(NULL, text, "GAME OVER", 0);
resetGame();
}
}
glutSwapBuffers();
}
void reshape_callback(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, COLUMNS, 0.0, ROWS, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void timer_callback(int) {
glutPostRedisplay();
glutTimerFunc(1000/FPS, timer_callback, 0);
}
void keyboard_callback(int key, int, int) {
if (gameStarted) {
switch(key) {
case GLUT_KEY_UP:
if (sDirection != DOWN)
sDirection = UP;
break;
case GLUT_KEY_DOWN:
if (sDirection != UP)
sDirection = DOWN;
break;
case GLUT_KEY_RIGHT:
if (sDirection != LEFT)
sDirection = RIGHT;
break;
case GLUT_KEY_LEFT:
if (sDirection != RIGHT)
sDirection = LEFT;
break;
case GLUT_KEY_F1:
// Escape key to return to menu
resetGame();
break;
}
}
}