fixed bug with game over on machines where nanosec counter fills up quicky

This commit is contained in:
rgimad
2025-01-09 23:11:31 +03:00
parent 56c58c0130
commit 5fc17f6a00
5 changed files with 21 additions and 7 deletions

BIN
dino

Binary file not shown.

4
main.c
View File

@@ -111,6 +111,10 @@ int main(int argc, char* args[]) {
// dbg_printf("frameTime = %d\n", frameTime);
if (frameTime < FRAME_TIME) { // 1000ms/60frames = 16.(6)
// printf("frameTime = %d\n", frameTime);
if (runner.crashed) {
// dbg_printf("runner.timeAfterCrashedMs +=\n");
runner.timeAfterCrashedMs += FRAME_TIME - frameTime;
}
graphicsDelay(FRAME_TIME - frameTime);
}
}

4
misc.c
View File

@@ -22,5 +22,7 @@ void intToStr(int num, int ndigits, char* result) {
}
int getTimeStamp() { // in ms
return (_ksys_get_ns_count()/1000000);
uint64_t x = 0;
x = _ksys_get_ns_count();
return (x/1000000);
}

View File

@@ -13,6 +13,7 @@ void runnerInit() {
runner.activated = false;
runner.playing = false;
runner.crashed = false;
runner.timeAfterCrashedMs = 0;
runner.paused = false;
runner.inverted = false;
runner.playingIntro = false;
@@ -83,12 +84,17 @@ void runnerOnKeyUp(int key) {
}
else if (runner.crashed) {
// Check that enough time has elapsed before allowing jump key to restart.
int deltaTime = getTimeStamp() - runner.time;
if (deltaTime < 0) {
deltaTime = DELTA_MS_DEFAULT;
}
//printf(".deltaTime = %d\n", deltaTime);
if (key == RUNNER_KEYCODE_RESTART || (deltaTime >= RUNNER_GAMEOVER_CLEAR_TIME && (key == RUNNER_KEYCODE_JUMP_1 || key == RUNNER_KEYCODE_JUMP_2))) {
int now = getTimeStamp();
int deltaTime = now - runner.time;
// if (deltaTime < 0) {
// deltaTime = DELTA_MS_DEFAULT;
// runner.time = 0;
// }
// dbg_printf(".now = %d .deltaTime = %d runner.time = %d\n", now, deltaTime, runner.time);
if (key == RUNNER_KEYCODE_RESTART || (runner.timeAfterCrashedMs >= RUNNER_GAMEOVER_CLEAR_TIME && (key == RUNNER_KEYCODE_JUMP_1 || key == RUNNER_KEYCODE_JUMP_2))) {
//dbg_printf("timeAfterCrashedMs = %d\n", runner.timeAfterCrashedMs);
runnerRestart();
}
}
@@ -235,6 +241,7 @@ void runnerRestart() {
runner.runningTime = 0;
runner.playing = true;
runner.crashed = false;
runner.timeAfterCrashedMs = 0;
runner.distanceRan = 0;
runner.currentSpeed = RUNNER_SPEED;
runner.time = getTimeStamp();

View File

@@ -52,6 +52,7 @@ typedef struct {
bool activated;
bool playing;
bool crashed;
int timeAfterCrashedMs;
bool paused;
bool inverted;
bool invertTimer;