summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Maciel <6810827+fabiomaciel@users.noreply.github.com>2022-12-12 18:49:17 -0300
committerFabio Maciel <6810827+fabiomaciel@users.noreply.github.com>2022-12-13 10:10:15 -0300
commit9116be1f3b96c491d1bfd3ea1e20c88f0b104894 (patch)
tree0a6bd1e6df2da2ff2dd874b82a1affa82aa7a8fc
parentd3d1da3472a93b711ca22cc955b15c9f40c20e06 (diff)
refactor: extract screen management from main into a module
-rw-r--r--src/main.c37
-rw-r--r--src/screen.c87
-rw-r--r--src/screen.h51
3 files changed, 147 insertions, 28 deletions
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 <stdint.h>
+#include <assert.h>
+#include <SDL.h>
+
+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 <stdint.h>
+#include <stdbool.h>
+
+#include <SDL.h>
+
+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 */