mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 02:23:44 +00:00
Creating the game loop
This commit is contained in:
+42
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user