-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
103 lines (97 loc) · 2.28 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
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include "game.h"
#define ROWS 40.0
#define COLUMNS 40.0
std::ofstream ofile;
std::ifstream ifile;
bool game_over=false;
extern int sDirection;
int score=0;
void init();
void display_callback();
void input_callback(int,int,int);
void reshape_callback(int,int);
void timer_callback(int);
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(10,10);
glutInitWindowSize(600,600);
glutCreateWindow("CG Final Project: Nokia Snake by Rifat and Sahash");
glutDisplayFunc(display_callback);
glutReshapeFunc(reshape_callback);
glutSpecialFunc(input_callback);
glutTimerFunc(100,timer_callback,0);
init();
glutMainLoop();
return 0;
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
initGrid(COLUMNS,ROWS);
}
//Callbacks
void display_callback()
{
if(game_over)
{
ofile.open("score.dat",std::ios::trunc);
ofile<<score<<std::endl;
ofile.close();
ifile.open("score.dat",std::ios::in);
char a[4];
ifile>>a;
std::cout<<a;
char text[50]= "Your score : ";
strcat(text,a);
MessageBox(NULL,text,"Game Over",0);
exit(0);
}
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
draw_grid();
draw_food();
draw_snake();
glutSwapBuffers();
}
void reshape_callback(int w, int h)
{
glViewport(0,0,(GLfloat)w,GLfloat(h));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,COLUMNS,0.0,ROWS,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void timer_callback(int)
{
glutPostRedisplay();
glutTimerFunc(100,timer_callback,0);
}
void input_callback(int key,int x,int y)
{
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;
}
}