From a180e360377e9ecb965902d970813129a8e2efa2 Mon Sep 17 00:00:00 2001 From: XProger Date: Fri, 17 Mar 2017 02:12:33 +0300 Subject: [PATCH] #15 UPS deltaTime logic --- src/game.h | 12 ++++++++++-- src/platform/android/jni/main.cpp | 29 ++++++++++++++--------------- src/platform/nix/main.cpp | 7 +------ src/platform/osx/main.cpp | 17 +---------------- src/platform/web/main.cpp | 10 +--------- src/platform/win/main.cpp | 7 +------ 6 files changed, 28 insertions(+), 54 deletions(-) diff --git a/src/game.h b/src/game.h index 6f83de0..c06d7bc 100644 --- a/src/game.h +++ b/src/game.h @@ -39,7 +39,7 @@ namespace Game { Core::free(); } - void update() { + void updateTick() { float dt = Core::deltaTime; if (Input::down[ikV]) { // third <-> first person view level->camera->changeView(!level->camera->firstPerson); @@ -55,10 +55,18 @@ namespace Game { level->update(); - Core::deltaTime = dt; } + void update(float delta) { + delta = min(1.0f, delta); + while (delta > EPS) { + Core::deltaTime = min(delta, 1.0f / 30.0f); + Game::updateTick(); + delta -= Core::deltaTime; + } + } + void render() { Core::beginFrame(); level->render(); diff --git a/src/platform/android/jni/main.cpp b/src/platform/android/jni/main.cpp index f2b1ed3..6d47ed4 100644 --- a/src/platform/android/jni/main.cpp +++ b/src/platform/android/jni/main.cpp @@ -1,5 +1,6 @@ #include #include +//#include "vr/gvr/capi/include/gvr.h" #include #include #include @@ -7,6 +8,10 @@ #include "game.h" +#define JNI_METHOD(return_type, method_name) \ + JNIEXPORT return_type JNICALL \ + Java_org_xproger_openlara_Wrapper_##method_name + int getTime() { timeval time; gettimeofday(&time, NULL); @@ -20,7 +25,7 @@ int lastTime, fpsTime, fps; char Stream::cacheDir[255]; char Stream::contentDir[255]; -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeInit(JNIEnv* env, jobject obj, jstring packName, jstring cacheDir, jint levelOffset, jint musicOffset) { +JNI_METHOD(void, nativeInit)(JNIEnv* env, jobject obj, jstring packName, jstring cacheDir, jint levelOffset, jint musicOffset) { const char* str; Stream::contentDir[0] = Stream::cacheDir[0] = 0; @@ -43,29 +48,23 @@ JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeInit(JNIEnv* env, fps = 0; } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeFree(JNIEnv* env) { +JNI_METHOD(void, nativeFree)(JNIEnv* env) { Game::free(); } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeReset(JNIEnv* env) { +JNI_METHOD(void, nativeReset)(JNIEnv* env) { // core->reset(); } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeUpdate(JNIEnv* env) { +JNI_METHOD(void, nativeUpdate)(JNIEnv* env) { int time = getTime(); if (time == lastTime) return; - - float delta = min(1.0f, (time - lastTime) * 0.001f); - while (delta > EPS) { - Core::deltaTime = min(delta, 1.0f / 30.0f); - Game::update(); - delta -= Core::deltaTime; - } + Game::update((time - lastTime) * 0.001f); lastTime = time; } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeRender(JNIEnv* env) { +JNI_METHOD(void, nativeRender)(JNIEnv* env) { Core::stats.dips = 0; Core::stats.tris = 0; Game::render(); @@ -77,7 +76,7 @@ JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeRender(JNIEnv* en fps++; } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeResize(JNIEnv* env, jobject obj, jint w, jint h) { +JNI_METHOD(void, nativeResize)(JNIEnv* env, jobject obj, jint w, jint h) { Core::width = w; Core::height = h; } @@ -86,7 +85,7 @@ float DeadZone(float x) { return x = fabsf(x) < 0.2f ? 0.0f : x; } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeTouch(JNIEnv* env, jobject obj, jint id, jint state, jfloat x, jfloat y) { +JNI_METHOD(void, nativeTouch)(JNIEnv* env, jobject obj, jint id, jint state, jfloat x, jfloat y) { if (id > 1) return; /* gamepad if (state < 0) { @@ -140,7 +139,7 @@ JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeTouch(JNIEnv* env } } -JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeSoundFill(JNIEnv* env, jobject obj, jshortArray buffer) { +JNI_METHOD(void, nativeSoundFill)(JNIEnv* env, jobject obj, jshortArray buffer) { jshort *frames = env->GetShortArrayElements(buffer, NULL); jsize count = env->GetArrayLength(buffer) / 2; Sound::fill((Sound::Frame*)frames, count); diff --git a/src/platform/nix/main.cpp b/src/platform/nix/main.cpp index 10d1cb5..fb99d7f 100644 --- a/src/platform/nix/main.cpp +++ b/src/platform/nix/main.cpp @@ -176,13 +176,8 @@ int main() { if (time <= lastTime) continue; - float delta = (time - lastTime) * 0.001f; pthread_mutex_lock(&sndMutex); - while (delta > EPS) { - Core::deltaTime = min(delta, 1.0f / 30.0f); - Game::update(); - delta -= Core::deltaTime; - } + Game::update((time - lastTime) * 0.001f); pthread_mutex_unlock(&sndMutex); lastTime = time; diff --git a/src/platform/osx/main.cpp b/src/platform/osx/main.cpp index eb6bd33..08b5eb8 100644 --- a/src/platform/osx/main.cpp +++ b/src/platform/osx/main.cpp @@ -203,26 +203,11 @@ int main() { int time = getTime(); if (time <= lastTime) continue; - - float delta = (time - lastTime) * 0.001f; - while (delta > EPS) { - Core::deltaTime = min(delta, 1.0f / 30.0f); - Game::update(); - delta -= Core::deltaTime; - } + Game::update((time - lastTime) * 0.001f); lastTime = time; - Core::stats.dips = 0; - Core::stats.tris = 0; Game::render(); aglSwapBuffers(context); - - if (fpsTime < getTime()) { - LOG("FPS: %d DIP: %d TRI: %d\n", fps, Core::stats.dips, Core::stats.tris); - fps = 0; - fpsTime = getTime() + 1000; - } else - fps++; } Game::free(); diff --git a/src/platform/web/main.cpp b/src/platform/web/main.cpp index 8f93e79..3521eea 100644 --- a/src/platform/web/main.cpp +++ b/src/platform/web/main.cpp @@ -96,16 +96,9 @@ void main_loop() { joyUpdate(); int time = getTime(); - if (time - lastTime <= 0) return; - - float delta = (time - lastTime) * 0.001f; - while (delta > EPS) { - Core::deltaTime = min(delta, 1.0f / 30.0f); - Game::update(); - delta -= Core::deltaTime; - } + Game::update((time - lastTime) * 0.001f); lastTime = time; Game::render(); @@ -148,7 +141,6 @@ void freeGL() { EM_BOOL resize() { int f; emscripten_get_canvas_size(&Core::width, &Core::height, &f); - LOG("resize %d x %d\n", Core::width, Core::height); return 1; } diff --git a/src/platform/win/main.cpp b/src/platform/win/main.cpp index 2097115..ca283b3 100644 --- a/src/platform/win/main.cpp +++ b/src/platform/win/main.cpp @@ -371,13 +371,8 @@ int main(int argc, char** argv) { if (time <= lastTime) continue; - float delta = min(1.0f, (time - lastTime) * 0.001f); EnterCriticalSection(&sndCS); - while (delta > EPS) { - Core::deltaTime = min(delta, 1.0f / 30.0f); - Game::update(); - delta -= Core::deltaTime; - } + Game::update((time - lastTime) * 0.001f); LeaveCriticalSection(&sndCS); lastTime = time;