From 80113d51ff53f504476ac6cce3b665de099e96e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sun, 25 Mar 2018 17:14:44 +0200 Subject: [PATCH] Remove global variables. Global variables are overall bad practice. --- src/display.c | 8 +++----- src/display.h | 10 ++++++++-- src/event_manager.c | 2 +- src/event_manager.h | 4 +++- src/globals.h | 2 -- src/main.c | 10 ++++++---- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/display.c b/src/display.c index 1b5863b..5969c79 100644 --- a/src/display.c +++ b/src/display.c @@ -21,11 +21,9 @@ #ifdef DEBUG # include #endif -#include -ALLEGRO_DISPLAY *display; - -int create_display(unsigned int width, unsigned int height) { +int create_display(ALLEGRO_DISPLAY *display, + unsigned int width, unsigned int height) { display = al_create_display(width, height); if(!display) return 0; @@ -35,7 +33,7 @@ int create_display(unsigned int width, unsigned int height) { return 1; } -void destroy_display() { +void destroy_display(ALLEGRO_DISPLAY *display) { al_destroy_display(display); #ifdef DEBUG puts("Destroyed display."); diff --git a/src/display.h b/src/display.h index e1db860..143f924 100644 --- a/src/display.h +++ b/src/display.h @@ -18,17 +18,23 @@ #pragma once +#include + /** * @brief Create an allegro 5 display with a given width and height. * + * @param display The display to create. * @param width The width of the display. * @param height The height of the display. * * @return If successful it will return 1, else it will return 0. */ -int create_display(unsigned int width, unsigned int height); +int create_display(ALLEGRO_DISPLAY *display, + unsigned int width, unsigned int height); /** * @brief Destroy the display (used for shutdown). + * + * @param display The display to destroy. */ -void destroy_display(); +void destroy_display(ALLEGRO_DISPLAY *display); diff --git a/src/event_manager.c b/src/event_manager.c index 1bbdd85..3a4f4d7 100644 --- a/src/event_manager.c +++ b/src/event_manager.c @@ -29,7 +29,7 @@ static ALLEGRO_EVENT_QUEUE *event_queue; static ALLEGRO_TIMER *timer; static int keys[KEY_MAX]; -int evnt_mngr_init() { +int evnt_mngr_init(ALLEGRO_DISPLAY *display) { if(!al_install_keyboard()) return 0; #ifdef DEBUG diff --git a/src/event_manager.h b/src/event_manager.h index 13c5fb1..e1d8c32 100644 --- a/src/event_manager.h +++ b/src/event_manager.h @@ -18,6 +18,8 @@ #pragma once +#include + enum { KEY_UP = 0x0, KEY_DOWN = 0x1, @@ -33,7 +35,7 @@ enum { * * @return 0 upon failure, 1 upon success. */ -int evnt_mngr_init(); +int evnt_mngr_init(ALLEGRO_DISPLAY *display); /** * @brief Deinitialize the event handler. diff --git a/src/globals.h b/src/globals.h index e2393ab..b32a708 100644 --- a/src/globals.h +++ b/src/globals.h @@ -28,8 +28,6 @@ #include -extern ALLEGRO_DISPLAY *display; ///< The allegro display. - extern int redraw; ///< Whether or not to redraw the screen. extern int run; ///< Whether or not to continue running the simulation. extern int show_help; ///< Whether or not to show the help info. diff --git a/src/main.c b/src/main.c index 2d45183..21e07da 100644 --- a/src/main.c +++ b/src/main.c @@ -43,16 +43,18 @@ int main() { fprintf(stderr, "alleg5: failed to initialize primitives addon.\n"); return 1; } - if(!create_display(800, 600)) + + ALLEGRO_DISPLAY *display; + if(!create_display(display, 800, 600)) { fprintf(stderr, "alleg5: failed to create display.\n"); return 1; } - if(!evnt_mngr_init()) + if(!evnt_mngr_init(display)) { fprintf(stderr, "alleg5: failed to initialize event queue.\n"); - destroy_display(); + destroy_display(display); return 1; } @@ -95,7 +97,7 @@ int main() { } evnt_mngr_deinit(); - destroy_display(); + destroy_display(display); al_shutdown_primitives_addon(); #ifdef DEBUG puts("Shutdown primitives addon.");