From f128345ee3b1d0480c7cf85d5299c396d64e4daa Mon Sep 17 00:00:00 2001 From: Victor Gamper Date: Sat, 8 Feb 2020 12:51:24 +0100 Subject: replaced clock() with SDL_GetTicks() --- .gitignore | 1 + catch.c | 35 +++++++++++++++-------------------- catch.h | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 324180f..7796cbb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /Windows /Windows32 /Release +/c-CATch diff --git a/catch.c b/catch.c index 9bdfee3..0ba86ac 100644 --- a/catch.c +++ b/catch.c @@ -68,7 +68,7 @@ int main(int argc, char* argv[]) { while(run) { // get the time - long deltaTime = clock(); + long deltaTime = SDL_GetTicks(); // process all sdl events SDL_Event event; @@ -208,10 +208,10 @@ int main(int argc, char* argv[]) { Font_DrawString(screen, SCREEN_WIDTH /2 - length * 4, 180, score_s); } - // TODO draw the fps + // draw the fps char fps_s[50]; // TODO this could create a buffer overflow - sprintf(fps_s, "FPS: %i", CLOCKS_PER_SEC / (clock() - last_fps_count_time)); - last_fps_count_time = clock(); + sprintf(fps_s, "FPS: %i", 1000 / (SDL_GetTicks() - last_fps_count_time)); + last_fps_count_time = SDL_GetTicks(); int length = strlen(fps_s) + 1; Font_DrawString(screen, SCREEN_WIDTH - length * 8, 24, fps_s); @@ -222,28 +222,23 @@ int main(int argc, char* argv[]) { SDL_Flip(screen); // get the delta time - deltaTime = clock()-deltaTime; + deltaTime = SDL_GetTicks()-deltaTime; - // get the time in microseconds - if(deltaTime != 0) { - deltaTime /= (CLOCKS_PER_SEC / 1000); + // calculate the time until the next frame + deltaTime = TICK_SPEED - deltaTime; - // calculate the time until the next frame - deltaTime = TICK_SPEED - deltaTime; + // if it is higher than 0, sleep + if(deltaTime > 0) { + unsigned int time_from_sleep_return = SDL_GetTicks(); - // if it is higher than 0, sleep - if(deltaTime > 0) { - unsigned int time_from_sleep_return = clock() / (CLOCKS_PER_SEC / 1000); + while(deltaTime + time_from_sleep_return > SDL_GetTicks()) { - while(deltaTime + time_from_sleep_return > (clock() / (CLOCKS_PER_SEC / 1000))) { + // try to sleep for the left time + unsigned int time = (deltaTime + time_from_sleep_return-SDL_GetTicks()) * 1000; + if(time > 0) + usleep(time); } - } } - //deltaTime = TICK_SPEED - deltaTime; - ///printf("%d\n", deltaTime); - // wait 50 milliseconds - - } // dispose the surfaces diff --git a/catch.h b/catch.h index 73d9710..f8315f1 100644 --- a/catch.h +++ b/catch.h @@ -40,7 +40,7 @@ #define STATE_GAME_OVER 3 #define GAME_TIME 60 -#define TICK_SPEED 18 +#define TICK_SPEED 20 #define TICKS_PER_SECOND 1000 / TICK_SPEED // cat and bird structures -- cgit v1.2.3