-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatform.h
89 lines (68 loc) · 1.76 KB
/
Platform.h
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
#ifndef PLATFORM_H
#define PLATFORM_H
#include <vector>
using namespace std;
#include <GL/glut.h>
#include "./PlatformObject.h"
#include "./Renderer.h"
#include "./constants.h"
#include "./Clock.h"
class Platform {
public:
Platform(double dt = 1.0/60.0);
~Platform();
void addObject(PlatformObject* object);
void removeObject(PlatformObject* object);
void setContactListener(q3ContactListener* listener);
void display();
private:
double dt;
Renderer renderer;
vector<PlatformObject*> objects;
q3Scene scene;
Clock clock;
};
Platform::Platform(double _dt): dt(_dt), scene(dt) {
scene.SetIterations( 10 );
scene.SetAllowSleep( true );
scene.RemoveAllBodies( );
}
Platform::~Platform() {
for (auto object: objects) {
delete object;
}
}
void Platform::setContactListener(q3ContactListener* listener) {
scene.SetContactListener(listener);
}
void Platform::addObject(PlatformObject* object) {
object->genBody(scene);
objects.push_back(object);
}
void Platform::removeObject(PlatformObject* object) {
for (int i = 0; i < objects.size(); ++i) {
if (objects[i] == object) {
scene.RemoveBody(objects[i]->body);
objects.erase(objects.begin()+i);
return;
}
}
}
void Platform::display() {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
static float accumulator = 0;
accumulator += clock.Start( ) * 0.5f;
while (accumulator >= dt) {
scene.Step( );
accumulator -= dt;
}
clock.Stop( );
for (auto object: objects) {
object->display();
}
if (RENDER_BODIES) {
scene.Render( &renderer );
}
glutSwapBuffers( );
}
#endif