aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Game/Game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Game/Game.cpp')
-rw-r--r--src/libjin/Game/Game.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/libjin/Game/Game.cpp b/src/libjin/Game/Game.cpp
new file mode 100644
index 0000000..3f905f2
--- /dev/null
+++ b/src/libjin/Game/Game.cpp
@@ -0,0 +1,74 @@
+#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()
+ {
+ if (_onLoad != nullptr)
+ _onLoad();
+ 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();
+ int dt = MS_PER_UPDATE;
+ while (_running)
+ {
+ while (jin::input::pollEvent(&e))
+ {
+ if (_onEvent != nullptr)
+ _onEvent(&e);
+ if (!_running) goto quitloop;
+ }
+ if (_onUpdate != nullptr) _onUpdate(dt);
+ if (_onDraw != nullptr) _onDraw();
+ wnd->swapBuffers();
+ const int current = getMilliSecond();
+ dt = current - previous;
+ const int wait = MS_PER_UPDATE - (current - previous);
+ previous += MS_PER_UPDATE;
+ if (wait > 0)
+ {
+ sleep(wait);
+ dt = MS_PER_UPDATE;
+ }
+ else
+ previous = current;
+ }
+ quitloop:;
+ }
+
+ bool Game::initSystem(const SettingBase* setting)
+ {
+ if (setting == nullptr)
+ return false;
+ Game::Setting* s = (Game::Setting*) setting;
+ _onEvent = s->eventHandler;
+ _onUpdate = s->updater;
+ _onDraw = s->drawer;
+ _onLoad = s->loader;
+ return true;
+ }
+
+ void Game::quitSystem()
+ {
+ }
+
+} // core
+} // jin \ No newline at end of file