Remove global variables.

Global variables are overall bad practice.
This commit is contained in:
Nicolás Ortega Froysa 2018-03-25 17:14:44 +02:00
parent 0faa83c162
commit 80113d51ff
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
6 changed files with 21 additions and 15 deletions

View File

@ -21,11 +21,9 @@
#ifdef DEBUG
# include <stdio.h>
#endif
#include <allegro5/allegro.h>
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.");

View File

@ -18,17 +18,23 @@
#pragma once
#include <allegro5/allegro.h>
/**
* @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);

View File

@ -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

View File

@ -18,6 +18,8 @@
#pragma once
#include <allegro5/allegro.h>
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.

View File

@ -28,8 +28,6 @@
#include <allegro5/allegro.h>
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.

View File

@ -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.");