-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEventListener.cpp
More file actions
408 lines (371 loc) · 11.2 KB
/
EventListener.cpp
File metadata and controls
408 lines (371 loc) · 11.2 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
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
#include <GL/glut.h>
#include <iostream>
#include <ctype.h>
#include <cstdlib>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "strstream"
#include "glFrame.h"
#include "math3d.cpp"
using std::strstreambuf;
using std::istrstream;
using std::ostrstream;
using std::strstream;
using std::fstream;
using namespace std;
const int ESC = 27;
const double PI = 3.14159;
/* ascii code for the escape key */
#define ESCAPE 27
#include "EventListener.h"
#include "Map.h"
EventListener::EventListener(GLint width, GLint height)
{
this->width = width;
this->height = height;
this->textureSwitch = true;
this->light1Switch = true;
this->light2Switch = true;
this->light3Switch = true;
this->light4Switch = true;
this->fullScreenMode = false;
this->cameraSwitch = 1;
this->mouseSwitch = false;
this->zoomByFovy = false;
this->yaw = 0.0;
this->roll = 0.0;
this->pitch = 0.0;
this->moveAngle = 90;
this->zoomMovingCamera = 45.0;
this->map = new Map();
this->mouse = new Mouse();
//Initiallize cameras
this->map->streetLights[0]->camera.SetOrigin(0.0,6.0,0.0);
this->map->streetLights[0]->camera.SetForwardVector(1.0,0.0,1.0);
this->map->streetLights[1]->camera.SetOrigin(0.0,6.0,56.0);
this->map->streetLights[1]->camera.SetForwardVector(1.0,0.0,-1.0);
this->map->streetLights[2]->camera.SetOrigin(56.0,6.0,0.0);
this->map->streetLights[2]->camera.SetForwardVector(-1.0,0.0,1.0);
this->map->streetLights[3]->camera.SetOrigin(56.0,6.0,56.0);
this->map->streetLights[3]->camera.SetForwardVector(-1.0,0.0,-1.0);
this->map->camera9.SetOrigin(28.0,0.0,100.0);
this->map->camera9.SetForwardVector(0.0,0.0,-1.0);
}
EventListener::~EventListener()
{
//dtor
}
void EventListener::drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(zoomMovingCamera, 1.5, 1.0, 120.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // Reset The View
switch (cameraSwitch)
{
case 1:
if (mouseSwitch==false)
gluLookAt(28.0,20.0,100.0,
28.0,0.0,28.0,
0.0,1.0,0.0);
else
this->mouse->mouseLookAt();
break;
case 2:
this->map->streetLights[0]->camera.ApplyCameraTransform();
break;
case 3:
this->map->streetLights[1]->camera.ApplyCameraTransform();
break;
case 4:
this->map->streetLights[2]->camera.ApplyCameraTransform();
break;
case 5:
this->map->streetLights[3]->camera.ApplyCameraTransform();
break;
case 6:
this->map->pacman->camera.ApplyCameraTransform();
break;
case 7:
this->map->ghosts[0]->camera.ApplyCameraTransform();
break;
case 8:
gluLookAt(28+72*cos(moveAngle*PI/180),20.0,28+72*sin(moveAngle*PI/180),
28.0,0.0,28.0,
0.0,1.0,0.0);
break;
case 9:
this->map->camera9.ApplyCameraTransform();
break;
default:
break;
}
this->map->drawAxis();
this->map->draw();
// since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers();
}
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void EventListener::initGL() // We call this right after our OpenGL window is created.
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
// Initialize the light.
glEnable(GL_LIGHTING);
//Ambient Light
glEnable(GL_LIGHT0);
//Street Lights
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHT4);
}
// Display some useful remarks in the Xterm window.
void EventListener::help ()
{
printf("Press a key to select a property.\n");
printf(" w wireframe mode \n");
printf(" f fill mode \n");
printf(" s smooth shading \n");
printf(" b flat shading \n");
printf(" r reset view position \n");
printf(" l pitch \n");
printf(" ; roll \n");
printf(" "" yaw \n");
printf(" 1 light1 camera \n");
printf(" 2 light2 camera \n");
printf(" 3 light3 camera \n");
printf(" 4 light4 camera \n");
printf(" 9 ghost camera \n");
printf(" 0 pacman camera \n");
printf(" d day \n");
printf(" n night \n");
printf(" u lightpost 1 \n");
printf(" i lightpost 2 \n");
printf(" o lightpost 3 \n");
printf(" z all texture mapping \n");
printf(" x pacman texture mapping \n");
printf(" c pellet texture mapping \n");
printf(" , moving camera left \n");
printf(" . moving camera right \n");
printf(" m moving main camera by mouse \n");
printf(" [ zoom in by fovy \n");
printf(" ] zoom out by fovy \n");
printf(" a autoplay \n");
printf(" 5 fullscreen \n");
printf(" 6 next level \n");
}
void EventListener::specialKeys(GLint key, GLint x, GLint y)
{
if(key == GLUT_KEY_UP)
{
char* canMove = this->map->whatDirectionsCanHeMove(this->map->pacman->getX(),this->map->pacman->getZ());
this->map->pacman->walkForward(canMove);
this->map->pacmanGhostCollisionDetection();
this->map->gotPellet();
}
if(key == GLUT_KEY_DOWN)
{
char* canMove = this->map->whatDirectionsCanHeMove(this->map->pacman->getX(),this->map->pacman->getZ());
this->map->pacman->walkBackward(canMove);
this->map->pacmanGhostCollisionDetection();
this->map->gotPellet();
}
if(key == GLUT_KEY_LEFT)
{
this->map->pacman->turnLeft();
}
if(key == GLUT_KEY_RIGHT)
{
this->map->pacman->turnRight();
}
// Refresh the Window
glutPostRedisplay();
}
void EventListener::resizeScene()
{
if (this->height==0) // Prevent A Divide By Zero If The Window Is Too Small
this->height=1;
if(!this->fullScreenMode)
glViewport(0, 0, this->width, this->height); // Reset The Current Viewport And Perspective Transformation
else
glViewport(0, 0, 1000, 1000);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)this->width/(GLfloat)this->height,0.1f,140.0f);
glMatrixMode(GL_MODELVIEW);
}
// Respond to a keystroke. Some responses are processed here; the default
// action is to record the keystroke and use it in the display callback function.
void EventListener::keys (unsigned char thiskey, GLint x, GLint y)
{
//wireframe, smooth, flat from http://mindfuck.de-brauwer.be/articles/polygon/index.php
switch (thiskey)
{
case 'ESC':
exit(0);
case '1':
cout << "Look from camera light 1" << endl;
cameraSwitch=2;
break;
case '2':
cout << "Look from camera light 2" << endl;
cameraSwitch=3;
break;
case '3':
cout << "Look from camera light 3" << endl;
cameraSwitch=4;
break;
case '4':
cout << "Look from camera light 4" << endl;
cameraSwitch=5;
break;
case '0':
cout << "Look from pacman" << endl;
cameraSwitch=6;
break;
case '9':
cout << "Look from a ghost" << endl;
cameraSwitch=7;
break;
case 'r':
cout << "Reset Camera" << endl;
cameraSwitch=1;
break;
case 'h': // Display help in text window
help();
break;
case 'w':
cout << "Wireframe mode on " << endl;
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glutPostRedisplay();
break;
case 'f':
cout << "Fill mode on " << endl;
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glutPostRedisplay();
break;
case 's':
cout << "Smooth shading " << endl;
glShadeModel(GL_SMOOTH);
glutPostRedisplay();
break;
case 'b':
cout << "Flat shading " << endl;
glShadeModel(GL_FLAT);
glutPostRedisplay();
break;
case 'd':
cout << "Day time " << endl;
glEnable(GL_LIGHT0);
break;
case 'n':
cout << "Night time " << endl;
glDisable(GL_LIGHT0);
break;
case 'y':
this->map->masterStreetLightSwitch();
break;
case 'u':
this->map->streetLightSwitch(0);
break;
case 'i':
this->map->streetLightSwitch(1);
break;
case 'o':
this->map->streetLightSwitch(2);
break;
case 'p':
this->map->streetLightSwitch(3);
break;
case 'c':
cout << "Switch Pellets Texture Maping" << endl;
this->map->switchPelletsTexture();
break;
case 'z':
cout << "Switch All Texture Maping" << endl;
this->map->switchAllTexture();
break;
case 'x':
this->map->pacman->switchTexture();
break;
case ',':
cout << "Moving Camera to Left" << endl;
cameraSwitch=8;
moveAngle += 1;
break;
case '.':
cout << "Moving Camera to Right" << endl;
cameraSwitch=8;
moveAngle -= 1;
break;
case 'm':
cout << "Moving Main Camera by Mouse" << endl;
if (mouseSwitch==false)
mouseSwitch=true;
else
mouseSwitch=false;
break;
case '[':
cout << "Zoom in by changing fovy " << endl;
cameraSwitch=8;
zoomMovingCamera += 2;
glutPostRedisplay();
break;
case ']':
cout << "Zoom out by changing fovy" << endl;
cameraSwitch=8;
zoomMovingCamera -= 2;
glutPostRedisplay();
break;
case 'l':
cout << " Pitching " << endl;
cameraSwitch=9;
this->map->camera9.RotateLocalX(this->pitch);
this->pitch += 0.001;
break;
case ';':
cout << " Rolling " << endl;
cameraSwitch=9;
this->map->camera9.RotateLocalZ(this->roll);
this->roll += 0.001;
break;
case '"':
cout << " Yawing " << endl;
cameraSwitch=9;
this->map->camera9.RotateLocalY(this->yaw);
this->yaw += 0.001;
break;
case 'a':
this->map->autoplay = !this->map->autoplay;
break;
case '5':
this->fullScreenMode = !this->fullScreenMode;
if(this->fullScreenMode==1)
{
glutFullScreen();
}
else
{
this->resizeScene();
glutPositionWindow(40,40);
glutReshapeWindow(this->width,this->height);
}
break;
case '6':
this->map->newLevel();
break;
default:
break;
}
glutPostRedisplay();
}