diff options
-rw-r--r-- | libjin/Audio/SDL/SDLAudio.cpp | 4 | ||||
-rw-r--r-- | libjin/Audio/SDL/SDLSource.cpp | 4 | ||||
-rw-r--r-- | libjin/Core/Game.cpp | 30 | ||||
-rw-r--r-- | libjin/Core/Game.h | 6 | ||||
-rw-r--r-- | libjin/Graphics/Texture.cpp | 4 | ||||
-rw-r--r-- | libjin/Math/Math.h | 75 | ||||
-rw-r--r-- | libjin/Math/Matrix.h | 2 | ||||
-rw-r--r-- | libjin/Time/Timer.cpp | 14 | ||||
-rw-r--r-- | libjin/Time/Timer.h | 25 | ||||
-rw-r--r-- | libjin/jin.h | 1 | ||||
-rw-r--r-- | libjin/modules.h | 1 | ||||
-rw-r--r-- | test/01HelloWorld/main.cpp | 11 |
12 files changed, 155 insertions, 22 deletions
diff --git a/libjin/Audio/SDL/SDLAudio.cpp b/libjin/Audio/SDL/SDLAudio.cpp index c154ae4..a41382b 100644 --- a/libjin/Audio/SDL/SDLAudio.cpp +++ b/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/libjin/Audio/SDL/SDLSource.cpp b/libjin/Audio/SDL/SDLSource.cpp index b70230d..18ba855 100644 --- a/libjin/Audio/SDL/SDLSource.cpp +++ b/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/libjin/Core/Game.cpp b/libjin/Core/Game.cpp index 707a983..be5cc00 100644 --- a/libjin/Core/Game.cpp +++ b/libjin/Core/Game.cpp @@ -2,6 +2,8 @@ #include "../Time/Timer.h" #include "../input/Event.h" #include "../Graphics/Window.h" +#include "../Math/Math.h" +#include <iostream> namespace jin { @@ -10,17 +12,20 @@ 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() { WindowSystem* wnd = WindowSystem::get(); + const int FPS = wnd ? wnd->getFPS() : 60; + const int MS_PER_UPDATE = 1000.0f / FPS; _running = true; Event e; - int fps = 60; - if (wnd != nullptr) - fps = wnd->getFPS(); + int previous = getMilliSecond(); + int lag = 0; while (_running) { while (jin::input::pollEvent(&e)) @@ -30,6 +35,25 @@ namespace core } if (!_running) break; + + if (_onUpdate) + _onUpdate(); + + if(_onDraw) + _onDraw(); + + int current = getMilliSecond(); + int wait = MS_PER_UPDATE - (current - previous); + previous += MS_PER_UPDATE; + if (wait >= 0 && lag == 0) + sleep(wait); + else if(wait >= 0 && lag > 0) // catch, dont sleep + lag = lowerBound(lag - MS_PER_UPDATE, 0); + else if(wait < 0) + { + lag += abs(wait); + previous = current; + } } } diff --git a/libjin/Core/Game.h b/libjin/Core/Game.h index 7ae1855..31825ba 100644 --- a/libjin/Core/Game.h +++ b/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 @@ -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/libjin/Graphics/Texture.cpp b/libjin/Graphics/Texture.cpp index 7349c36..4c6707d 100644 --- a/libjin/Graphics/Texture.cpp +++ b/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/libjin/Math/Math.h b/libjin/Math/Math.h index 849f74b..76f9da7 100644 --- a/libjin/Math/Math.h +++ b/libjin/Math/Math.h @@ -1,16 +1,77 @@ #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) +//#define abs(n) (a > 0 ? a : -a) +// +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; + } -#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) + 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/libjin/Math/Matrix.h b/libjin/Math/Matrix.h index 673d253..ff4a51a 100644 --- a/libjin/Math/Matrix.h +++ b/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/libjin/Time/Timer.cpp b/libjin/Time/Timer.cpp index e69de29..fe72a90 100644 --- a/libjin/Time/Timer.cpp +++ b/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/libjin/Time/Timer.h b/libjin/Time/Timer.h index 1742306..9458215 100644 --- a/libjin/Time/Timer.h +++ b/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/libjin/jin.h b/libjin/jin.h index aa7e277..d32731d 100644 --- a/libjin/jin.h +++ b/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/libjin/modules.h b/libjin/modules.h index 48b71c9..e46d527 100644 --- a/libjin/modules.h +++ b/libjin/modules.h @@ -34,5 +34,6 @@ #define JIN_MODULES_THREAD 1 #define JIN_MODULES_TIME 1 +#define JIN_TIME_SDL 1 #endif
\ No newline at end of file diff --git a/test/01HelloWorld/main.cpp b/test/01HelloWorld/main.cpp index 77d5096..6d76c8a 100644 --- a/test/01HelloWorld/main.cpp +++ b/test/01HelloWorld/main.cpp @@ -11,15 +11,20 @@ void onEvent(jin::input::Event* e) if (e->type == EventType::QUIT) game->stop(); } - -void onUpdate(float dt) +static float dt = 0; +void onUpdate() { } void onDraw() { - + dt += 16; + if (dt > 1000) + { + std::cout << "a"; + dt = 0; + } } int main(int argc, char* argv[]) |