- Online Community Forums » Programming Questions » Issues passing the Display to functions...

This thread is locked; no one can reply to it. rss feed Print
Issues passing the Display to functions...
Member #14,667
October 2012

Basically I am cloning Asteroids in C but I want separate functions for each part of the program so I would have functions like:


and these functions would open subfunctions.

I am trying to pass the display to the functions and allegro is giving me errors.
So my main function looks like this:

1int main(int argc, char **argv){ 2 int gamestate; 3 gamestate = loadallegro(); 4 //CREATE A POINTER TO A DISPLAY (A WINDOW WITH A BITMAP BUFFER) 5 ALLEGRO_DISPLAY *display = NULL; 6 //this program will no longer shutdown the window as in previous projects. The project shall work from the same window. 7 gamestate = initializedisplay(display);//INITIALIZE DISPLAY 8 gamestate = displayintro(display); 9 gamestate = mainmenu(display); 10 al_destroy_display(display); 11 return 0; 12}

Any function I pass the display to looks like this:

1//prototype 2int displayintro(ALLEGRO_DISPLAY * const display); 3 4int displayintro(ALLEGRO_DISPLAY * const display) 5{ 6 // code... 7 return 0; 8}

This issue is when the function is modified by a second function such as the game menu.
This code crashes the program:

1int mainmenu(ALLEGRO_DISPLAY * display) 2{ 3 //other code 4 al_register_event_source(event_queue, al_get_display_event_source(display)); 5 if (DEBUG == true) puts("Display Queue"); 6//other code 7}

So I do what I don't want to do, which is reset the display.

1int mainmenu(ALLEGRO_DISPLAY * display) 2{ 3//other code 4 al_destroy_display(display); 5 al_set_new_display_flags(DEFAULTWINDOWCONFIG); 6 display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT); 7 al_register_event_source(event_queue, al_get_display_event_source(display)); 8 if (DEBUG == true) puts("Display Queue"); 9// other code 10}

But then the title screen's display doesn't get destroyed by my main menu function and I end up with two display windows.

What is going on?

l j
Member #10,584
January 2009

Well you pass a copy of the pointer of your display to your function.

Then you create a new window and assign it's address to this copy of the pointer.
And then after the function returns you throw away this pointer.

You can hower pass a pointer to a pointer or a reference to a pointer.

int initializedisplay(ALLEGRO_DISPLAY *&display)
    // code

Called like


int initializedisplay(ALLEGRO_DISPLAY **display)
    // code

Which is called like this

Not using one of these ways will merely pass a copy of the pointer.
This is not a problem unless you are trying to create a new display as this modifies the address the pointer points to.

I'm assuming you create the display inside of initializedisplay.
After calling this function display will still be null inside your main function.

Member #14,667
October 2012

This works taron thanks!

I am able to pass the display to different functions without having to renew the display.

But I am having a new problem. For some reason this function doesn't want to work to reset the display. Its required if the user wants to switch between full and windowed screens during the game. What happens is the function crashes the program. Strangely, I am able to reset the display using the same code in other functions. I don't have any active event handlers on the display when I destroy the display.

1//restarts the display with new values 2int resetdisplay(ALLEGRO_DISPLAY *&display, gameconfiguration * const Lgameconfiguration) 3{ 4 al_destroy_display(display); 5 al_set_window_title(display, "Asteriods"); 6 al_set_new_display_flags(Lgameconfiguration[0].WindowFlag); 7 display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT); 8 al_clear_to_color(al_map_rgb(0,0,0)); 9 if(!display && DEBUG == true) { 10 fprintf(stderr, "failed to create display!\n"); 11 return -1; 12 } 13 al_hide_mouse_cursor(display); //HIDES CURSOR ON SCREEN 14 al_flip_display(); 15 return 0; 16}

It was this line of code which was causing it to crash inside the function:

1 al_set_window_title(display, "Asteriods");

Member #7,536
July 2006

You seem to lack a basic understanding of programming concepts. I would recommend that you go back to basics. You're destroying the display and then trying to manipulate it. It is destroyed. After that it doesn't exist anymore. You therefore cannot manipulate it. ::)

Go to: