flappychik

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

commit 30132ce7542e0bf1cabc8d7ae925edd0c89d0d19
parent 66c8cc71ce87445e32ec68cb214177a5081b2365
Author: bsandro <brian.drosan@gmail.com>
Date:   Tue, 15 Feb 2022 00:58:29 +0200

game_t, sprite_t structs

Diffstat:
Mmain.c | 91+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 54 insertions(+), 37 deletions(-)

diff --git a/main.c b/main.c @@ -14,73 +14,90 @@ #define FPS 60 #define SPEED 80 // pixels per second +struct sprite_t { + SDL_Texture *texture; + SDL_Rect rect; + int angle; +}; + +struct game_t { + bool over; + SDL_Window *screen; + SDL_Renderer *renderer; + uint64_t last_frame; + struct sprite_t bg; + struct sprite_t ship; +}; + int main(int argc, char *argv[]) { (void)argc; (void)argv; - bool quit = false; SDL_Event event; + struct game_t game = {0}; + game.over = false; SDL_Init(SDL_INIT_VIDEO); - SDL_Window *screen = SDL_CreateWindow("flappychik", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, WINDOW_FLAGS); - - assert(screen != NULL); - SDL_Renderer *renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - assert(renderer != NULL); + game.screen = SDL_CreateWindow("flappychik", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, WINDOW_FLAGS); + assert(game.screen != NULL); + game.renderer = SDL_CreateRenderer(game.screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + assert(game.renderer != NULL); - SDL_Surface *bg = IMG_Load("assets/kohina.jpg"); - assert(bg != NULL); + SDL_Surface *bg_img = IMG_Load("assets/kohina.jpg"); + assert(bg_img != NULL); + SDL_Surface *knife_img = IMG_Load("assets/chikaiKnife.png"); // 56x56px + assert(knife_img != NULL); - SDL_Surface *knife = IMG_Load("assets/chikaiKnife.png"); // 56x56px - assert(knife != NULL); + game.bg.texture = SDL_CreateTextureFromSurface(game.renderer, bg_img); + assert(game.bg.texture != NULL); + game.ship.texture = SDL_CreateTextureFromSurface(game.renderer, knife_img); + assert(game.ship.texture != NULL); - SDL_Texture *tex_bg = SDL_CreateTextureFromSurface(renderer, bg); - SDL_Texture *tex_knife = SDL_CreateTextureFromSurface(renderer, knife); - SDL_FreeSurface(knife); - SDL_FreeSurface(bg); + SDL_FreeSurface(knife_img); + SDL_FreeSurface(bg_img); - SDL_RenderCopy(renderer, tex_bg, NULL, NULL); - SDL_Rect knife_rect = {0, 0, 56, 56}; - int knife_angle = 0; - SDL_RenderCopy(renderer, tex_knife, NULL, &knife_rect); + game.ship.rect.w = 56; + game.ship.rect.h = 56; + game.ship.angle = 0; - SDL_RenderPresent(renderer); + SDL_RenderPresent(game.renderer); - uint64_t t = SDL_GetTicks64(); - while (!quit) { + game.last_frame = SDL_GetTicks64(); + while (!game.over) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - quit = true; + game.over = true; break; } } - uint64_t t1 = SDL_GetTicks64(); + uint64_t t = SDL_GetTicks64(); + uint64_t ftime = t - game.last_frame; - if (t1 - t >= 1000/FPS) { - SDL_RenderClear(renderer); - uint64_t ftime = t1 - t; + if (ftime >= 1000/FPS) { int offset = round(ftime * SPEED / 1000.0f); printf("offset: %d\n", offset); - knife_rect.x += offset; - knife_rect.y += offset; - knife_angle += 1; - knife_angle %= 360; - SDL_RenderCopy(renderer, tex_bg, NULL, NULL); - SDL_RenderCopyEx(renderer, tex_knife, NULL, &knife_rect, knife_angle, NULL, SDL_FLIP_NONE); - SDL_RenderPresent(renderer); + 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); - t = t1; + game.last_frame = t; } } - SDL_DestroyTexture(tex_bg); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(screen); + SDL_DestroyTexture(game.bg.texture); + SDL_DestroyTexture(game.ship.texture); + SDL_DestroyRenderer(game.renderer); + SDL_DestroyWindow(game.screen); SDL_Quit(); printf("bye!\n");