From 52693d68f7181d707e1a192d67a617145b358394 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 28 Jul 2018 22:10:27 +0800 Subject: *update --- src/libjin/Core/Game.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/libjin/Core/Game.cpp') 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 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) -- cgit v1.1-26-g67d0