From f889c9c20fc09f26eb8a70674c1d60181835c38a Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 25 Oct 2018 00:50:35 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Game/je_application.cpp | 79 ++++++++++++++++++++++++++++++++++ src/libjin/Game/je_application.h | 88 ++++++++++++++++++++++++++++++++++++++ src/libjin/Game/je_game.cpp | 79 ---------------------------------- src/libjin/Game/je_game.h | 88 -------------------------------------- 4 files changed, 167 insertions(+), 167 deletions(-) create mode 100644 src/libjin/Game/je_application.cpp create mode 100644 src/libjin/Game/je_application.h delete mode 100644 src/libjin/Game/je_game.cpp delete mode 100644 src/libjin/Game/je_game.h (limited to 'src/libjin/Game') diff --git a/src/libjin/Game/je_application.cpp b/src/libjin/Game/je_application.cpp new file mode 100644 index 0000000..a5a8812 --- /dev/null +++ b/src/libjin/Game/je_application.cpp @@ -0,0 +1,79 @@ +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include + +#include "../time/je_timer.h" +#include "../input/je_event.h" +#include "../graphics/je_window.h" +#include "../math/je_math.h" + +#include "je_application.h" + +namespace JinEngine +{ + namespace Game + { + + using namespace JinEngine::Graphics; + using namespace JinEngine::Input; + using namespace JinEngine::Time; + using namespace JinEngine::Math; + + Application::Application() :_running(true) {}; + + /* default game loop */ + void Application::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 (JinEngine::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 Application::initSystem(const SettingBase* setting) + { + if (setting == nullptr) + return false; + Application::Setting* s = (Application::Setting*) setting; + _onEvent = s->eventHandler; + _onUpdate = s->updater; + _onDraw = s->drawer; + _onLoad = s->loader; + return true; + } + + void Application::quitSystem() + { + } + + } // namespace Core +} // namespace JinEngine + +#endif // jin_game \ No newline at end of file diff --git a/src/libjin/Game/je_application.h b/src/libjin/Game/je_application.h new file mode 100644 index 0000000..f740c22 --- /dev/null +++ b/src/libjin/Game/je_application.h @@ -0,0 +1,88 @@ +#ifndef __JE_CORE_GAME_H +#define __JE_CORE_GAME_H + +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include "../common/je_subsystem.hpp" +#include "../utils/je_macros.h" +#include "../input/je_Event.h" + +#include "SDL2/SDL.h" + +namespace JinEngine +{ + namespace Game + { + + /// + /// Game class. + /// + class Application : public Subsystem + { + public: + + typedef void(*onLoad)(); + typedef void(*onEvent)(JinEngine::Input::Event* e); + typedef void(*onUpdate)(int dt); + typedef void(*onDraw)(); + + /// + /// Game setting. + /// + struct Setting : SettingBase + { + onEvent eventHandler; + onUpdate updater; + onDraw drawer; + onLoad loader; + }; + + /// + /// Main game loop. + /// + void run(); + + /// + /// Stop game. + /// + inline void stop() + { + _running = false; + }; + + /// + /// Return if game is running. + /// + /// @return True if game is running, otherwise return false. + /// + inline bool running() + { + return _running; + }; + + private: + + Application(); + ~Application() {}; + + singleton(Application); + + onEvent _onEvent; + onUpdate _onUpdate; + onDraw _onDraw; + onLoad _onLoad; + + bool _running; + + bool initSystem(const SettingBase* setting); + void quitSystem(); + + }; + + } // namespace Core +} // namespace JinEngine + +#endif // jin_game + +#endif // __JE_CORE_GAME_H \ No newline at end of file diff --git a/src/libjin/Game/je_game.cpp b/src/libjin/Game/je_game.cpp deleted file mode 100644 index 7562831..0000000 --- a/src/libjin/Game/je_game.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "../core/je_configuration.h" -#if defined(jin_game) - -#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 JinEngine -{ - namespace Game - { - - using namespace JinEngine::Graphics; - using namespace JinEngine::Input; - using namespace JinEngine::Time; - using namespace JinEngine::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 (JinEngine::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 JinEngine - -#endif // jin_game \ No newline at end of file diff --git a/src/libjin/Game/je_game.h b/src/libjin/Game/je_game.h deleted file mode 100644 index 2ccc428..0000000 --- a/src/libjin/Game/je_game.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef __JE_CORE_GAME_H -#define __JE_CORE_GAME_H - -#include "../core/je_configuration.h" -#if defined(jin_game) - -#include "../common/je_subsystem.hpp" -#include "../utils/je_macros.h" -#include "../input/je_Event.h" - -#include "SDL2/SDL.h" - -namespace JinEngine -{ - namespace Game - { - - /// - /// Game class. - /// - class Game : public Subsystem - { - public: - - typedef void(*onLoad)(); - typedef void(*onEvent)(JinEngine::Input::Event* e); - typedef void(*onUpdate)(int dt); - typedef void(*onDraw)(); - - /// - /// Game setting. - /// - struct Setting : SettingBase - { - onEvent eventHandler; - onUpdate updater; - onDraw drawer; - onLoad loader; - }; - - /// - /// Main game loop. - /// - void run(); - - /// - /// Stop game. - /// - inline void stop() - { - _running = false; - }; - - /// - /// Return if game is running. - /// - /// @return True if game is running, otherwise return 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 JinEngine - -#endif // jin_game - -#endif // __JE_CORE_GAME_H \ No newline at end of file -- cgit v1.1-26-g67d0