From 9dadbc4409e2ce32eee80c7a8a6ee93e2524763b Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 03:34:03 +0200 Subject: player: Introduce player object --- src/main.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 70b283d..f9b89bf 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ #include #include "controller.h" +#include "player.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; @@ -76,12 +77,14 @@ main (int argc, return EXIT_FAILURE; } - SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 }; SDL_Rect bg_rect = { .x = 0, .y = 0, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT }; bool quit = false; controller_t ctrl = {0}; + player_t player; + player_init(&player); + while (!quit) { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 1); SDL_RenderClear(renderer); @@ -99,28 +102,9 @@ main (int argc, controller_update(&ctrl, &event); } - if (controller_is_up_pressed(&ctrl) && rect.y > 0) { - rect.y -= 8; - rect.y = rect.y < 0 ? 0 : rect.y; - } - - if (controller_is_down_pressed(&ctrl) && rect.y + 8 < SCREEN_HEIGHT) { - rect.y += 8; - rect.y = rect.y + rect.h > SCREEN_HEIGHT ? SCREEN_HEIGHT - rect.h : rect.y; - } - - if (controller_is_left_pressed(&ctrl) && rect.x > 0) { - rect.x -= 8; - rect.x = rect.x < 0 ? 0 : rect.x; - } - - if (controller_is_right_pressed(&ctrl) && rect.x + 8 < SCREEN_WIDTH) { - rect.x += 8; - rect.x = rect.x + rect.h > SCREEN_WIDTH ? SCREEN_WIDTH - rect.h : rect.x; - } + player_update(&player, &ctrl, SCREEN_WIDTH, SCREEN_HEIGHT); + player_draw(&player, renderer); - SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1); - SDL_RenderFillRect(renderer, &rect); SDL_RenderPresent(renderer); uint64_t end = SDL_GetPerformanceCounter(); -- cgit v1.2.3