-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cpp
181 lines (148 loc) · 4.84 KB
/
game.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
#include "game.hpp"
Game::Game(int w, int h): WIDTH(w), HEIGHT(h){
//titleText = Object("models/title.obj");
titleText = Object("models/untitled.obj");
}
Game::~Game(){
}
void Game::init(){
setBgColor(1.0f, 1.0f, 1.0f);
}
void Game::renderScene(){
glClearColor(bgColorR, bgColorG, bgColorB, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(gameState == TITLE){
// Lighting
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
float light_ambient[] = {1, 1, 1, 1};
float light_diffuse[] = {0.7, 0.7, 0.7, 1};
float light_specular[] = {0.5, 0.5, 0.5, 1};
float light_position[] = {1, 1, 1, 0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, ((float)WIDTH)/(float)HEIGHT, 0.1f, 100.0f);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.5f, 1.3f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(0, 0.2, 0);
glRotatef(20.0f * sin(1.0f * titleCubeAngle), 1, 0, 0);
glRotatef(20.0f * sin(0.5f * titleCubeAngle), 0, 1, 0);
titleText.draw();
glPopMatrix();
glPushMatrix();
glTranslatef(0, -0.5, -1);
glRotatef(2.0f * titleCubeAngle, 0, 1, 0);
glColor3f(0, 0, 0);
glutSolidCube(0.5);
glPopMatrix();
}
else if(gameState == PLAY_1P){
// Lighting
glDisable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
territory->drawMap(-1, 1);
}
glutSwapBuffers();
}
void Game::normalKeys(unsigned char key, int x, int y){
switch(key){
case '\e': // ESC
exit(0);
break;
}
switch(gameState){
case TITLE:
switch(key){
case '\r': // ENTER
// PLAY_1P로 전환
gameState = PLAY_1P;
int terrW = WIDTH / 10, terrH = HEIGHT / 10;
territory = new Territory(terrW, terrH);
territory->setPlayerTerritory(terrW/2-5, terrH/2-5, terrW/2+5, terrH/2+5);
territory->setPlayerPosition(terrW/2, terrH/2);
territory->createZombies(10, 100);
setBgColor(0, 0, 0);
glutPostRedisplay();
break;
}
break;
case PLAY_1P:
break;
}
}
void Game::specialKeys(int key, int x, int y){
switch(gameState){
case TITLE:
break;
case PLAY_1P:
switch(key){
case GLUT_KEY_UP:
territory->setPlayerDirection(UP);
break;
case GLUT_KEY_DOWN:
territory->setPlayerDirection(DOWN);
break;
case GLUT_KEY_LEFT:
territory->setPlayerDirection(LEFT);
break;
case GLUT_KEY_RIGHT:
territory->setPlayerDirection(RIGHT);
break;
}
break;
}
}
void Game::idleFunc(){
// FPS control
if(clock() - startTimeForFPS < 1000.0f / FPS) return;
startTimeForFPS = clock();
switch(gameState){
case TITLE:
titleCubeAngle += 0.1f;
break;
case PLAY_1P:
idlePerTerritoryUpdateCount++;
if(idlePerTerritoryUpdateCount == idlePerTerritoryUpdate){
territory->update();
idlePerTerritoryUpdateCount = 0;
}
break;
}
glutPostRedisplay();
}
void Game::reshapeFunc(int w, int h){
switch(gameState){
case TITLE:
if(w > MIN_WIDTH) WIDTH = w;
else WIDTH = MIN_WIDTH;
if(h > MIN_HEIGHT) HEIGHT = h;
else HEIGHT = MIN_HEIGHT;
if(HEIGHT > WIDTH) WIDTH = HEIGHT;
WIDTH /= 10; HEIGHT /= 10;
WIDTH *= 10; HEIGHT *= 10;
break;
case PLAY_1P:
break;
}
glutReshapeWindow(WIDTH, HEIGHT);
glViewport(0, 0, WIDTH, HEIGHT);
glutPostRedisplay();
}