Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » DISPLAY NOT CLOSING

This thread is locked; no one can reply to it. rss feed Print
DISPLAY NOT CLOSING
_avash
Member #15,988
June 2015

while(!done)
{
for(t=0;t<n*2*3.14;t+=0.01)
{
ALLEGRO_EVENT events;
al_wait_for_event(event_queue,&events);
if(events.type==ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done=true;
}
else if(events.type==ALLEGRO_EVENT_TIMER)
{

x=100*cos(t) + 400;
y=100*sin(t) + 400;
al_draw_filled_circle(x,y,5,al_map_rgb(100,200,100));
al_draw_filled_circle(400,400,15,al_map_rgb(500,200,100));

al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
n++;
}

}
}
what am i doing wrong here .. i cant seem to close the display

Thomas Fjellstrom
Member #476
June 2000
avatar

You need to register the display's event source with your event queue.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

You're incrementing n and the loop never ends.

It's only doing that in the TIMER event though. Not entirely sure what the purpose of that is though.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

_avash
Member #15,988
June 2015

I'm kinda new to allegro... and i am having some trouble to understand how this event system works. how should this program be modified to close the display??

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

What you basically want to do with events is to handle all events currently in the queue, and then handle drawing separately. Separate logic and drawing.

For example, to change your code to work you just need to rearrange it a little.

#SelectExpand
1bool redraw = true; 2while (!done) { 3 4 if (redraw) { 5 for(t=0;t<n*2*3.14;t+=0.01) 6 { 7 x=100*cos(t) + 400; 8 y=100*sin(t) + 400; 9 al_draw_filled_circle(x,y,5,al_map_rgb(100,200,100)); 10 al_draw_filled_circle(400,400,15,al_map_rgb(500,200,100)); 11 } 12 al_flip_display(); 13 al_clear_to_color(al_map_rgb(0,0,0)); 14 redraw = false; 15 } 16 17 do { 18 ALLEGRO_EVENT ev; 19 al_wait_for_event(event_queue , &ev); 20 21 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 22 done = true; 23 } 24 else if (ev.type == ALLEGRO_EVENT_TIMER) { 25 redraw = true; 26 n++; 27 } 28 29 } while (!al_is_event_queue_empty(event_queue); 30 31}

I'm not entirely sure what you're doing with 'n' there. Because all it is doing is making it go around the circle 'n' times, but once is enough.

Thomas Fjellstrom
Member #476
June 2000
avatar

Avash, each thing that can emit events is called an event source. For example, the mouse, keyboard, and all displays are all event sources. To actually listen for events from each source, you have to connect them to your event queue.

For a display, that would look something like: al_register_event_source(event_queue, al_get_display_event_source(display));

Now you will see events that come from the display (like DISPLAY_CLOSE) show up in your event queue.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Neil Roy
Member #2,229
April 2002
avatar

An event is basically the computer telling you "hey! Something just happened!". Now when you register events, you are telling the computer "let me know if something happens with this!" so you register a timer which fires off however often you set it to, so that will now trigger an event. Then you register your keyboard and mouse so they trigger events. Now when a timer comes up, or a keyboard or mouse is pressed or moved, it triggers an event. These events are added to an event queue (a list of events that happened). You have some code that waits for an event to happen (waits for someone to press a key, move a mouse or the timer to fire off that might be for updating the screen). When an event is triggered, you then ask allegro, hey, what event was that? And you have some code that says if it was the keyboard, then we'll go into this block of code and check which keys were pressed and respond accordingly. The same goes for the other events.

That's about as well as I can explain it. ;)

One additional note. When you post your program code, it helps us all if you put

<code>

before it and

</code>

after it. So when you post it you see...

<code>
if(a==b) {
   c--;
}
</code>

and we see this...

if(a==b) {
   c--;
}

It looks better and is easier for us to help you.

note: I almost typed c++ in the above example but realized that would look too ugly! :P

---
“I love you too.” - last words of Wanda Roy

_avash
Member #15,988
June 2015

Thanks guys . Looks like I got it. And for that n++ though, its for infinte loop.
Actually its for the electron revolving around proton
:D

Go to: