From fa68923c6a941d8d55a3f06210e3483417c119c7 Mon Sep 17 00:00:00 2001 From: Victor Gamper Date: Tue, 4 Feb 2020 17:43:56 +0100 Subject: Fixed timings after I confused milli- and microseconds --- .cproject | 52 +++++++++++++++++++++- .gitignore | 1 + .settings/language.settings.xml | 11 +++++ .../org.eclipse.cdt.managedbuilder.core.prefs | 10 +++++ PidginImage.c | 2 +- catch.c | 46 ++++++++++++++----- catch.h | 11 ++--- 7 files changed, 116 insertions(+), 17 deletions(-) diff --git a/.cproject b/.cproject index f68fb8e..752e3ae 100644 --- a/.cproject +++ b/.cproject @@ -178,7 +178,7 @@ @@ -199,6 +199,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 2779131..324180f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /Debug /Release32 /Windows +/Windows32 /Release diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index fba9e44..0361b61 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -44,4 +44,15 @@ + + + + + + + + + + + diff --git a/.settings/org.eclipse.cdt.managedbuilder.core.prefs b/.settings/org.eclipse.cdt.managedbuilder.core.prefs index e6fd9f9..f8fa1aa 100644 --- a/.settings/org.eclipse.cdt.managedbuilder.core.prefs +++ b/.settings/org.eclipse.cdt.managedbuilder.core.prefs @@ -1,10 +1,20 @@ eclipse.preferences.version=1 +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/CPATH/delimiter=\: +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/CPATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/C_INCLUDE_PATH/delimiter=\: +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/C_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/append=true +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/appendContributed=true environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/CPATH/delimiter=\: environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/CPATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/C_INCLUDE_PATH/delimiter=\: environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/C_INCLUDE_PATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/append=true environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/appendContributed=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/LIBRARY_PATH/delimiter=\: +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/LIBRARY_PATH/operation=remove +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/append=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039.1368829244/appendContributed=true environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/LIBRARY_PATH/delimiter=\: environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/LIBRARY_PATH/operation=remove environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.exe.release.1532734451.937370039/append=true diff --git a/PidginImage.c b/PidginImage.c index 791926d..9cd0b39 100644 --- a/PidginImage.c +++ b/PidginImage.c @@ -34,7 +34,7 @@ void draw_Pidgin(SDL_Surface* surface, int x, int y) { void Pidgin_IncrementFrame() { delay++; //printf("%d\n", delay); - if(delay >= 50) { + if(delay >= 10) { delay = 0; animation_index+=ax; sdl_rect_source.y = animation_index * PIDGIN_HEIGHT; diff --git a/catch.c b/catch.c index 667a5b6..dc40c9f 100644 --- a/catch.c +++ b/catch.c @@ -19,7 +19,9 @@ SDL_Surface* image_chargebar, *image_chargebar_dark, *image_cat, *image_bird; struct Cat cat; struct Bird bird[BIRD_COUNT]; -int state, menu_state, score, time_left, ticks_to_next_second; +unsigned int state, menu_state, score; +int ticks_to_next_second, time_left; +unsigned long last_delta_time; int main(int argc, char* argv[]) { memset(keyPressed,0,sizeof(keyPressed)); @@ -60,6 +62,8 @@ int main(int argc, char* argv[]) { run = true; state = 1; while(run) { + + // get the time long deltaTime = clock(); // process all sdl events @@ -200,6 +204,17 @@ int main(int argc, char* argv[]) { Font_DrawString(screen, screen->w /2 - length * 4, 180, score_s); } + // TODO draw the fps + /* + char fps_s[50]; // TODO this could create a buffer overflow + if(last_delta_time <= 0) + last_delta_time = 1; + printf("%d\n", last_delta_time); + sprintf(fps_s, "FPS: %ld", 1000000000 / last_delta_time); + int length = strlen(fps_s) + 1; + Font_DrawString(screen, screen->w - length * 8, 24, fps_s); + */ + //Font_DrawString(screen, 0,5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstufwxyz\n 01234567890: +"); Font_DrawString(screen, screen->h-10, 5, ""); @@ -207,12 +222,23 @@ int main(int argc, char* argv[]) { SDL_Flip(screen); // get the delta time - deltaTime = clock() - deltaTime; - deltaTime /= CLOCKS_PER_SEC / 1000; - deltaTime = TICK_SPEED - deltaTime; - // wait 50 milliseconds - if(deltaTime>0) + deltaTime = clock()-deltaTime; + + // get the time in microseconds + deltaTime /= CLOCKS_PER_SEC/1000000; + last_delta_time = deltaTime; + + // calculate the time until the next frame + deltaTime = TICK_SPEED * 1000 - deltaTime; + + // if it is higher than 0, sleep + if(deltaTime > 0) usleep(deltaTime); + //deltaTime = TICK_SPEED - deltaTime; + ///printf("%d\n", deltaTime); + // wait 50 milliseconds + + } // dispose the surfaces @@ -231,7 +257,7 @@ void gameRoutine() { // do the physics calculation if(cat.jumping) { - cat.downwardForce+=0.01; + cat.downwardForce+=0.1; cat.y+=cat.downwardForce; // the cat has touched the floor @@ -248,7 +274,7 @@ void gameRoutine() { } else if(charge) { if(cat.downwardForce > -MAXIMUM_JUMP_FORCE) - cat.downwardForce-=0.02; + cat.downwardForce-=0.2; } } @@ -299,7 +325,7 @@ void gameRoutine() { // if the selected bird exists, move it if(bird[i].type == BIRD_TYPE_PIDGIN) { - bird[i].x+=0.5; + bird[i].x+= 1; // if the bird left the screen, reset it if(bird[i].x > screen->w) { @@ -315,7 +341,7 @@ void gameRoutine() { } } else if(bird[i].type == BIRD_TYPE_DEAD_PIDGIN) { - bird[i].y+=0.5; + bird[i].y+= 2; if(bird[i].y > screen->h-FLOOR_HEIGHT) { bird[i].type = BIRD_TYPE_NONE; diff --git a/catch.h b/catch.h index 936e6b2..1f2dd36 100644 --- a/catch.h +++ b/catch.h @@ -27,9 +27,9 @@ #define CHARGEBAR_POS_X 10 #define CHARGEBAR_POS_Y 10 -#define MAXIMUM_JUMP_FORCE 3 +#define MAXIMUM_JUMP_FORCE 9 -#define BIRD_COUNT 3 +#define BIRD_COUNT 5 #define BIRD_TYPE_NONE 0 #define BIRD_TYPE_PIDGIN 1 #define BIRD_TYPE_DEAD_PIDGIN 2 @@ -37,10 +37,10 @@ #define STATE_MAIN_MENU 1 #define STATE_GAME 2 #define STATE_GAME_OVER 3 -#define GAME_TIME 10 +#define GAME_TIME 60 -#define TICK_SPEED 100 -#define TICKS_PER_SECOND 10000 / TICK_SPEED +#define TICK_SPEED 20 +#define TICKS_PER_SECOND 1000 / TICK_SPEED // cat and bird structures struct Cat { @@ -58,6 +58,7 @@ struct Bird { // function predefines extern void gameRoutine(); extern bool checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); +extern int getRandomInt(int limit); // BitmapFontRenderer.c extern void Font_Init(); -- cgit v1.2.3