Skip to content

Commit

Permalink
delta time 60/90/120 Hz fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimBoev committed Sep 23, 2024
1 parent bf3287d commit 24166ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
Binary file modified FlappyBird/app/build/outputs/apk/FlappyBird-signed.apk
Binary file not shown.
37 changes: 0 additions & 37 deletions FlappyBird/app/src/main/jni/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include "audio.h"
#include "game.h"

#include <time.h>

struct timespec lastFrameTime;
double deltaTime = 0.0f;

bool g_Initialized = false;
EGLDisplay g_EglDisplay = EGL_NO_DISPLAY;
Expand Down Expand Up @@ -155,57 +151,24 @@ void Init(struct android_app* app)
gColorHandle = glGetUniformLocation(colorProgram, "u_Color");


clock_gettime(CLOCK_MONOTONIC, &lastFrameTime);

Log("FlappyBird is loaded!");

g_Initialized = true;
}

void Update()
{
struct timespec currentTime;
clock_gettime(CLOCK_MONOTONIC, &currentTime);

deltaTime = (currentTime.tv_sec - lastFrameTime.tv_sec) + (currentTime.tv_nsec - lastFrameTime.tv_nsec) / 1e9;
lastFrameTime = currentTime;
}

void MainLoopStep()
{
if (g_EglDisplay == EGL_NO_DISPLAY)
return;

static struct timespec lastFrameTime = { 0, 0 };
struct timespec currentFrameTime;
clock_gettime(CLOCK_MONOTONIC, &currentFrameTime);

if (lastFrameTime.tv_sec != 0 || lastFrameTime.tv_nsec != 0)
{
deltaTime = (currentFrameTime.tv_sec - lastFrameTime.tv_sec) +
(currentFrameTime.tv_nsec - lastFrameTime.tv_nsec) / 1e9;
}

// Setup display size (every frame to accommodate for window resizing)
glViewport(0, 0, (int)WindowSizeX, (int)WindowSizeY);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

Update();
Render();

eglSwapBuffers(g_EglDisplay, g_EglSurface);

lastFrameTime = currentFrameTime;

double frameDuration = deltaTime;
if (frameDuration < 1.0 / 60.0)
{
struct timespec sleepTime;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = (1.0 / 60.0 - frameDuration) * 1e9;
nanosleep(&sleepTime, NULL);
}
}

void Shutdown()
Expand Down
30 changes: 29 additions & 1 deletion FlappyBird/app/src/main/jni/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void handle_input(struct android_app* app, AInputEvent* event)
}
}

#define TARGET_FPS 60
#define TARGET_FRAME_TIME (1.0f / TARGET_FPS)

double g_Time = 0.0;
float DeltaTime = 0.0f;

void android_main(struct android_app* state)
{
Expand All @@ -85,8 +90,31 @@ void android_main(struct android_app* state)
}
}

if (g_Initialized)
if (g_Initialized)
{
struct timespec current_timespec;
clock_gettime(CLOCK_MONOTONIC, &current_timespec);
double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);

// fps limit
if (DeltaTime < TARGET_FRAME_TIME)
{
// calc time wait
double sleep_time = TARGET_FRAME_TIME - DeltaTime;
struct timespec sleep_spec;
sleep_spec.tv_sec = (time_t)sleep_time;
sleep_spec.tv_nsec = (long)((sleep_time - sleep_spec.tv_sec) * 1000000000.0);
nanosleep(&sleep_spec, NULL);

// update now time after wait
clock_gettime(CLOCK_MONOTONIC, &current_timespec);
current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
DeltaTime = (float)(current_time - g_Time);
}

g_Time = current_time;

MainLoopStep();
MouseReset(&mouse);
}
Expand Down
Binary file modified FlappyBird/app/src/main/libs/arm64-v8a/libflappybird.so
Binary file not shown.
Binary file modified FlappyBird/app/src/main/libs/armeabi-v7a/libflappybird.so
Binary file not shown.

0 comments on commit 24166ab

Please sign in to comment.