From 7d5f055547e70fa93ee9ac944e62f8d657b9dc55 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 19 Oct 2018 08:36:44 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Game/Game.cpp | 72 ------------------------------------------- src/libjin/Game/Game.h | 58 ----------------------------------- src/libjin/Game/je_game.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++ src/libjin/Game/je_game.h | 58 +++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 130 deletions(-) delete mode 100644 src/libjin/Game/Game.cpp delete mode 100644 src/libjin/Game/Game.h create mode 100644 src/libjin/Game/je_game.cpp create mode 100644 src/libjin/Game/je_game.h (limited to 'src/libjin/Game') diff --git a/src/libjin/Game/Game.cpp b/src/libjin/Game/Game.cpp deleted file mode 100644 index b04661f..0000000 --- a/src/libjin/Game/Game.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#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) {}; - - /* default game loop */ - 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 current = getMilliSecond(); - int previous = current; - int dt = 0; - while (_running) - { - while (jin::input::pollEvent(&e)) - { - if (_onEvent != nullptr) - _onEvent(&e); - if (!_running) - goto quitloop; - } - previous = current; - current = getMilliSecond(); - dt = current - previous; - if (_onUpdate != nullptr) - _onUpdate(dt); - if (_onDraw != nullptr) - _onDraw(); - wnd->swapBuffers(); - sleep(1); - } - 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() - { - } - - } // namespace core -} // namespace jin \ No newline at end of file diff --git a/src/libjin/Game/Game.h b/src/libjin/Game/Game.h deleted file mode 100644 index c7607a1..0000000 --- a/src/libjin/Game/Game.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef __LIBJIN_CORE_GAME_H -#define __LIBJIN_CORE_GAME_H - -#include "SDL2/SDL.h" - -#include "../Common/Subsystem.hpp" -#include "../utils/macros.h" -#include "../Input/Event.h" - -namespace jin -{ - namespace core - { - - class Game : public Subsystem - { - public: - - typedef void(*onLoad)(); - typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(int dt); - typedef void(*onDraw)(); - - struct Setting : SettingBase - { - onEvent eventHandler; - onUpdate updater; - onDraw drawer; - onLoad loader; - }; - - void run(); - inline void stop() { _running = false; }; - inline bool running() { return _running; }; - - private: - - Game(); - ~Game() {}; - - singleton(Game); - - onEvent _onEvent; - onUpdate _onUpdate; - onDraw _onDraw; - onLoad _onLoad; - - bool _running; - - bool initSystem(const SettingBase* setting); - void quitSystem(); - - }; - - } // namespace core -} // namespace jin - -#endif // __LIBJIN_CORE_GAME_H diff --git a/src/libjin/Game/je_game.cpp b/src/libjin/Game/je_game.cpp new file mode 100644 index 0000000..10e9e44 --- /dev/null +++ b/src/libjin/Game/je_game.cpp @@ -0,0 +1,74 @@ +#include + +#include "../time/je_timer.h" +#include "../input/je_event.h" +#include "../graphics/je_window.h" +#include "../math/je_math.h" + +#include "je_game.h" + +namespace jin +{ + namespace core + { + + using namespace jin::graphics; + using namespace jin::input; + using namespace jin::time; + using namespace jin::math; + + Game::Game() :_running(true) {}; + + /* default game loop */ + 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 current = getMilliSecond(); + int previous = current; + int dt = 0; + while (_running) + { + while (jin::input::pollEvent(&e)) + { + if (_onEvent != nullptr) + _onEvent(&e); + if (!_running) + goto quitloop; + } + previous = current; + current = getMilliSecond(); + dt = current - previous; + if (_onUpdate != nullptr) + _onUpdate(dt); + if (_onDraw != nullptr) + _onDraw(); + wnd->swapBuffers(); + sleep(1); + } + 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() + { + } + + } // namespace core +} // namespace jin \ No newline at end of file diff --git a/src/libjin/Game/je_game.h b/src/libjin/Game/je_game.h new file mode 100644 index 0000000..9323177 --- /dev/null +++ b/src/libjin/Game/je_game.h @@ -0,0 +1,58 @@ +#ifndef __LIBJIN_CORE_GAME_H +#define __LIBJIN_CORE_GAME_H + +#include "../common/je_subsystem.hpp" +#include "../utils/je_macros.h" +#include "../input/je_Event.h" + +#include "SDL2/SDL.h" + +namespace jin +{ + namespace core + { + + class Game : public Subsystem + { + public: + + typedef void(*onLoad)(); + typedef void(*onEvent)(jin::input::Event* e); + typedef void(*onUpdate)(int dt); + typedef void(*onDraw)(); + + struct Setting : SettingBase + { + onEvent eventHandler; + onUpdate updater; + onDraw drawer; + onLoad loader; + }; + + void run(); + inline void stop() { _running = false; }; + inline bool running() { return _running; }; + + private: + + Game(); + ~Game() {}; + + singleton(Game); + + onEvent _onEvent; + onUpdate _onUpdate; + onDraw _onDraw; + onLoad _onLoad; + + bool _running; + + bool initSystem(const SettingBase* setting); + void quitSystem(); + + }; + + } // namespace core +} // namespace jin + +#endif // __LIBJIN_CORE_GAME_H -- cgit v1.1-26-g67d0