|
Issues passing the Display to functions... |
Shinn22
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: Titlescreen and these functions would open subfunctions. I am trying to pass the display to the functions and allegro is giving me errors. 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 // ...my code...
7 return 0;
8}
This issue is when the function is modified by a second function such as the game menu. 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. You can hower pass a pointer to a pointer or a reference to a pointer. int initializedisplay(ALLEGRO_DISPLAY *&display) { // code }
Called like Or 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. I'm assuming you create the display inside of initializedisplay.
|
Shinn22
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}
Edit:
|
bamccaig
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|