flappychik

Silly SDL2 game
git clone git://bsandro.tech/flappychik
Log | Files | Refs

commit b2c903946a4853d4f92408f0ad1695e085539559
parent 30132ce7542e0bf1cabc8d7ae925edd0c89d0d19
Author: bsandro <brian.drosan@gmail.com>
Date:   Wed, 16 Mar 2022 01:45:07 +0200

small cleanup (frame processing is a separate function now)

Diffstat:
Mmain.c | 53+++++++++++++++++++++++++++++++++--------------------
1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/main.c b/main.c @@ -11,8 +11,10 @@ #define WINDOW_FLAGS SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI #endif -#define FPS 60 -#define SPEED 80 // pixels per second +#define GAME_WIN_WIDTH 640 +#define GAME_WIN_HEIGHT 480 +#define GAME_FPS 60 +#define GAME_SPEED 80 // pixels per second struct sprite_t { SDL_Texture *texture; @@ -29,6 +31,23 @@ struct game_t { struct sprite_t ship; }; +void process_frame(uint64_t ts, uint64_t ftime, struct game_t *game) { + int offset = round(ftime * GAME_SPEED / 1000.0f); + //printf("offset: %d\n", offset); + + game->ship.rect.x += offset; + game->ship.rect.y += offset; + game->ship.angle = ++game->ship.angle % 360; + + SDL_RenderClear(game->renderer); + SDL_RenderCopy(game->renderer, game->bg.texture, NULL, NULL); + SDL_RenderCopyEx(game->renderer, game->ship.texture, NULL, &game->ship.rect, game->ship.angle, NULL, SDL_FLIP_NONE); + SDL_RenderPresent(game->renderer); + + //printf("frame time: %llu\n", ftime); + game->last_frame = ts; +} + int main(int argc, char *argv[]) { (void)argc; (void)argv; @@ -39,7 +58,7 @@ int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO); - game.screen = SDL_CreateWindow("flappychik", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, WINDOW_FLAGS); + game.screen = SDL_CreateWindow("flappychik", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, GAME_WIN_WIDTH, GAME_WIN_HEIGHT, WINDOW_FLAGS); assert(game.screen != NULL); game.renderer = SDL_CreateRenderer(game.screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); assert(game.renderer != NULL); @@ -64,36 +83,30 @@ int main(int argc, char *argv[]) { SDL_RenderPresent(game.renderer); game.last_frame = SDL_GetTicks64(); + // main game loop while (!game.over) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: game.over = true; break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_q) { // quit if "q" button is pressed + game.over = true; + } + break; } } - uint64_t t = SDL_GetTicks64(); - uint64_t ftime = t - game.last_frame; - - if (ftime >= 1000/FPS) { - int offset = round(ftime * SPEED / 1000.0f); - printf("offset: %d\n", offset); - - game.ship.rect.x += offset; - game.ship.rect.y += offset; - game.ship.angle = ++game.ship.angle % 360; - - SDL_RenderClear(game.renderer); - SDL_RenderCopy(game.renderer, game.bg.texture, NULL, NULL); - SDL_RenderCopyEx(game.renderer, game.ship.texture, NULL, &game.ship.rect, game.ship.angle, NULL, SDL_FLIP_NONE); - SDL_RenderPresent(game.renderer); + uint64_t ts = SDL_GetTicks64(); + uint64_t ftime = ts - game.last_frame; - printf("frame time: %llu\n", ftime); - game.last_frame = t; + if (ftime >= 1000/GAME_FPS) { + process_frame(ts, ftime, &game); } } + // cleanup SDL_DestroyTexture(game.bg.texture); SDL_DestroyTexture(game.ship.texture); SDL_DestroyRenderer(game.renderer);