forked from vishusmartishus/CS330
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneWindow.cpp
executable file
·472 lines (389 loc) · 11.6 KB
/
SceneWindow.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#else
#include <glut.h>
#include <GL/gl.h>
#endif
#include <iostream>
#include <sstream>
#include <string>
#include <time.h>
#include "SceneWindow.h"
#include "Mario.h"
#include "Level.h"
#include "LList.h"
#include "LListIterator.h"
#include "Movable.h"
#include "Game.h"
#include "Coin.h"
extern SceneWindow *sw;
Mario *mario;
Game *game;
Coin *coin;
const int WINDOWWIDTH = 512;
const int WINDOWHEIGHT = 448;
using namespace std;
SceneWindow::SceneWindow(int argc, char **argv)
{
// create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WINDOWWIDTH, WINDOWHEIGHT);
glutInitWindowPosition(30, 100);
glutCreateWindow("Mario");
// GLUT callbacks
glutDisplayFunc(&SceneWindow::displayFunc);
glutKeyboardFunc(&SceneWindow::keyboardFunc);
glutKeyboardUpFunc(&SceneWindow::keyboardUpFunc);
glutIgnoreKeyRepeat(true);
// initialize orthographic viewing projections
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WINDOWWIDTH, 0, WINDOWHEIGHT/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// view port initializer
viewportWidth_ = WINDOWWIDTH/2;
viewportHeight_ = WINDOWHEIGHT/2;
viewportLeftX_ = 0;
viewportRightX_ = viewportLeftX_ + viewportWidth_;
glViewport(0, 0, viewportWidth_, viewportHeight_);
glClearColor(0, 0, 0, 0);
start_=false;
game = new Game();
startGame();
glutPostRedisplay();
}
//----------------------------------------------------------------------
void SceneWindow::mainLoop()
{
glutMainLoop();
}
//----------------------------------------------------------------------
void SceneWindow::startGame()
{
mario = new Mario();
pause_ = false;
coin = new Coin();
sw->loadLevel();
glutTimerFunc(10, &SceneWindow::timerFunc, 0);
}
//----------------------------------------------------------------------
void SceneWindow::loadLevel()
{
//loads in current level
Level *level_ = Level::sharedLevel();
level_->makeLevel(game->getLevel());
// initialize orthographic viewing projections
//Reset Viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WINDOWWIDTH, 0, WINDOWHEIGHT/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Set Marios Starting Position
mario->setLeft(16);
mario->setRight(16+16);
mario->setBottom(32);
mario->setLeftBound(0);
if (mario->isDead() || mario->getState() == SMALL_STATE) {
mario->setTop(32+16);
}
else{
mario->setTop(32+24);
}
mario->reset();
}
//----------------------------------------------------------------------
void SceneWindow::displayFunc()
{
sw->displayCB();
}
//----------------------------------------------------------------------
void SceneWindow::keyboardFunc(unsigned char key, int x, int y)
{
sw->keyboardCB(key, x, y);
}
//----------------------------------------------------------------------
void SceneWindow::keyboardUpFunc(unsigned char key, int x, int y)
{
sw->keyboardUpCB(key, x, y);
}
//----------------------------------------------------------------------
void SceneWindow::timerFunc(int value)
{
sw->timerCB(value);
}
//----------------------------------------------------------------------
void SceneWindow::displayCB()
{
// clear window
glClear(GL_COLOR_BUFFER_BIT);
// gets lists of the active objects to draw them
Level *level_ = Level::sharedLevel();
LList movable = level_->getActiveMovable();
LList drawable = level_->getActiveDrawable();
LList blocks = level_->getActiveBlocks();
LListIterator li;
if (start_==true){
glClearColor(0.4196, 0.549, 1.0, 1.0);
Drawable *item;
//Loops through all active drawable objects
li.init(drawable);
while ((item = li.next())) {
item->draw();
}
//Loops through all active block objects
li.init(blocks);
while ((item = li.next())) {
item->draw();
}
//Loops through all active moveable objects
li.init(movable);
while ((item = li.next())) {
item->draw();
}
// mario isn't in any of the lists, so must be drawn seperately
mario->draw();
}
//Game is not started determine why and draw correct screen
else {
//Mario is out of lives
//Draw Game over Screen
if (game->getLives()<0) {
string name = "GAME OVER";
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 95, 110);
for (int i=0; i<name.length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, name[i]);
}
glutSwapBuffers();
glutPostRedisplay();
//Return to the start screen in 3 sec
time_t start_time, cur_time;
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 3);
game = new Game;
loadLevel();
}
//User has not yet started a level
//Draw Load Screen
else{
string press = "Press 'S' to start";
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 95, 110);
for (int i=0; i<press.length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, press[i]);
}
string name = "Mario x";
glRasterPos2f(viewportLeftX_ +95, 100);
for (int i=0; i<name.length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, name[i]);
}
//draw lives
std::stringstream lives;
lives << game->getLives();
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 130, 100);
for (int i=0; i<(lives.str()).length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, lives.str()[i]);
}
}
}
//draw top line of game information
string name = "MARIO WORLD TIME";
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 20, 210);
for (int i=0; i<name.length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, name[i]);
}
//draw point total
std::stringstream points;
points << game->getPoints();
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 20, 200);
for (int i=0; i<(points.str()).length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, points.str()[i]);
}
//draw number of coins
coin->setTop(208);
coin->setBottom(198);
coin->setLeft(viewportLeftX_+70);
coin->setRight(viewportLeftX_ + 82);
coin->draw();
std::stringstream coins;
coins << game->getCoins();
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 82, 200);
for (int i=0; i<(coins.str()).length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, coins.str()[i]);
}
//draw level number
std::stringstream level;
level << game->getLevel();
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 145, 200);
for (int i=0; i<(level.str()).length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, level.str()[i]);
}
//draw time
std::stringstream lives;
lives << game->getTime();
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 201, 200);
for (int i=0; i<(lives.str()).length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, lives.str()[i]);
}
//Game is Paused
//Overlay Pause on active game
if (pause_ && !mario->isDead()) {
string pause = "PAUSE";
glColor3f(255,255,255);
glRasterPos2f(viewportLeftX_ + 110, 110);
for (int i=0; i<pause.length(); ++i){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, pause[i]);
}
}
// force screen update
glFlush();
glutSwapBuffers();
}
//----------------------------------------------------------------------
void SceneWindow::keyboardCB(unsigned char key, int x, int y)
{
if (key == 's'){
start_ = true;
}
// quit = escape(ASCII code 27)
if (key == 27) {
exit(0);
}
if (start_) {
// pause = p, hit again to unpause
if (key == 'p') {
if (pause_==false){
pause_=true;
}
else if (pause_==true){
pause_=false;
timerFunc(0);
}
}
else if (key == 'r') {
// reset level
viewportLeftX_ = 0;
viewportRightX_ = viewportLeftX_ + viewportWidth_;
sw->loadLevel();
}
//call update mario to move
mario->updateKeyDown(key);
}
}
//----------------------------------------------------------------------
void SceneWindow::keyboardUpCB(unsigned char key, int x, int y)
{
mario->updateKeyUp(key);
//call update mario to move
}
//----------------------------------------------------------------------
void SceneWindow::timerCB(int value)
{
// look at where mario is
// update extents in level class
// iterate through objects and move them
// redraw
//Mario is at the end of a level
//Load next level and reset level independant variables
if (mario->levelDone()) {
pause_ = false;
viewportLeftX_ = 0;
viewportRightX_ = viewportLeftX_ + viewportWidth_;
start_=false;
sw->loadLevel();
glClearColor(0, 0, 0, 0);
game->resetClock();
}
//Game is active
else if (!mario->isDead() && start_){
//Pulse Clock and update active lists
game->pulseClock();
Level *level_ = Level:: sharedLevel();
LList movable = level_->getActiveMovable();
LListIterator li;
li.init(movable);
Drawable *item;
Movable *movableItem;
while ((item = li.next())) {
movableItem = (Movable*)item;
movableItem->updateScene();
}
mario ->updateScene();
// check if screen needs to be moved
int viewportMid_;
viewportMid_ = (viewportLeftX_ + viewportRightX_)/2;
if (mario->right() > viewportMid_)
{
viewportRightX_ = mario->right() + viewportWidth_/2;
viewportLeftX_ = mario->right() - viewportWidth_/2;
level_->updateExtents(viewportLeftX_, viewportRightX_);
mario->setLeftBound(viewportLeftX_);
}
}
//Mario is dead
//Update his position for death animation
else if (start_ ){
//Set point of death for reference
if (!pause_) {
pause_ = true;
deadups_ = true;
deathPosY_ = mario->bottom();
mario->setLeft(mario->left());
mario->setRight(mario->left()+16.0);
mario->setBottom(mario->bottom());
mario->setTop(mario->bottom()+16.0);
}
//Mario is in the death animation and still on screen
else if (mario->top() > 0){
//Mario has reached the peak of the animation
//Move his position down
if (deadups_ == false) {
mario->setBottom(mario->bottom()-2);
mario->setTop(mario->top()-2);
}
//Mario starting the death animation
//Move his position up
else{
mario->setBottom(mario->bottom()+1);
mario->setTop(mario->top()+1);
if (mario->bottom() > deathPosY_+48.0) {
deadups_ = false;
}
}
}
//Mario is dead and the death animation is over
//Reset level
else{
pause_ = false;
viewportLeftX_ = 0;
viewportRightX_ = viewportLeftX_ + viewportWidth_;
game->subLife();
start_=false;
sw->loadLevel();
glClearColor(0, 0, 0, 0);
game->resetClock();
}
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(viewportLeftX_, viewportRightX_, 0, WINDOWHEIGHT/2);
glutPostRedisplay();
if ((pause_==false) || (pause_ && mario->isDead())) {
glutTimerFunc(10, &SceneWindow::timerFunc, 0);
}
}
//----------------------------------------------------------------------