#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();
}
}