-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
432 lines (334 loc) · 10.6 KB
/
Main.cpp
File metadata and controls
432 lines (334 loc) · 10.6 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "Framework.h"
#include "Shader.h"
#include "Model.h"
#include "Camera.h"
#include "InputListener.h"
#include "Framebuffer.h"
#include "MotionBlur.h"
#include "Ship.h"
#include "BodyEmitter.h"
#include "HUD.h"
#include "ParticleEngine.h"
#include "StatusBar.h"
#include "Gate.h"
#include "LevelManager.h"
#include "Level.h"
#include "StatusText.h"
#include <btBulletDynamicsCommon.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "MusicManager.h"
#include "SpaceBat.h"
#define TIMESTEP (1.0 / 60.0)
using namespace std;
// Note: See the SMFL documentation for info on setting up fullscreen mode
// and using rendering settings
// http://www.sfml-dev.org/tutorials/1.6/window-window.php
sf::WindowSettings settings(24, 8, 2);
sf::RenderWindow window(sf::VideoMode(1000, 650), "sPaCEbaTS", sf::Style::Close, settings);
// This is a clock you can use to control animation. For more info, see:
// http://www.sfml-dev.org/tutorials/1.6/window-time.php
sf::Clock clck;
GLfloat accum = 0.0;
GLfloat timeElapsed = 0.0;
// This creates an asset importer using the Open Asset Import library.
// It automatically manages resources for you, and frees them when the program
// exits.
Assimp::Importer importer;
Shader *blurShader;
sf::Image background, splash;
ParticleEngine pEngine;
Camera camera;
BodyEmitter *bodyEmitter;
btDiscreteDynamicsWorld *world;
vector<InputListener*> inputListeners;
Framebuffer *normalsBuffer = NULL;
const int NUM_MOTION_BLUR_FRAMES = 4;
MotionBlur* motionBlur;
bool useMotionBlur = false;
bool isStarted = false;
HUD* hud;
StatusBar* boostbar;
StatusBar* healthbar;
StatusText *statusText;
MusicManager music;
bool menuPlaying = false;
bool atmosplaying = false;
#define NUM_LEVELS 9
LevelManager levels(NUM_LEVELS);
Ship spaceship(btVector3(0.0, 0.0, 0.0), &camera, &pEngine, &levels, &music);
void initOpenGL();
void loadAssets();
void handleInput();
void renderFrame();
void renderSplash();
btVector3 flame(0.0,0.0,0.0);
int main(int argc, char** argv) {
srand(time(0));
initOpenGL();
btBroadphaseInterface *broadphase = new btDbvtBroadphase();
btDefaultCollisionConfiguration *collisionConfig = new btDefaultCollisionConfiguration();
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfig);
btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
world->setGravity(btVector3(0, 0, 0));
spaceship.setWorld(world);
statusText = new StatusText(&window, btVector4(-1.0, -0.73, 0.5, 0.25));
loadAssets();
hud = new HUD(&spaceship);
boostbar = new StatusBar(btVector4(0.85, -0.95, 0.05, 1.0));
healthbar = new StatusBar(btVector4(0.92, -0.95, 0.05, 1.0));
healthbar->setTopColor(btVector4(0, 1, 0, 0.5));
healthbar->setBottomColor(btVector4(1, 0, 0, 0.5));
hud->addComponent(boostbar);
hud->addComponent(healthbar);
hud->addComponent(statusText);
Gate::loadChangeImage();
spaceship.setBoostBar(boostbar);
spaceship.setHealthBar(healthbar);
spaceship.setStatusText(statusText);
motionBlur = new MotionBlur(NUM_MOTION_BLUR_FRAMES, window.GetWidth(), window.GetHeight());
glClear(GL_ACCUM_BUFFER_BIT);
inputListeners.push_back(&camera);
inputListeners.push_back(&spaceship);
inputListeners.push_back(&levels);
pEngine.setWindow(window.GetWidth());
pEngine.addEmitter(&spaceship.pos, FIRE, false);
pEngine.addEmitter(&spaceship.pos, SMOKE, false);
pEngine.addEmitter(&spaceship.pos, PLASMA, true);
music.playSound(ATMOSPHERE);
int counter = 0;
// Put your game loop here (i.e., render with OpenGL, update animation)
while (window.IsOpened()) {
handleInput();
if(levels.shouldShowSplashScreen()){
if(atmosplaying){
music.stopSound(ATMOSPHERE);
atmosplaying = false;
}
if(!menuPlaying){
music.playSound(BACKGROUND);
menuPlaying = true;
}
bodyEmitter->clear();
levels.renderSplash();
window.Display();
continue;
}
//music.playSound(ATMOSPHERE);
//music.loopSound(ATMOSPHERE);
float elapsed = clck.GetElapsedTime();
accum += elapsed;
timeElapsed += elapsed;
counter++;
if(healthbar->getValue() <= 0 ){
levels.setGameOver(true);
levels.setWon(false);
printf("Game over\n");
continue;
}
clck.Reset();
while (accum > TIMESTEP) {
spaceship.update(TIMESTEP);
world->stepSimulation(TIMESTEP);
spaceship.testCollision();
pEngine.updateEmitters(TIMESTEP, useMotionBlur);
if(levels.current()->shouldEmitLandmark(timeElapsed)){
BodyType landmarkType = levels.current()->firstLandmark();
bodyEmitter->emit(landmarkType, &pEngine);
}
bodyEmitter->emitBodies(TIMESTEP, &pEngine, levels.current());
camera.update(TIMESTEP);
accum -= TIMESTEP;
}
renderFrame();
window.Display();
}
delete blurShader;
delete normalsBuffer;
delete world;
delete solver;
delete dispatcher;
delete collisionConfig;
delete broadphase;
delete motionBlur;
delete boostbar;
delete healthbar;
delete hud;
return 0;
}
void initOpenGL() {
// Initialize GLEW on Windows, to make sure that OpenGL 2.0 is loaded
#ifdef FRAMEWORK_USE_GLEW
GLint error = glewInit();
if (GLEW_OK != error) {
std::cerr << glewGetErrorString(error) << std::endl;
exit(-1);
}
if (!GLEW_VERSION_2_0 || !GL_EXT_framebuffer_object) {
std::cerr << "This program requires OpenGL 2.0 and FBOs" << std::endl;
exit(-1);
}
#endif
// This initializes OpenGL with some common defaults. More info here:
// http://www.sfml-dev.org/tutorials/1.6/window-opengl.php
glClearDepth(1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glViewport(0, 0, window.GetWidth(), window.GetHeight());
}
void loadAssets() {
blurShader = new Shader("shaders/blur");
StatusBar::loadShader();
Level::loadShaders();
SpaceBat::loadImages();
bodyEmitter = new BodyEmitter(world);
bodyEmitter->loadModels();
levels.setBodyEmitter(bodyEmitter);
Model::loadShaders();
normalsBuffer = new Framebuffer(window.GetWidth(), window.GetHeight());
Model::setNormalsBuffer(normalsBuffer);
background.LoadFromFile("models/space2.jpg");
splash.LoadFromFile("models/TitleScreen.jpg");
spaceship.model.loadFromFile("models/ship", "space_frigate_0.3DS", importer);
spaceship.model.setScaleFactor(0.9);
statusText->loadFont("fonts/Spaceship Bullet.ttf");
}
void handleInput() {
//////////////////////////////////////////////////////////////////////////
// TODO: ADD YOUR INPUT HANDLING HERE.
//////////////////////////////////////////////////////////////////////////
// Event loop, for processing user input, etc. For more info, see:
// http://www.sfml-dev.org/tutorials/1.6/window-events.php
sf::Event evt;
while (window.GetEvent(evt)) {
switch (evt.Type) {
case sf::Event::Closed:
// Close the window. This will cause the game loop to exit,
// because the IsOpened() function will no longer return true.
window.Close();
break;
case sf::Event::Resized:
// If the window is resized, then we need to change the perspective
// transformation and viewport
glViewport(0, 0, evt.Size.Width, evt.Size.Height);
break;
case sf::Event::KeyPressed:
switch (evt.Key.Code) {
case sf::Key::Escape:
window.Close();
break;
case sf::Key::T:
cout << "FORMERTIME = " << timeElapsed << endl;
timeElapsed = levels.getLevel(levels.currentLevel)->endTime;
cout << "TIME = " << timeElapsed << endl;
break;
case sf::Key::Space:
if (levels.shouldShowSplashScreen() && !levels.gameOver()) {
levels.setSplash(false);
if(menuPlaying && levels.currentLevel !=0){
music.stopSound(BACKGROUND);
menuPlaying = false;
}
if(!atmosplaying){
music.playSound(ATMOSPHERE);
music.loopSound(ATMOSPHERE);
atmosplaying = true;
}
if(levels.currentLevel == 0){
levels.nextLevel();
}
clck.Reset();
return;
}
if(boostbar->getValue() <= 0) return;
if (useMotionBlur) return;
music.playSound(POWERUP);
useMotionBlur = true;
bodyEmitter->setBoostMode(true);
bodyEmitter->boostSpeed();
SpaceBat::setEatable(true);
break;
default:
break;
}
break;
case sf::Event::KeyReleased:
switch (evt.Key.Code) {
case sf::Key::Space:
music.stopSound(POWERUP);
useMotionBlur = false;
bodyEmitter->setBoostMode(false);
bodyEmitter->resetSpeed();
SpaceBat::setEatable(false);
break;
default:
break;
}
break;
default:
break;
}
for (unsigned i = 0; i < inputListeners.size(); i++)
inputListeners[i]->handleEvent(evt, window.GetInput());
}
}
void setupLights()
{
GLfloat pos[] = { 1.0, 4.0, 0.0, 0.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient[] = { 0.3, 0.3, 0.3, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
}
void clearNormalsBuffer()
{
normalsBuffer->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
normalsBuffer->unbind();
}
void renderFrame() {
glViewport(0, 0, window.GetWidth(), window.GetHeight());
clearNormalsBuffer();
camera.setProjectionAndView((float)window.GetWidth()/window.GetHeight());
spaceship.model.render(NORMALS_PASS);
bodyEmitter->drawBodies(NORMALS_PASS);
if (motionBlur->shouldRenderFrame()) {
motionBlur->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(levels.current()->hasBG())
levels.current()->renderBackground();
glClear(GL_DEPTH_BUFFER_BIT);
camera.setProjectionAndView((float)window.GetWidth() / window.GetHeight());
setupLights();
bodyEmitter->drawBodies(FINAL_PASS);
motionBlur->unbind();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(useMotionBlur && boostbar->getValue() <= 0){
useMotionBlur = false;
bodyEmitter->setBoostMode(false);
bodyEmitter->resetSpeed();
}
if(useMotionBlur){
motionBlur->render(blurShader);
} else {
if(levels.current()->hasBG())
levels.current()->renderBackground();
}
motionBlur->update();
glClear(GL_DEPTH_BUFFER_BIT);
camera.setProjectionAndView((float)window.GetWidth()/window.GetHeight());
setupLights();
bodyEmitter->drawBodies(FINAL_PASS);
glSecondaryColor3f(0.0,0.0,0.0);
spaceship.model.render(FINAL_PASS);
camera.setProjectionAndView((float)window.GetWidth()/window.GetHeight());
pEngine.renderEmitters(useMotionBlur);
hud->render();
}