Skip to content

Commit

Permalink
update delta time algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimBoev committed Sep 24, 2024
1 parent cfcd66b commit 00b4104
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
Binary file modified FlappyBird/app/build/outputs/apk/FlappyBird-signed.apk
Binary file not shown.
90 changes: 47 additions & 43 deletions FlappyBird/app/src/main/jni/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#include "utils.h"
#include "mouse.h"


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

double g_Time = 0.0;
float DeltaTime = 0.0f;
double g_LastFrameTime = 0.0;


static void handleAppCmd(struct android_app* app, int32_t cmd)
{
switch (cmd)
Expand All @@ -27,32 +36,40 @@ int32_t handle_input(struct android_app* app, AInputEvent* event)
if (eventType == AINPUT_EVENT_TYPE_MOTION)
{
int32_t action = AMotionEvent_getAction(event);
int32_t pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
float x = AMotionEvent_getX(event, pointerIndex);
float y = AMotionEvent_getY(event, pointerIndex);
action &= AMOTION_EVENT_ACTION_MASK;
int whichsource = action >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
size_t pointerCount = AMotionEvent_getPointerCount(event);
float x = 0.0f;
float y = 0.0f;

This comment has been minimized.

Copy link
@karthikreddychereddy

karthikreddychereddy Sep 25, 2024

we can remove redundant calculations

bool isDown = false;
bool isReleased = false;
bool isMoved = false;
int index;

switch (action & AMOTION_EVENT_ACTION_MASK)
for (size_t i = 0; i < pointerCount; ++i)
{
case AMOTION_EVENT_ACTION_DOWN:
case AMOTION_EVENT_ACTION_POINTER_DOWN:
isDown = true;
//Log("Touch down at (%f, %f)", x, y);
break;

case AMOTION_EVENT_ACTION_UP:
case AMOTION_EVENT_ACTION_POINTER_UP:
isReleased = true;
//Log("Touch up at (%f, %f)", x, y);
break;

case AMOTION_EVENT_ACTION_MOVE:
isMoved = true;
//Log("Touch move at (%f, %f)", x, y);
break;
x = AMotionEvent_getX(event, i);
y = AMotionEvent_getY(event, i);
index = AMotionEvent_getPointerId(event, i);

if (action == AMOTION_EVENT_ACTION_POINTER_DOWN || action == AMOTION_EVENT_ACTION_DOWN)
{
int id = index;
if (action == AMOTION_EVENT_ACTION_POINTER_DOWN && id != whichsource) continue;

isDown = true;
}
else if (action == AMOTION_EVENT_ACTION_POINTER_UP || action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL)
{
int id = index;
if (action == AMOTION_EVENT_ACTION_POINTER_UP && id != whichsource) continue;

isReleased = true;
}
else if (action == AMOTION_EVENT_ACTION_MOVE)
{
isMoved = true;
}
}

MouseUpdate(&mouse, x, y, isDown, isReleased, isMoved);
Expand All @@ -66,12 +83,6 @@ int32_t handle_input(struct android_app* app, AInputEvent* event)
return 0; //Return 0 if you are not processing the 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)
{
state->onInputEvent = handle_input;
Expand Down Expand Up @@ -102,28 +113,21 @@ void android_main(struct android_app* state)
struct timespec current_timespec;
clock_gettime(CLOCK_MONOTONIC, &current_timespec);
double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);

// calc delta time
DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);

// fps limit
if (DeltaTime < TARGET_FRAME_TIME)
// checking if enough time has passed for a new frame
if (current_time - g_LastFrameTime >= 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);
MainLoopStep();
MouseReset(&mouse);

// update time last frame
g_LastFrameTime = current_time;
}

g_Time = current_time;

MainLoopStep();
MouseReset(&mouse);
}
}
}
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 00b4104

Please sign in to comment.