Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » two options for display close

This thread is locked; no one can reply to it. rss feed Print
two options for display close
Bater55
Member #16,769
November 2017

Hi!
Can you tell why key_down doesn't close the window? It works a while but I probably I changed something and now it doesn't. I can't find my problem.

#SelectExpand
1int main(int argc, char **argv) 2{ 3 ALLEGRO_DISPLAY *displaystart = NULL; 4 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 5 if (!al_init()) { 6 fprintf(stderr, "failed to initialize allegro!\n"); 7 return -1; 8 } 9 al_init_font_addon(); // initialize the font addon 10 al_init_ttf_addon();// initialize the ttf (True Type Font) addon 11 al_install_keyboard(); 12 13 displaystart = al_create_display(1024, 768); 14 if (!displaystart) { 15 fprintf(stderr, "failed to create displaystart!\n"); 16 return -1; 17 } 18 ALLEGRO_FONT *font = al_load_ttf_font("Artesana.ttf", 64, 0); 19 ALLEGRO_FONT *fonttytul = al_load_ttf_font("ARRIBAAR.TTF", 100, 0); 20 21 if (!font) { 22 fprintf(stderr, "Could not load 'Artesana.ttf'.\n"); 23 return -1; 24 } 25 if (!fonttytul) { 26 fprintf(stderr, "Could not load 'ARRIBAAR.TTF'.\n"); 27 return -1; 28 } 29 event_queue = al_create_event_queue(); 30 if (!event_queue) { 31 fprintf(stderr, "failed to create event_queue!\n"); 32 al_destroy_display(displaystart); 33 return -1; 34 } 35 36 al_register_event_source(event_queue, al_get_display_event_source(displaystart)); 37 al_clear_to_color(al_map_rgb(2, 147, 0)); 38 al_draw_text(fonttytul, al_map_rgb(223, 234, 18), 1024 / 2, 80, ALLEGRO_ALIGN_CENTRE, "Indi w tarapatach!"); 39 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 250, ALLEGRO_ALIGN_CENTRE, "Nowa gra"); 40 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 400, ALLEGRO_ALIGN_CENTRE, "Wyjdz z gry"); 41 al_flip_display(); 42 while (1) 43 { 44 ALLEGRO_EVENT ev; 45 ALLEGRO_TIMEOUT timeout; 46 al_init_timeout(&timeout, 0.06); 47 48 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 49 50 51 if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 52 break; 53 } 54 else if (get_event && ev.type == ALLEGRO_KEY_DOWN) { 55 break; 56 } 57 58 al_clear_to_color(al_map_rgb(2, 147, 0)); 59 al_draw_text(fonttytul, al_map_rgb(223, 234, 18), 1024 / 2, 80, ALLEGRO_ALIGN_CENTRE, "Indi w tarapatach!"); 60 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 250, ALLEGRO_ALIGN_CENTRE, "Nowa gra"); 61 al_draw_text(font, al_map_rgb(255, 255, 255), 1024 / 2, 400, ALLEGRO_ALIGN_CENTRE, "Wyjdz z gry"); 62 al_flip_display(); 63 } 64 65 al_destroy_display(displaystart); 66 al_destroy_event_queue(event_queue); 67 return 0; 68}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

I used to make that mistake ALL THE TIME.

If this was C++, I'd say we should overload that function and make one that takes a variable number of arguments so that it's obvious you're supposed to pipe sources to it.

ala

event_queue = al_create_event_queue(display_source, keyboard_source, mouse_source, fps_timer, ...);

Let the API structure itself give hints for what your supposed to do.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Bater55
Member #16,769
November 2017

Thanks. Also, I changed ALLEGRO_KEY_DOWN to ALLEGRO_EVENT_KEY_DOWN and now it works!
By the way - do you know how to display window after window? I mean, start menu, then click "1" and the game display is on.

Rodolfo Lam
Member #16,045
August 2015

Just start drawing something else... No need to have several separate "Windows" for each game state, unless you really need and intend to do that.

Game states are and should be independent of the state of the external application Window.

Go to: