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 +++++++------------------- src/screen.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/screen.h | 51 +++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 src/screen.c create mode 100644 src/screen.h 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; } diff --git a/src/screen.c b/src/screen.c new file mode 100644 index 0000000..52dec55 --- /dev/null +++ b/src/screen.c @@ -0,0 +1,87 @@ +/* +* Copyright (c) 2021, Fabio Maciel +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* 3. Neither the name of the copyright holder nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "screen.h" + +#include +#include +#include + +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +bool +screen_init(screen_t* screen) +{ + assert(screen && "screen is required"); + + screen->width = SCREEN_WIDTH; + screen->height = SCREEN_HEIGHT; + + screen->window = SDL_CreateWindow( + "Blast Attack", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + screen->width, screen->height, + SDL_WINDOW_RESIZABLE + ); + + if (screen->window == NULL) { + fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return false; + } + + screen->renderer = SDL_CreateRenderer(screen->window, -1, SDL_RENDERER_ACCELERATED); + if (screen->renderer == NULL) { + fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); + return false; + } + + if (SDL_RenderSetLogicalSize(screen->renderer, screen->width, screen->height) < 0) { + fprintf(stderr, "Could not set logical size: %s\n", SDL_GetError()); + return false; + } + + return true; +} + +void +screen_reset(screen_t *screen) +{ + SDL_SetRenderDrawColor(screen->renderer, 0, 0, 0, 1); + SDL_RenderClear(screen->renderer); +} + +void +screen_destroy(screen_t *screen) +{ + SDL_DestroyRenderer(screen->renderer); + SDL_DestroyWindow(screen->window); +} diff --git a/src/screen.h b/src/screen.h new file mode 100644 index 0000000..15ba91e --- /dev/null +++ b/src/screen.h @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2021, Fabio Maciel +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* 3. Neither the name of the copyright holder nor the names of its +* contributors may be used to endorse or promote products derived from +* this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef SCREEN_H +#define SCREEN_H + +#include +#include + +#include + +typedef struct screen { + uint16_t width; + uint16_t height; + SDL_Renderer* renderer; + SDL_Window* window +} screen_t; + +bool screen_init(screen_t *screen); + +void screen_reset(screen_t *screen); + +void screen_destroy(screen_t *screen); + +#endif /* SCREEN_H */ -- cgit v1.2.3