diff options
Diffstat (limited to 'src/libjin/Core/Game.cpp')
-rw-r--r-- | src/libjin/Core/Game.cpp | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/libjin/Core/Game.cpp b/src/libjin/Core/Game.cpp index f2223b2..4509478 100644 --- a/src/libjin/Core/Game.cpp +++ b/src/libjin/Core/Game.cpp @@ -1,30 +1,48 @@ #include "game.h" #include "../Time/Timer.h" #include "../input/Event.h" +#include "../Graphics/Window.h" +#include "../Math/Math.h" +#include <iostream> namespace jin { namespace core { + using namespace jin::graphics; using namespace jin::input; + using namespace jin::time; + using namespace jin::math; Game::Game() :_running(true) {}; void Game::run() { + Window* wnd = Window::get(); + const int FPS = wnd ? wnd->getFPS() : 60; + const int MS_PER_UPDATE = 1000.0f / FPS; _running = true; Event e; + int previous = getMilliSecond(); while (_running) { while (jin::input::pollEvent(&e)) { - if (_instance != nullptr && _onEvent) - _onEvent(&e); + SAFECALL(_onEvent, &e); + if (!_running) goto stoploop; } - if (!_running) - break; + SAFECALL(_onUpdate); + SAFECALL(_onDraw); + const int current = getMilliSecond(); + const int wait = MS_PER_UPDATE - (current - previous); + previous += MS_PER_UPDATE; + if (wait > 0) + sleep(wait); + else + previous = current; } + stoploop:; } bool Game::initSystem(const SettingBase* setting) |