summaryrefslogtreecommitdiff
path: root/src/main.c
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 /src/main.c
parentd3d1da3472a93b711ca22cc955b15c9f40c20e06 (diff)
refactor: extract screen management from main into a module
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c37
1 files changed, 9 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;
}