aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libjin/Audio/Audio.h10
-rw-r--r--libjin/Audio/SDL/SDLAudio.h6
-rw-r--r--libjin/Core/Game.cpp22
-rw-r--r--libjin/Graphics/Canvas.cpp2
-rw-r--r--libjin/Graphics/Window.cpp13
-rw-r--r--libjin/Graphics/Window.h12
-rw-r--r--libjin/Utils/macros.h3
-rw-r--r--test/01HelloWorld/main.cpp7
8 files changed, 38 insertions, 37 deletions
diff --git a/libjin/Audio/Audio.h b/libjin/Audio/Audio.h
index 60de66d..5b43729 100644
--- a/libjin/Audio/Audio.h
+++ b/libjin/Audio/Audio.h
@@ -14,8 +14,8 @@ namespace audio
{
class Source;
- template<class SubAudioSystem>
- class AudioSystem : public Subsystem<SubAudioSystem>
+ template<class SubAudio>
+ class Audio : public Subsystem<SubAudio>
{
public:
@@ -32,10 +32,10 @@ namespace audio
protected:
- AudioSystem() {};
- virtual ~AudioSystem() {};
+ Audio() {};
+ virtual ~Audio() {};
- SINGLETON(AudioSystem);
+ SINGLETON(Audio);
};
diff --git a/libjin/Audio/SDL/SDLAudio.h b/libjin/Audio/SDL/SDLAudio.h
index bac4544..6837126 100644
--- a/libjin/Audio/SDL/SDLAudio.h
+++ b/libjin/Audio/SDL/SDLAudio.h
@@ -15,7 +15,7 @@ namespace audio
#define SDLAUDIO_BYTEDEPTH (SDLAUDIO_BITDEPTH >> 3)
#define SDLAUDIO_CHANNELS 2
- class SDLAudio : public AudioSystem<SDLAudio>
+ class SDLAudio : public Audio<SDLAudio>
{
public:
@@ -52,8 +52,8 @@ namespace audio
SINGLETON(SDLAudio);
- onlyonce bool initSystem(const SettingBase* setting) override;
- onlyonce void quitSystem() override;
+ bool initSystem(const SettingBase* setting) override;
+ void quitSystem() override;
unsigned int audioDevice;
diff --git a/libjin/Core/Game.cpp b/libjin/Core/Game.cpp
index be5cc00..4296c21 100644
--- a/libjin/Core/Game.cpp
+++ b/libjin/Core/Game.cpp
@@ -19,7 +19,7 @@ namespace core
void Game::run()
{
- WindowSystem* wnd = WindowSystem::get();
+ Window* wnd = Window::get();
const int FPS = wnd ? wnd->getFPS() : 60;
const int MS_PER_UPDATE = 1000.0f / FPS;
_running = true;
@@ -30,20 +30,13 @@ namespace core
{
while (jin::input::pollEvent(&e))
{
- if (_onEvent != nullptr)
- _onEvent(&e);
+ SAFECALL(_onEvent, &e);
+ if (!_running) goto stoploop;
}
- if (!_running)
- break;
-
- if (_onUpdate)
- _onUpdate();
-
- if(_onDraw)
- _onDraw();
-
- int current = getMilliSecond();
- int wait = MS_PER_UPDATE - (current - previous);
+ SAFECALL(_onUpdate);
+ SAFECALL(_onDraw);
+ const int current = getMilliSecond();
+ const int wait = MS_PER_UPDATE - (current - previous);
previous += MS_PER_UPDATE;
if (wait >= 0 && lag == 0)
sleep(wait);
@@ -55,6 +48,7 @@ namespace core
previous = current;
}
}
+ stoploop:;
}
bool Game::initSystem(const SettingBase* setting)
diff --git a/libjin/Graphics/Canvas.cpp b/libjin/Graphics/Canvas.cpp
index dddc889..f5bd09f 100644
--- a/libjin/Graphics/Canvas.cpp
+++ b/libjin/Graphics/Canvas.cpp
@@ -109,7 +109,7 @@ namespace graphics
cur = 0;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
- WindowSystem* wnd = WindowSystem::get();
+ Window* wnd = Window::get();
int ww = wnd->getW(),
wh = wnd->getH();
diff --git a/libjin/Graphics/Window.cpp b/libjin/Graphics/Window.cpp
index 8a7e8a2..cdff426 100644
--- a/libjin/Graphics/Window.cpp
+++ b/libjin/Graphics/Window.cpp
@@ -14,7 +14,7 @@ namespace jin
namespace graphics
{
- bool WindowSystem::initSystem(const SettingBase* s)
+ bool Window::initSystem(const SettingBase* s)
{
Loghelper::log(Loglevel::LV_INFO, "Init window system");
@@ -22,7 +22,6 @@ namespace graphics
return false;
const Setting* setting = (Setting*)s;
-
width = setting->width;
height = setting->height;
fps = setting->fps;
@@ -53,8 +52,12 @@ namespace graphics
int wx = SDL_WINDOWPOS_UNDEFINED,
wy = SDL_WINDOWPOS_UNDEFINED;
+
+ int flag = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;
+ if (setting->fullscreen) flag |= SDL_WINDOW_FULLSCREEN;
+ if (setting->resizable) flag |= SDL_WINDOW_RESIZABLE;
- wnd = SDL_CreateWindow(title, wx, wy, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
+ wnd = SDL_CreateWindow(title, wx, wy, width, height, flag);
if (wnd == NULL)
return false;
ctx = SDL_GL_CreateContext(wnd);
@@ -74,13 +77,13 @@ namespace graphics
return true;
}
- void WindowSystem::quitSystem()
+ void Window::quitSystem()
{
SDL_DestroyWindow(wnd);
SDL_Quit();
}
- inline void WindowSystem::swapBuffers()
+ inline void Window::swapBuffers()
{
if (wnd)
SDL_GL_SwapWindow(wnd);
diff --git a/libjin/Graphics/Window.h b/libjin/Graphics/Window.h
index 66c34a5..e09e9f9 100644
--- a/libjin/Graphics/Window.h
+++ b/libjin/Graphics/Window.h
@@ -12,7 +12,7 @@ namespace jin
namespace graphics
{
- class WindowSystem : public Subsystem<WindowSystem>
+ class Window : public Subsystem<Window>
{
public:
@@ -20,22 +20,24 @@ namespace graphics
{
public:
const char* title; //
+ bool fullscreen; // ȫ
int width, height; // ڴС
bool vsync; // ֱͬ
int fps; // FPS
+ bool resizable; // resize
};
inline int getW(){ return width; }
inline int getH(){ return height; }
- inline int getFPS() { return fps; }
+ inline int getFPS(){ return fps; }
inline void swapBuffers();
private:
- WindowSystem() {};
- virtual ~WindowSystem() {};
+ Window() {};
+ virtual ~Window() {};
- SINGLETON(WindowSystem);
+ SINGLETON(Window);
SDL_Window* wnd;
diff --git a/libjin/Utils/macros.h b/libjin/Utils/macros.h
index 684e8e8..a57cb7c 100644
--- a/libjin/Utils/macros.h
+++ b/libjin/Utils/macros.h
@@ -8,8 +8,9 @@
#define MASK // enum
-#define CALLONCE(call) static char __dummy__=(call, 1) // ֻһ
#define onlyonce // ֻһ
+#define CALLONCE(call) static char __dummy__=(call, 1) // ֻһ
+#define SAFECALL(func, params) if(func) func(params)
#define zero(mem) memset(&mem, 0, sizeof(mem))
diff --git a/test/01HelloWorld/main.cpp b/test/01HelloWorld/main.cpp
index 6d76c8a..d187849 100644
--- a/test/01HelloWorld/main.cpp
+++ b/test/01HelloWorld/main.cpp
@@ -22,7 +22,6 @@ void onDraw()
dt += 16;
if (dt > 1000)
{
- std::cout << "a";
dt = 0;
}
}
@@ -36,13 +35,15 @@ int main(int argc, char* argv[])
setting.drawer = onDraw;
game->init(&setting);
- WindowSystem* wnd = WindowSystem::get();
- WindowSystem::Setting wndSetting;
+ Window* wnd = Window::get();
+ Window::Setting wndSetting;
wndSetting.width = 600;
wndSetting.height = 512;
wndSetting.title = "test";
wndSetting.fps = 60;
wndSetting.vsync = false;
+ wndSetting.fullscreen = false;
+ wndSetting.resizable = true;
wnd->init(&wndSetting);
game->run();