Creating the game loop

This commit is contained in:
Zoe Roux
2021-05-17 16:26:21 +02:00
parent f9363c7aef
commit 7313a9d4e0
9 changed files with 95 additions and 13 deletions
+42 -2
View File
@@ -5,8 +5,11 @@
#include <chrono>
#include "Wal.hpp"
using namespace std::chrono_literals;
namespace WAL
{
std::chrono::nanoseconds WAL::_timestep = 8ms;
SceneManager &WAL::getSceneManger()
{
@@ -16,12 +19,49 @@ namespace WAL
void WAL::run()
{
auto lastTick = std::chrono::steady_clock::now();
std::chrono::nanoseconds dtime(0);
while (!this->_shouldClose) {
auto now = std::chrono::steady_clock::now();
auto dtime = now - lastTick;
dtime += now - lastTick;
lastTick = now;
// see https://gist.github.com/mariobadr/673bbd5545242fcf9482
this->_update(dtime);
while (dtime > WAL::_timestep) {
dtime -= WAL::_timestep;
this->_fixedUpdate();
}
this->_renderer->render();
}
}
void WAL::_update(std::chrono::nanoseconds dtime)
{
auto &entities = this->_scenes.getCurrent().getEntities();
for (auto &system : this->_systems) {
for (auto &entity : entities) {
auto &cmp = system->getComponent();
if (!entity.hasComponent(cmp))
continue;
// TODO handle dependencies.
system->onUpdate(entity, dtime);
}
}
}
void WAL::_fixedUpdate()
{
auto &entities = this->_scenes.getCurrent().getEntities();
for (auto &system : this->_systems) {
for (auto &entity : entities) {
auto &cmp = system->getComponent();
if (!entity.hasComponent(cmp))
continue;
// TODO handle dependencies.
system->onFixedUpdate(entity);
}
}
}
}