diff options
Diffstat (limited to 'src/libjin/Core')
-rw-r--r-- | src/libjin/Core/Game.cpp | 26 | ||||
-rw-r--r-- | src/libjin/Core/Game.h | 8 |
2 files changed, 26 insertions, 8 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) diff --git a/src/libjin/Core/Game.h b/src/libjin/Core/Game.h index e9d0340..31825ba 100644 --- a/src/libjin/Core/Game.h +++ b/src/libjin/Core/Game.h @@ -17,7 +17,7 @@ namespace core public: typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(float dt); + typedef void(*onUpdate)(); typedef void(*onDraw)(); struct Setting : SettingBase @@ -29,7 +29,7 @@ namespace core void run(); inline void stop() { _running = false; }; - bool running() { return _running; }; + inline bool running() { return _running; }; private: @@ -44,8 +44,8 @@ namespace core bool _running; - onlyonce bool initSystem(const SettingBase* setting); - onlyonce void quitSystem(); + bool initSystem(const SettingBase* setting); + void quitSystem(); }; |