diff options
author | chai <chaifix@163.com> | 2018-07-28 22:10:27 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-07-28 22:10:27 +0800 |
commit | 52693d68f7181d707e1a192d67a617145b358394 (patch) | |
tree | 5a39bcdf225def306738ead6683cd224e2c5762d /src | |
parent | d033400614e7e2c0ff49e5100c81e937e5818e74 (diff) |
*update
Diffstat (limited to 'src')
28 files changed, 348 insertions, 102 deletions
diff --git a/src/libjin/Audio/Audio.h b/src/libjin/Audio/Audio.h index 60de66d..5b43729 100644 --- a/src/libjin/Audio/Audio.h +++ b/src/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/src/libjin/Audio/SDL/SDLAudio.cpp b/src/libjin/Audio/SDL/SDLAudio.cpp index c154ae4..a41382b 100644 --- a/src/libjin/Audio/SDL/SDLAudio.cpp +++ b/src/libjin/Audio/SDL/SDLAudio.cpp @@ -12,6 +12,8 @@ namespace jin namespace audio { + using namespace jin::math; + /* עcallbackƵ̵߳ */ static void defaultCallback(void *userdata, Uint8 *stream, int size) { @@ -34,7 +36,7 @@ namespace audio return false; unsigned int samplerate = setting->samplerate; - unsigned int samples = clamp(setting->samples, 1, setting->samplerate); + unsigned int samples = clamp<int>(setting->samples, 1, setting->samplerate); spec.freq = samplerate; // ÿsample,õ 11025, 22050, 44100 and 48000 Hz. spec.format = AUDIO_S16SYS; // signed 16-bit samples in native byte order diff --git a/src/libjin/Audio/SDL/SDLAudio.h b/src/libjin/Audio/SDL/SDLAudio.h index bac4544..6837126 100644 --- a/src/libjin/Audio/SDL/SDLAudio.h +++ b/src/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/src/libjin/Audio/SDL/SDLSource.cpp b/src/libjin/Audio/SDL/SDLSource.cpp index b70230d..18ba855 100644 --- a/src/libjin/Audio/SDL/SDLSource.cpp +++ b/src/libjin/Audio/SDL/SDLSource.cpp @@ -18,6 +18,8 @@ namespace jin namespace audio { + using namespace jin::math; + #define BITS 8 typedef struct SDLSourceCommand @@ -134,7 +136,7 @@ namespace audio raw.samplerate = wav.samplerate; raw.bitdepth = wav.bitdepth; raw.samples = raw.length / (wav.bitdepth / 8.f) / wav.channels; - raw.channels = clamp(wav.channels, CHANNEL::MONO, CHANNEL::STEREO); + raw.channels = clamp<int>(wav.channels, CHANNEL::MONO, CHANNEL::STEREO); } else throw SourceException(); diff --git a/src/libjin/Core/Game.cpp b/src/libjin/Core/Game.cpp index f2223b2..4509478 100644 --- a/src/libjin/Core/Game.cpp +++ b/src/libjin/Core/Game.cpp @@ -1,30 +1,48 @@ #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() { + 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(); while (_running) { while (jin::input::pollEvent(&e)) { - if (_instance != nullptr && _onEvent) - _onEvent(&e); + SAFECALL(_onEvent, &e); + if (!_running) goto stoploop; } - if (!_running) - break; + SAFECALL(_onUpdate); + SAFECALL(_onDraw); + const int current = getMilliSecond(); + const int wait = MS_PER_UPDATE - (current - previous); + previous += MS_PER_UPDATE; + if (wait > 0) + sleep(wait); + else + previous = current; } + stoploop:; } bool Game::initSystem(const SettingBase* setting) diff --git a/src/libjin/Core/Game.h b/src/libjin/Core/Game.h index e9d0340..31825ba 100644 --- a/src/libjin/Core/Game.h +++ b/src/libjin/Core/Game.h @@ -17,7 +17,7 @@ namespace core public: typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(float dt); + typedef void(*onUpdate)(); typedef void(*onDraw)(); struct Setting : SettingBase @@ -29,7 +29,7 @@ namespace core void run(); inline void stop() { _running = false; }; - bool running() { return _running; }; + inline bool running() { return _running; }; private: @@ -44,8 +44,8 @@ namespace core bool _running; - onlyonce bool initSystem(const SettingBase* setting); - onlyonce void quitSystem(); + bool initSystem(const SettingBase* setting); + void quitSystem(); }; diff --git a/src/libjin/Graphics/Canvas.cpp b/src/libjin/Graphics/Canvas.cpp index dddc889..f5bd09f 100644 --- a/src/libjin/Graphics/Canvas.cpp +++ b/src/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/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp index 7349c36..4c6707d 100644 --- a/src/libjin/Graphics/Texture.cpp +++ b/src/libjin/Graphics/Texture.cpp @@ -5,13 +5,15 @@ #include "texture.h" #include "../3rdparty/stb/stb_image.h" #include "../utils/utils.h" -#include "../math/math.h" +#include "../Math/Math.h" namespace jin { namespace graphics { + using namespace jin::math; + Texture* Texture::createTexture(const char* file) { std::ifstream fs; diff --git a/src/libjin/Graphics/Window.cpp b/src/libjin/Graphics/Window.cpp index 28e5b84..708a30f 100644 --- a/src/libjin/Graphics/Window.cpp +++ b/src/libjin/Graphics/Window.cpp @@ -14,17 +14,19 @@ namespace jin namespace graphics { - bool WindowSystem::initSystem(const SettingBase* s) + bool Window::initSystem(const SettingBase* s) { +#if JIN_DEBUG Loghelper::log(Loglevel::LV_INFO, "Init window system"); +#endif // JIN_DEBUG if (SDL_Init(SDL_INIT_VIDEO) < 0) return false; const Setting* setting = (Setting*)s; - width = setting->width; height = setting->height; + fps = setting->fps; bool vsync = setting->vsync; const char* title = setting->title; @@ -52,8 +54,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); @@ -73,13 +79,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/src/libjin/Graphics/Window.h b/src/libjin/Graphics/Window.h index 6213cee..e09e9f9 100644 --- a/src/libjin/Graphics/Window.h +++ b/src/libjin/Graphics/Window.h @@ -12,40 +12,37 @@ namespace jin namespace graphics { - class WindowSystem : public Subsystem<WindowSystem> + class Window : public Subsystem<Window> { public: struct Setting : SettingBase { public: + const char* title; // + bool fullscreen; // ȫ int width, height; // ڴС bool vsync; // ֱͬ - const char* title; // + int fps; // FPS + bool resizable; // resize }; - inline int getW() - { - return width; - } - - inline int getH() - { - return height; - } - + inline int getW(){ return width; } + inline int getH(){ return height; } + inline int getFPS(){ return fps; } inline void swapBuffers(); private: - WindowSystem() {}; - virtual ~WindowSystem() {}; + Window() {}; + virtual ~Window() {}; - SINGLETON(WindowSystem); + SINGLETON(Window); SDL_Window* wnd; int width, height; + int fps; onlyonce bool initSystem(const SettingBase* setting) override; onlyonce void quitSystem() override; diff --git a/src/libjin/Input/Event.h b/src/libjin/Input/Event.h index 91d8593..9feb3a5 100644 --- a/src/libjin/Input/Event.h +++ b/src/libjin/Input/Event.h @@ -16,12 +16,32 @@ namespace input enum EventType { QUIT = SDL_QUIT, - KEYDOWN = SDL_KEYDOWN, - KEYUP = SDL_KEYUP, - MOUSEMOTION = SDL_MOUSEMOTION, - MOUSEBUTTONDOWN = SDL_MOUSEBUTTONDOWN, - MOUSEBUTTONUP = SDL_MOUSEBUTTONUP, - MOUSEWHEEL = SDL_MOUSEWHEEL + KEY_DOWN = SDL_KEYDOWN, + KEY_UP = SDL_KEYUP, + MOUSE_MOTION = SDL_MOUSEMOTION, + MOUSE_BUTTON_DOWN = SDL_MOUSEBUTTONDOWN, + MOUSE_BUTTON_UP = SDL_MOUSEBUTTONUP, + MOUSE_WHEEL = SDL_MOUSEWHEEL, + WINDOW_EVENT = SDL_WINDOWEVENT, + }; + + enum WindowEvent { + WINDOW_SHOWN = SDL_WINDOWEVENT_SHOWN , + WINDOW_HIDDEN = SDL_WINDOWEVENT_HIDDEN , + WINDOW_EXPOSED = SDL_WINDOWEVENT_EXPOSED , + WINDOW_MOVED = SDL_WINDOWEVENT_MOVED , + WINDOW_RESIZED = SDL_WINDOWEVENT_RESIZED , + WINDOW_SIZE_CAHNGE = SDL_WINDOWEVENT_SIZE_CHANGED , + WINDOW_MINIMIZED = SDL_WINDOWEVENT_MINIMIZED , + WINDOW_MAXIMIZED = SDL_WINDOWEVENT_MAXIMIZED , + WINDOW_RESTORED = SDL_WINDOWEVENT_RESTORED , + WINDOW_ENTER = SDL_WINDOWEVENT_ENTER , + WINDOW_LEAVE = SDL_WINDOWEVENT_LEAVE , + WINDOW_FOCUS_GAINED = SDL_WINDOWEVENT_FOCUS_GAINED, + WINDOW_FOCUS_LOST = SDL_WINDOWEVENT_FOCUS_LOST , + WINDOW_CLOSE = SDL_WINDOWEVENT_CLOSE , + WINDOW_TAKE_FOCUS = SDL_WINDOWEVENT_TAKE_FOCUS , + WINDOW_HIT_TEST = SDL_WINDOWEVENT_HIT_TEST , }; inline int pollEvent(Event* e) @@ -64,8 +84,8 @@ namespace input */ #endif // JIN_INPUT_SDL -} -} +} // input +} // jin #endif // JIN_MODULES_INPUT #endif
\ No newline at end of file diff --git a/src/libjin/Math/Math.h b/src/libjin/Math/Math.h index 849f74b..5b34f4c 100644 --- a/src/libjin/Math/Math.h +++ b/src/libjin/Math/Math.h @@ -1,16 +1,71 @@ #ifndef __JIN_UTILS_MATH_H #define __JIN_UTILS_MATH_H -#include <math.h> - #include "constant.h" #include "matrix.h" #include "quad.h" -#define min(a,b) (a < b ? a : b) -#define max(a,b) (a > b ? a : b) -#define clamp(a, mi,ma) (min(max(a,mi),ma)) -#define within(a,min,max) (a >= min && a <= max) -#define without(a,min,max) (a < min || a > max) +namespace jin +{ +namespace math +{ + +#ifdef min +#undef min +#endif // min +#ifdef max +#undef max +#endif // max + + template<typename T> + inline T min(T a, T b) + { + return a < b ? a : b; + } + + template<typename T> + inline T max(T a, T b) + { + return a > b ? a : b; + } + + template<typename T> + inline T clamp(T a, T mi, T ma) + { + return min<T>(max<T>(a, mi), ma); + } + + template<typename T> + inline bool within(T a, T mi, T ma) + { + return a >= mi && a <= ma; + } + + template<typename T> + inline bool without(T a, T mi, T ma) + { + return a < mi || a > ma; + } + + template<typename T> + inline T abs(T a) + { + return a > 0 ? a : -a; + } + + template<typename T> + inline T lowerBound(T a, T lower) + { + return a < lower ? lower : a; + } + + template<typename T> + inline T upperBound(T a, T upper) + { + return a > upper ? upper : a; + } + +} +} #endif
\ No newline at end of file diff --git a/src/libjin/Math/Matrix.h b/src/libjin/Math/Matrix.h index 673d253..ff4a51a 100644 --- a/src/libjin/Math/Matrix.h +++ b/src/libjin/Math/Matrix.h @@ -1,6 +1,6 @@ #ifndef __JIN_MATRIX_H #define __JIN_MATRIX_H -#include <math.h> + namespace jin { namespace math diff --git a/src/libjin/Time/Timer.cpp b/src/libjin/Time/Timer.cpp index e69de29..fe72a90 100644 --- a/src/libjin/Time/Timer.cpp +++ b/src/libjin/Time/Timer.cpp @@ -0,0 +1,14 @@ +#include "../modules.h" +#if JIN_MODULES_TIME + +namespace jin +{ +namespace time +{ + + + +} +} + +#endif // JIN_MODULES_TIME
\ No newline at end of file diff --git a/src/libjin/Time/Timer.h b/src/libjin/Time/Timer.h index 1742306..9458215 100644 --- a/src/libjin/Time/Timer.h +++ b/src/libjin/Time/Timer.h @@ -3,15 +3,36 @@ #include "../modules.h" #if JIN_MODULES_TIME +#include "SDL2/SDL.h" + namespace jin { namespace time { + inline void sleep(int ms) + { +#if JIN_TIME_SDL + SDL_Delay(ms); +#endif + } + + inline double getSecond() + { +#if JIN_TIME_SDL + return SDL_GetTicks() / 1000.f; +#endif + } + inline double getMilliSecond() + { +#if JIN_TIME_SDL + return SDL_GetTicks(); +#endif + } -} -} +} // time +} // jin #endif // JIN_MODULES_TIME #endif // __JIN_TIMER_H
\ No newline at end of file diff --git a/src/libjin/Utils/macros.h b/src/libjin/Utils/macros.h index 684e8e8..a57cb7c 100644 --- a/src/libjin/Utils/macros.h +++ b/src/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/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index 60de66d..5b43729 100644 --- a/src/libjin/audio/audio.h +++ b/src/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/src/libjin/core/game.cpp b/src/libjin/core/game.cpp index f2223b2..4509478 100644 --- a/src/libjin/core/game.cpp +++ b/src/libjin/core/game.cpp @@ -1,30 +1,48 @@ #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() { + 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(); while (_running) { while (jin::input::pollEvent(&e)) { - if (_instance != nullptr && _onEvent) - _onEvent(&e); + SAFECALL(_onEvent, &e); + if (!_running) goto stoploop; } - if (!_running) - break; + SAFECALL(_onUpdate); + SAFECALL(_onDraw); + const int current = getMilliSecond(); + const int wait = MS_PER_UPDATE - (current - previous); + previous += MS_PER_UPDATE; + if (wait > 0) + sleep(wait); + else + previous = current; } + stoploop:; } bool Game::initSystem(const SettingBase* setting) diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index e9d0340..31825ba 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -17,7 +17,7 @@ namespace core public: typedef void(*onEvent)(jin::input::Event* e); - typedef void(*onUpdate)(float dt); + typedef void(*onUpdate)(); typedef void(*onDraw)(); struct Setting : SettingBase @@ -29,7 +29,7 @@ namespace core void run(); inline void stop() { _running = false; }; - bool running() { return _running; }; + inline bool running() { return _running; }; private: @@ -44,8 +44,8 @@ namespace core bool _running; - onlyonce bool initSystem(const SettingBase* setting); - onlyonce void quitSystem(); + bool initSystem(const SettingBase* setting); + void quitSystem(); }; diff --git a/src/libjin/input/event.h b/src/libjin/input/event.h index 91d8593..9feb3a5 100644 --- a/src/libjin/input/event.h +++ b/src/libjin/input/event.h @@ -16,12 +16,32 @@ namespace input enum EventType { QUIT = SDL_QUIT, - KEYDOWN = SDL_KEYDOWN, - KEYUP = SDL_KEYUP, - MOUSEMOTION = SDL_MOUSEMOTION, - MOUSEBUTTONDOWN = SDL_MOUSEBUTTONDOWN, - MOUSEBUTTONUP = SDL_MOUSEBUTTONUP, - MOUSEWHEEL = SDL_MOUSEWHEEL + KEY_DOWN = SDL_KEYDOWN, + KEY_UP = SDL_KEYUP, + MOUSE_MOTION = SDL_MOUSEMOTION, + MOUSE_BUTTON_DOWN = SDL_MOUSEBUTTONDOWN, + MOUSE_BUTTON_UP = SDL_MOUSEBUTTONUP, + MOUSE_WHEEL = SDL_MOUSEWHEEL, + WINDOW_EVENT = SDL_WINDOWEVENT, + }; + + enum WindowEvent { + WINDOW_SHOWN = SDL_WINDOWEVENT_SHOWN , + WINDOW_HIDDEN = SDL_WINDOWEVENT_HIDDEN , + WINDOW_EXPOSED = SDL_WINDOWEVENT_EXPOSED , + WINDOW_MOVED = SDL_WINDOWEVENT_MOVED , + WINDOW_RESIZED = SDL_WINDOWEVENT_RESIZED , + WINDOW_SIZE_CAHNGE = SDL_WINDOWEVENT_SIZE_CHANGED , + WINDOW_MINIMIZED = SDL_WINDOWEVENT_MINIMIZED , + WINDOW_MAXIMIZED = SDL_WINDOWEVENT_MAXIMIZED , + WINDOW_RESTORED = SDL_WINDOWEVENT_RESTORED , + WINDOW_ENTER = SDL_WINDOWEVENT_ENTER , + WINDOW_LEAVE = SDL_WINDOWEVENT_LEAVE , + WINDOW_FOCUS_GAINED = SDL_WINDOWEVENT_FOCUS_GAINED, + WINDOW_FOCUS_LOST = SDL_WINDOWEVENT_FOCUS_LOST , + WINDOW_CLOSE = SDL_WINDOWEVENT_CLOSE , + WINDOW_TAKE_FOCUS = SDL_WINDOWEVENT_TAKE_FOCUS , + WINDOW_HIT_TEST = SDL_WINDOWEVENT_HIT_TEST , }; inline int pollEvent(Event* e) @@ -64,8 +84,8 @@ namespace input */ #endif // JIN_INPUT_SDL -} -} +} // input +} // jin #endif // JIN_MODULES_INPUT #endif
\ No newline at end of file diff --git a/src/libjin/jin.h b/src/libjin/jin.h index aa7e277..d32731d 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -12,6 +12,7 @@ #include "Input/Input.h" #include "Net/Net.h" #include "Graphics/Graphics.h" +#include "Time/Timer.h" #define JIN_VERSION "Jin 0.1" #define JIN_RELEASE "Jin 0.1.1" diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h index 849f74b..5b34f4c 100644 --- a/src/libjin/math/math.h +++ b/src/libjin/math/math.h @@ -1,16 +1,71 @@ #ifndef __JIN_UTILS_MATH_H #define __JIN_UTILS_MATH_H -#include <math.h> - #include "constant.h" #include "matrix.h" #include "quad.h" -#define min(a,b) (a < b ? a : b) -#define max(a,b) (a > b ? a : b) -#define clamp(a, mi,ma) (min(max(a,mi),ma)) -#define within(a,min,max) (a >= min && a <= max) -#define without(a,min,max) (a < min || a > max) +namespace jin +{ +namespace math +{ + +#ifdef min +#undef min +#endif // min +#ifdef max +#undef max +#endif // max + + template<typename T> + inline T min(T a, T b) + { + return a < b ? a : b; + } + + template<typename T> + inline T max(T a, T b) + { + return a > b ? a : b; + } + + template<typename T> + inline T clamp(T a, T mi, T ma) + { + return min<T>(max<T>(a, mi), ma); + } + + template<typename T> + inline bool within(T a, T mi, T ma) + { + return a >= mi && a <= ma; + } + + template<typename T> + inline bool without(T a, T mi, T ma) + { + return a < mi || a > ma; + } + + template<typename T> + inline T abs(T a) + { + return a > 0 ? a : -a; + } + + template<typename T> + inline T lowerBound(T a, T lower) + { + return a < lower ? lower : a; + } + + template<typename T> + inline T upperBound(T a, T upper) + { + return a > upper ? upper : a; + } + +} +} #endif
\ No newline at end of file diff --git a/src/libjin/math/matrix.h b/src/libjin/math/matrix.h index 673d253..ff4a51a 100644 --- a/src/libjin/math/matrix.h +++ b/src/libjin/math/matrix.h @@ -1,6 +1,6 @@ #ifndef __JIN_MATRIX_H #define __JIN_MATRIX_H -#include <math.h> + namespace jin { namespace math diff --git a/src/libjin/modules.h b/src/libjin/modules.h index 48b71c9..9db11e7 100644 --- a/src/libjin/modules.h +++ b/src/libjin/modules.h @@ -34,5 +34,12 @@ #define JIN_MODULES_THREAD 1 #define JIN_MODULES_TIME 1 +#define JIN_TIME_SDL 1 + +/* +* Debug +*/ + +#define JIN_DEBUG 1 #endif
\ No newline at end of file diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h index 684e8e8..a57cb7c 100644 --- a/src/libjin/utils/macros.h +++ b/src/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/src/lua/event/luaopen_event.cpp b/src/lua/event/luaopen_event.cpp index a53863d..8802175 100644 --- a/src/lua/event/luaopen_event.cpp +++ b/src/lua/event/luaopen_event.cpp @@ -31,37 +31,37 @@ namespace lua luax_setfield_string(L, "type", "quit"); break; - case EventType::KEYDOWN: + case EventType::KEY_DOWN: luax_setfield_string(L, "type", "keydown"); luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); break; - case EventType::KEYUP: + case EventType::KEY_UP: luax_setfield_string(L, "type", "keyup"); luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); break; - case EventType::MOUSEMOTION: + case EventType::MOUSE_MOTION: luax_setfield_string(L, "type", "mousemotion"); luax_setfield_number(L, "x", e.motion.x); luax_setfield_number(L, "y", e.motion.y); break; - case EventType::MOUSEBUTTONDOWN: + case EventType::MOUSE_BUTTON_DOWN: luax_setfield_string(L, "type", "mousebuttondown"); luax_setfield_string(L, "button", getButtonName(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; - case EventType::MOUSEBUTTONUP: + case EventType::MOUSE_BUTTON_UP: luax_setfield_string(L, "type", "mousebuttonup"); luax_setfield_string(L, "button", getButtonName(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; - case EventType::MOUSEWHEEL: + case EventType::MOUSE_WHEEL: luax_setfield_string(L, "type", "wheel"); if(e.wheel.x == -1) luax_setfield_string(L, "x", "left"); diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index 2d1ff57..a394cc3 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -27,12 +27,14 @@ namespace lua */ static int l_init(lua_State* L) { - WindowSystem* wnd = WindowSystem::get(); - WindowSystem::Setting setting; + Window* wnd = Window::get(); + Window::Setting setting; setting.width = luax_getfield_integer(L, 1, "width"); setting.height = luax_getfield_integer(L, 1, "height"); setting.title = luax_getfield_string(L, 1, "title"); setting.vsync = luax_getfield_bool(L, 1, "vsync"); + setting.fullscreen = luax_getfield_bool(L, 1, "fullscreen"); + setting.resizable= luax_getfield_bool(L, 1, "resizable"); if (!wnd->init(&setting)) { luax_pushboolean(L, false); @@ -44,7 +46,7 @@ namespace lua static int l_destroy(lua_State* L) { - WindowSystem* wnd = WindowSystem::get(); + Window* wnd = Window::get(); wnd->quit(); return 0; } @@ -54,7 +56,7 @@ namespace lua */ static int l_getSize(lua_State* L) { - WindowSystem* wnd = WindowSystem::get(); + Window* wnd = Window::get(); luax_pushnumber(L, wnd->getW()); luax_pushnumber(L, wnd->getH()); return 2; @@ -131,7 +133,7 @@ namespace lua */ static int l_present(lua_State* L) { - WindowSystem::get()->swapBuffers(); + Window::get()->swapBuffers(); return 0; } diff --git a/src/lua/time/luaopen_time.cpp b/src/lua/time/luaopen_time.cpp index 370d868..4ef5372 100644 --- a/src/lua/time/luaopen_time.cpp +++ b/src/lua/time/luaopen_time.cpp @@ -1,20 +1,24 @@ #include "lua/luax.h" #include <SDL2/SDL.h> +#include "libjin/jin.h" + namespace jin { namespace lua { + using namespace jin::time; + static int l_sec(lua_State* L) { - luax_pushnumber(L, SDL_GetTicks()/1000.f); + luax_pushnumber(L, getSecond()); return 1; } static int l_sleep(lua_State* L) { double sec = luax_checknumber(L, 1); - SDL_Delay(sec * 1000); + sleep(sec * 1000.0f); return 0; } |