mirror of
https://github.com/XProger/OpenLara.git
synced 2025-08-14 09:04:31 +02:00
#15 UPS deltaTime logic
This commit is contained in:
12
src/game.h
12
src/game.h
@@ -39,7 +39,7 @@ namespace Game {
|
|||||||
Core::free();
|
Core::free();
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() {
|
void updateTick() {
|
||||||
float dt = Core::deltaTime;
|
float dt = Core::deltaTime;
|
||||||
if (Input::down[ikV]) { // third <-> first person view
|
if (Input::down[ikV]) { // third <-> first person view
|
||||||
level->camera->changeView(!level->camera->firstPerson);
|
level->camera->changeView(!level->camera->firstPerson);
|
||||||
@@ -55,10 +55,18 @@ namespace Game {
|
|||||||
|
|
||||||
level->update();
|
level->update();
|
||||||
|
|
||||||
|
|
||||||
Core::deltaTime = dt;
|
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() {
|
void render() {
|
||||||
Core::beginFrame();
|
Core::beginFrame();
|
||||||
level->render();
|
level->render();
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
|
//#include "vr/gvr/capi/include/gvr.h"
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -7,6 +8,10 @@
|
|||||||
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
|
#define JNI_METHOD(return_type, method_name) \
|
||||||
|
JNIEXPORT return_type JNICALL \
|
||||||
|
Java_org_xproger_openlara_Wrapper_##method_name
|
||||||
|
|
||||||
int getTime() {
|
int getTime() {
|
||||||
timeval time;
|
timeval time;
|
||||||
gettimeofday(&time, NULL);
|
gettimeofday(&time, NULL);
|
||||||
@@ -20,7 +25,7 @@ int lastTime, fpsTime, fps;
|
|||||||
char Stream::cacheDir[255];
|
char Stream::cacheDir[255];
|
||||||
char Stream::contentDir[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;
|
const char* str;
|
||||||
|
|
||||||
Stream::contentDir[0] = Stream::cacheDir[0] = 0;
|
Stream::contentDir[0] = Stream::cacheDir[0] = 0;
|
||||||
@@ -43,29 +48,23 @@ JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeInit(JNIEnv* env,
|
|||||||
fps = 0;
|
fps = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeFree(JNIEnv* env) {
|
JNI_METHOD(void, nativeFree)(JNIEnv* env) {
|
||||||
Game::free();
|
Game::free();
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeReset(JNIEnv* env) {
|
JNI_METHOD(void, nativeReset)(JNIEnv* env) {
|
||||||
// core->reset();
|
// core->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeUpdate(JNIEnv* env) {
|
JNI_METHOD(void, nativeUpdate)(JNIEnv* env) {
|
||||||
int time = getTime();
|
int time = getTime();
|
||||||
if (time == lastTime)
|
if (time == lastTime)
|
||||||
return;
|
return;
|
||||||
|
Game::update((time - lastTime) * 0.001f);
|
||||||
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;
|
|
||||||
}
|
|
||||||
lastTime = time;
|
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.dips = 0;
|
||||||
Core::stats.tris = 0;
|
Core::stats.tris = 0;
|
||||||
Game::render();
|
Game::render();
|
||||||
@@ -77,7 +76,7 @@ JNIEXPORT void JNICALL Java_org_xproger_openlara_Wrapper_nativeRender(JNIEnv* en
|
|||||||
fps++;
|
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::width = w;
|
||||||
Core::height = h;
|
Core::height = h;
|
||||||
}
|
}
|
||||||
@@ -86,7 +85,7 @@ float DeadZone(float x) {
|
|||||||
return x = fabsf(x) < 0.2f ? 0.0f : 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;
|
if (id > 1) return;
|
||||||
/* gamepad
|
/* gamepad
|
||||||
if (state < 0) {
|
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);
|
jshort *frames = env->GetShortArrayElements(buffer, NULL);
|
||||||
jsize count = env->GetArrayLength(buffer) / 2;
|
jsize count = env->GetArrayLength(buffer) / 2;
|
||||||
Sound::fill((Sound::Frame*)frames, count);
|
Sound::fill((Sound::Frame*)frames, count);
|
||||||
|
@@ -176,13 +176,8 @@ int main() {
|
|||||||
if (time <= lastTime)
|
if (time <= lastTime)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
float delta = (time - lastTime) * 0.001f;
|
|
||||||
pthread_mutex_lock(&sndMutex);
|
pthread_mutex_lock(&sndMutex);
|
||||||
while (delta > EPS) {
|
Game::update((time - lastTime) * 0.001f);
|
||||||
Core::deltaTime = min(delta, 1.0f / 30.0f);
|
|
||||||
Game::update();
|
|
||||||
delta -= Core::deltaTime;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&sndMutex);
|
pthread_mutex_unlock(&sndMutex);
|
||||||
lastTime = time;
|
lastTime = time;
|
||||||
|
|
||||||
|
@@ -203,26 +203,11 @@ int main() {
|
|||||||
int time = getTime();
|
int time = getTime();
|
||||||
if (time <= lastTime)
|
if (time <= lastTime)
|
||||||
continue;
|
continue;
|
||||||
|
Game::update((time - lastTime) * 0.001f);
|
||||||
float delta = (time - lastTime) * 0.001f;
|
|
||||||
while (delta > EPS) {
|
|
||||||
Core::deltaTime = min(delta, 1.0f / 30.0f);
|
|
||||||
Game::update();
|
|
||||||
delta -= Core::deltaTime;
|
|
||||||
}
|
|
||||||
lastTime = time;
|
lastTime = time;
|
||||||
|
|
||||||
Core::stats.dips = 0;
|
|
||||||
Core::stats.tris = 0;
|
|
||||||
Game::render();
|
Game::render();
|
||||||
aglSwapBuffers(context);
|
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();
|
Game::free();
|
||||||
|
@@ -96,16 +96,9 @@ void main_loop() {
|
|||||||
joyUpdate();
|
joyUpdate();
|
||||||
|
|
||||||
int time = getTime();
|
int time = getTime();
|
||||||
|
|
||||||
if (time - lastTime <= 0)
|
if (time - lastTime <= 0)
|
||||||
return;
|
return;
|
||||||
|
Game::update((time - lastTime) * 0.001f);
|
||||||
float delta = (time - lastTime) * 0.001f;
|
|
||||||
while (delta > EPS) {
|
|
||||||
Core::deltaTime = min(delta, 1.0f / 30.0f);
|
|
||||||
Game::update();
|
|
||||||
delta -= Core::deltaTime;
|
|
||||||
}
|
|
||||||
lastTime = time;
|
lastTime = time;
|
||||||
|
|
||||||
Game::render();
|
Game::render();
|
||||||
@@ -148,7 +141,6 @@ void freeGL() {
|
|||||||
EM_BOOL resize() {
|
EM_BOOL resize() {
|
||||||
int f;
|
int f;
|
||||||
emscripten_get_canvas_size(&Core::width, &Core::height, &f);
|
emscripten_get_canvas_size(&Core::width, &Core::height, &f);
|
||||||
LOG("resize %d x %d\n", Core::width, Core::height);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -371,13 +371,8 @@ int main(int argc, char** argv) {
|
|||||||
if (time <= lastTime)
|
if (time <= lastTime)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
float delta = min(1.0f, (time - lastTime) * 0.001f);
|
|
||||||
EnterCriticalSection(&sndCS);
|
EnterCriticalSection(&sndCS);
|
||||||
while (delta > EPS) {
|
Game::update((time - lastTime) * 0.001f);
|
||||||
Core::deltaTime = min(delta, 1.0f / 30.0f);
|
|
||||||
Game::update();
|
|
||||||
delta -= Core::deltaTime;
|
|
||||||
}
|
|
||||||
LeaveCriticalSection(&sndCS);
|
LeaveCriticalSection(&sndCS);
|
||||||
lastTime = time;
|
lastTime = time;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user