1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-13 16:44:50 +02:00

#15 UPS deltaTime logic

This commit is contained in:
XProger
2017-03-17 02:12:33 +03:00
parent 7ea7f783e9
commit a180e36037
6 changed files with 28 additions and 54 deletions

View File

@@ -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();

View File

@@ -1,5 +1,6 @@
#include <jni.h>
#include <android/log.h>
//#include "vr/gvr/capi/include/gvr.h"
#include <sys/time.h>
#include <stdint.h>
#include <stdio.h>
@@ -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);

View File

@@ -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;

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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;