From 9116be1f3b96c491d1bfd3ea1e20c88f0b104894 Mon Sep 17 00:00:00 2001 From: Fabio Maciel <6810827+fabiomaciel@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:49:17 -0300 Subject: refactor: extract screen management from main into a module --- src/main.c | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index f9b89bf..9a57654 100644 --- a/src/main.c +++ b/src/main.c @@ -36,9 +36,7 @@ #include "controller.h" #include "player.h" - -const int SCREEN_WIDTH = 640; -const int SCREEN_HEIGHT = 480; +#include "screen.h" int main (int argc, @@ -54,30 +52,13 @@ main (int argc, return EXIT_FAILURE; } - SDL_Window* window = SDL_CreateWindow( - "Blast Attack", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, SCREEN_HEIGHT, - SDL_WINDOW_RESIZABLE - ); - if (window == NULL) { - fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); - return EXIT_FAILURE; - } + screen_t screen; - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - if (renderer == NULL) { - fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); + if(screen_init(&screen) == false){ return EXIT_FAILURE; } - if (SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0) { - fprintf(stderr, "Could not set logical size: %s\n", SDL_GetError()); - return EXIT_FAILURE; - } - - SDL_Rect bg_rect = { .x = 0, .y = 0, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT }; + SDL_Rect bg_rect = { .x = 0, .y = 0, .w = screen.width, .h = screen.height }; bool quit = false; controller_t ctrl = {0}; @@ -85,10 +66,10 @@ main (int argc, player_t player; player_init(&player); + SDL_Renderer *renderer = screen.renderer; while (!quit) { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 1); - SDL_RenderClear(renderer); + screen_reset(&screen); uint64_t start = SDL_GetPerformanceCounter(); SDL_SetRenderDrawColor(renderer, 0XCC, 0XCC, 0XCC, 1); @@ -102,7 +83,7 @@ main (int argc, controller_update(&ctrl, &event); } - player_update(&player, &ctrl, SCREEN_WIDTH, SCREEN_HEIGHT); + player_update(&player, &ctrl, screen.width, screen.height); player_draw(&player, renderer); SDL_RenderPresent(renderer); @@ -113,8 +94,8 @@ main (int argc, SDL_Delay(floor(16.666f - elapsedMS)); } - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); + screen_destroy(&screen); SDL_Quit(); + return EXIT_SUCCESS; } -- cgit v1.2.3