fixed bug with freeze/UB caused by starting timestamp from 0. now use default delay in this case (quite rare). also experimented with bad timestamp( % 100 in get timestamp) - playable

This commit is contained in:
rgimad
2025-01-07 17:54:44 +03:00
parent 8189fcdd15
commit 56c58c0130
6 changed files with 18 additions and 4 deletions

View File

@@ -4,6 +4,8 @@
#define DEFAULT_WIDTH 600
#define FPS 60
#define DELTA_MS_DEFAULT 20
// #define DBG
#ifdef DBG

BIN
dino

Binary file not shown.

3
main.c
View File

@@ -104,6 +104,9 @@ int main(int argc, char* args[]) {
}
int frameTime = getTimeStamp() - frameStartTime;
if (frameTime < 0) {
frameTime = DELTA_MS_DEFAULT;
}
#define FRAME_TIME 20 //16
// dbg_printf("frameTime = %d\n", frameTime);
if (frameTime < FRAME_TIME) { // 1000ms/60frames = 16.(6)

4
misc.c
View File

@@ -21,6 +21,6 @@ void intToStr(int num, int ndigits, char* result) {
}
}
int getTimeStamp() {
return _ksys_get_ns_count()/1000000;
int getTimeStamp() { // in ms
return (_ksys_get_ns_count()/1000000);
}

View File

@@ -84,6 +84,9 @@ 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))) {
runnerRestart();
@@ -101,12 +104,15 @@ void runnerClearCanvas() {
}
void runnerUpdate() {
//printf("runnerUpdate() runner.playing = %d\n", runner.playing);
//dbg_printf("runnerUpdate() runner.playing = %d\n", runner.playing);
//runner.updatePending = false;
int now = getTimeStamp();
//printf("now = %d\n", now);
int deltaTime = now - (runner.time ? runner.time : 0);
//printf("runnerUpdate() deltaTime = %d\n", deltaTime);
if (deltaTime < 0) {
deltaTime = DELTA_MS_DEFAULT;
}
// dbg_printf("runnerUpdate() deltaTime = %d\n", deltaTime);
runner.time = now;
if (runner.playing) {
//printf("runnerUpdate() %d\n", getTimeStamp());

3
trex.c
View File

@@ -127,6 +127,9 @@ void trexSetBlinkDelay() {
void trexBlink(int time) {
//printf("trexBlink(%d)\n", time);
int deltaTime = time - trex.animStartTime;
if (deltaTime < 0) {
deltaTime = DELTA_MS_DEFAULT;
}
if (deltaTime >= trex.blinkDelay) {
trexDraw(trex.currentAnimFrames.frames[trex.currentFrame], 0);
if (trex.currentFrame == 1) {