diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/audio/audio.h | 9 | ||||
-rw-r--r-- | src/libjin/audio/sdl/audio.h | 7 | ||||
-rw-r--r-- | src/libjin/common/singleton.h | 6 | ||||
-rw-r--r-- | src/libjin/common/subsystem.h | 6 | ||||
-rw-r--r-- | src/libjin/core/game.cpp | 2 | ||||
-rw-r--r-- | src/libjin/core/game.h | 17 | ||||
-rw-r--r-- | src/libjin/render/canvas.cpp | 2 | ||||
-rw-r--r-- | src/libjin/render/window.cpp | 10 | ||||
-rw-r--r-- | src/libjin/render/window.h | 9 | ||||
-rw-r--r-- | src/libjin/utils/macros.h | 2 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_graphics.cpp | 8 |
11 files changed, 46 insertions, 32 deletions
diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index ccfc8a1..7b27e1f 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -12,8 +12,7 @@ namespace audio { class Source; - template<class T> - class Audio : public Subsystem<T> + class AudioSystem : public Subsystem<AudioSystem> { public: @@ -30,8 +29,10 @@ namespace audio protected: - Audio() {}; - virtual ~Audio() {}; + AudioSystem() {}; + virtual ~AudioSystem() {}; + + SINGLETON(AudioSystem); }; diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h index 953a853..861acb3 100644 --- a/src/libjin/audio/sdl/audio.h +++ b/src/libjin/audio/sdl/audio.h @@ -13,7 +13,7 @@ namespace audio #define SDLAUDIO_BYTEDEPTH (SDLAUDIO_BITDEPTH >> 3) #define SDLAUDIO_CHANNELS 2 - class SDLAudio : public Audio<SDLAudio> + class SDLAudio : public AudioSystem { public: @@ -45,6 +45,11 @@ namespace audio private: + SDLAudio() {}; + ~SDLAudio() {}; + + SINGLETON(SDLAudio); + onlyonce bool initSystem(const SettingBase* setting) override; onlyonce void quitSystem() override; diff --git a/src/libjin/common/singleton.h b/src/libjin/common/singleton.h index e6326b2..a6c7d6d 100644 --- a/src/libjin/common/singleton.h +++ b/src/libjin/common/singleton.h @@ -21,7 +21,7 @@ namespace jin protected: Singleton() {}; virtual ~Singleton() {}; - private: + private: Singleton(const Singleton&); Singleton& operator = (const Singleton&); @@ -29,6 +29,10 @@ namespace jin }; template<class T> T* Singleton<T>::_instance = nullptr; + +#define SINGLETON(T) \ + friend Singleton<T> + } #endif
\ No newline at end of file diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h index 55b0f0d..90e8570 100644 --- a/src/libjin/common/subsystem.h +++ b/src/libjin/common/subsystem.h @@ -16,7 +16,7 @@ namespace jin void init(const SettingBase* setting) { - CallOnce(initSystem(setting)); + CALLONCE(initSystem(setting)); } void quit() @@ -26,11 +26,15 @@ namespace jin } protected: + Subsystem() {}; virtual ~Subsystem() {}; + SINGLETON(System); + virtual bool initSystem(const Setting* setting) = 0; virtual void quitSystem() = 0; + }; } diff --git a/src/libjin/core/game.cpp b/src/libjin/core/game.cpp index 1c47d87..5c2e69b 100644 --- a/src/libjin/core/game.cpp +++ b/src/libjin/core/game.cpp @@ -5,8 +5,6 @@ namespace jin namespace core { - Game* Game::g_game = 0; - Game::Game() :run(true) {}; } diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index cd99b42..e155607 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -3,13 +3,14 @@ #include <SDL2/SDL.h> +#include "../common/singleton.h" #include "../utils/macros.h" namespace jin { namespace core { - class Game + class Game : public Singleton<Game> { public: @@ -18,14 +19,9 @@ namespace core }; - static inline Game* get() - { - return g_game ? g_game : (g_game = new Game()); - } - inline void quit() // quit game loop { - CallOnce(_quit()); + CALLONCE(_quit()); } inline bool running() @@ -35,14 +31,15 @@ namespace core inline void exit() // exit game { - CallOnce(_exit()); + CALLONCE(_exit()); } private: Game(); - - static Game* g_game; + ~Game() {}; + + SINGLETON(Game); bool run; diff --git a/src/libjin/render/canvas.cpp b/src/libjin/render/canvas.cpp index fb84434..55a141b 100644 --- a/src/libjin/render/canvas.cpp +++ b/src/libjin/render/canvas.cpp @@ -106,7 +106,7 @@ namespace render cur = 0; glBindFramebuffer(GL_FRAMEBUFFER, 0); - Window* wnd = Window::get(); + WindowSystem* wnd = WindowSystem::get(); int ww = wnd->getW(), wh = wnd->getH(); diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 677618d..3357f07 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -12,7 +12,7 @@ namespace jin namespace render { - onlyonce bool Window::initSystem(const SettingBase* s) + onlyonce bool WindowSystem::initSystem(const SettingBase* s) { Loghelper::log(Loglevel::LV_INFO, "Init window system"); @@ -69,22 +69,22 @@ namespace render return true; } - onlyonce void Window::quitSystem() + onlyonce void WindowSystem::quitSystem() { SDL_DestroyWindow(wnd); } - SDL_Window* Window::getWnd() + SDL_Window* WindowSystem::getWnd() { return wnd; } - SDL_GLContext Window::getCtx() + SDL_GLContext WindowSystem::getCtx() { return ctx; } - inline void Window::swapBuffers() + inline void WindowSystem::swapBuffers() { if (wnd) SDL_GL_SwapWindow(wnd); diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h index 0c2c79d..c7e2651 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -9,7 +9,7 @@ namespace jin namespace render { - class Window : public Subsystem<Window> + class WindowSystem : public Subsystem<WindowSystem> { public: @@ -39,6 +39,11 @@ namespace render private: + WindowSystem() {}; + virtual ~WindowSystem() {}; + + SINGLETON(WindowSystem); + SDL_Window* wnd; SDL_GLContext ctx; @@ -49,7 +54,7 @@ namespace render onlyonce void quitSystem() override; }; - typedef Window::Setting WindowSetting; + typedef WindowSystem::Setting WindowSetting; } } diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h index adfc64c..684e8e8 100644 --- a/src/libjin/utils/macros.h +++ b/src/libjin/utils/macros.h @@ -8,7 +8,7 @@ #define MASK // enum -#define CallOnce(call) static char __dummy__=(call, 1) // ֻһ +#define CALLONCE(call) static char __dummy__=(call, 1) // ֻһ #define onlyonce // ֻһ #define zero(mem) memset(&mem, 0, sizeof(mem)) diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 025781e..44b0a2c 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -27,7 +27,7 @@ namespace lua */ static int l_init(lua_State* L) { - Window* wnd = Window::get(); + WindowSystem* wnd = WindowSystem::get(); WindowSetting setting; setting.width = luax_getfield_integer(L, 1, "width"); setting.height = luax_getfield_integer(L, 1, "height"); @@ -40,7 +40,7 @@ namespace lua static int l_destroy(lua_State* L) { - Window* wnd = Window::get(); + WindowSystem* wnd = WindowSystem::get(); wnd->quit(); return 0; } @@ -50,7 +50,7 @@ namespace lua */ static int l_getSize(lua_State* L) { - Window* wnd = Window::get(); + WindowSystem* wnd = WindowSystem::get(); luax_pushnumber(L, wnd->getW()); luax_pushnumber(L, wnd->getH()); return 2; @@ -127,7 +127,7 @@ namespace lua */ static int l_present(lua_State* L) { - Window::get()->swapBuffers(); + WindowSystem::get()->swapBuffers(); return 0; } |