[go: up one dir, main page]

Menu

[r40]: / logic / Logic.cpp  Maximize  Restore  History

Download this file

66 lines (60 with data), 1.4 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
#include "Logic.hpp"
#include "Object.hpp"
#include <thread>
#include <ctime>
const Vector Logic::g(
0,
- 9.8,
0
);
Graphics& Logic::onLoop() {
static const auto sleep = std::chrono::milliseconds(20);
const auto now = std::chrono::high_resolution_clock::now();
const double dt = std::chrono::duration<double>(now - past).count();
if (dt > 0) {
for (
std::vector<Object*>::iterator i = Object::objects.begin();
i < Object::objects.end();
i++
) {
(*i)->simulate(dt);
}
past = now;
}
std::this_thread::sleep_for(sleep);
return Graphics::onLoop();
}
const GraphicsListener& Logic::render(Graphics& graphics) const {
for (
std::vector<Object*>::const_iterator i = Object::objects.begin();
i < Object::objects.end();
i++
) {
if (
(*i)->active
) {
(*i)->render(
graphics.push().apply(
(*i)->matrix
)
);
graphics.pop();
}
}
return *this;
}
Logic::Logic() : Graphics(NAME) {
std::srand(
std::clock() * std::time(NULL)
);
past = std::chrono::high_resolution_clock::now();
}
Logic::~Logic() {
std::vector<Object*>::iterator i = Object::objects.begin();
while (
i < Object::objects.end()
) {
delete *i;
i = Object::objects.begin();
}
}