From 93d77661277962bb75dfc3f22a7282ff0f11c2f9 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Tue, 22 Jan 2008 23:22:12 +0100 Subject: [PATCH] Add FPS limiting code; fix touchpad movement Add code to limit rendering to 33 FPS (should make the CPU a bit happier, after all); this can be disabled. Fix touchpad movement to be more exact in input_human. Remove the obsolete BALL_DEFAULT_SPEED define in game.h --- game.c | 23 ++++++++++++++++++++--- game.h | 5 ++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/game.c b/game.c index 4ae6a1b..826bea5 100644 --- a/game.c +++ b/game.c @@ -71,6 +71,10 @@ void game( bool singleplayer) { bool quit = false; int x, y, z; +#ifdef ENABLE_FPS_LIMIT + Uint32 ft, frames; /* frame timer and frames */ +#endif + if( singleplayer) { #ifdef DEBUG s.player1.type = PLAYER_TYPE_AI; @@ -88,7 +92,11 @@ void game( bool singleplayer) { } } } - + +#ifdef ENABLE_FPS_LIMIT + frames = 0; + ft = SDL_GetTicks(); +#endif while( !quit) { nt = SDL_GetTicks(); diff = nt-ot; @@ -110,6 +118,13 @@ void game( bool singleplayer) { } } +#ifdef ENABLE_FPS_LIMIT + while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) { + SDL_Delay(10); + } + frames++; +#endif + render( &s); } @@ -450,12 +465,14 @@ void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, if (mb&SDL_BUTTON(SDL_BUTTON_LEFT)) { hit = true; } - if (player->mouse_y < player->y-RACKET_Y_MID-diff) { + if (player->mouse_y < player->y) { down = false; up = true; - } else if (player->mouse_y > player->y-RACKET_Y_MID+diff) { + diff = (player->y-player->mouse_yy-player->mouse_y):(diff); + } else if (player->mouse_y > player->y) { up = false; down = true; + diff = (player->mouse_y-player->ymouse_y-player->y):(diff); } } diff --git a/game.h b/game.h index 55ac99e..fcaddb8 100644 --- a/game.h +++ b/game.h @@ -123,7 +123,6 @@ typedef struct { #define BALL_JUMP_MIN 4 #define BALL_JUMP_MAX 10 -#define BALL_DEFAULT_SPEED 3.0 #define RACKET_X_MID 15 #define RACKET_Y_MID 24 @@ -167,6 +166,10 @@ typedef struct { #define ENABLE_MOUSE false +/* Comment out the following #define to disable FPS limiting */ +#define ENABLE_FPS_LIMIT +#define DEFAULT_FPS 33 + void game( bool); void render( GameState*); bool step( GameState*); -- 2.11.4.GIT