User Events?
Aaron Santiago

I tried reading it in the docs, but I became hopelessly lost, really really fast, right around here:
bool al_emit_user_event(ALLEGRO_EVENT_SOURCE *src,ALLEGRO_EVENT *event, void (*dtor)(ALLEGRO_USER_EVENT *))
I understand that I have to make an event source, then register it. But what is the normal event argument and what's dtor? D:


RTFM some more? There's more to the documentation than the function signature...

Essentially though, the use case would be like this:


ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_register_event_source(queue, &source);

event.user.data1 = 5;

al_emit_user_event(&source, &event, 0);

al_wait_for_event(queue, &event2);
printf("%d\n", (int)event2.user.data1); // will print 5

Aaron Santiago

So if a user source can emit normal events, then what's the point of user events?
Also, I have no idea what manual you're reading, but hell, I'd love to get my hands on it.


So if a user source can emit normal events

How did you arrive at this conclusion? A user source can only emit user events.

EDIT: Well, looking at the source I guess it would work. But that's an implementational detail, it certainly is not documented. Nvm, it is not guaranteed to work.

Either way, user events are useful because they have 4 separate data fields that you can fill with anything you desire. No other event type has the same.

Aaron Santiago

That's what the code you posted did, it emitted a normal event.
The user events aren't that well documented, and it would seem that what you posted wouldn't work.



That's what the code you posted did, it emitted a normal event.

You're misunderstaning.
It emits an ALLEGRO_EVENT, as all event sources do. One particular type of event is an ALLEGRO_USER_EVENT, others are ALLEGRO_KEYBOARD_EVENT or ALLEGRO_DISPLAY_EVENT.
To access the fields of that particular event type from an ALLEGRO_EVENT you use the "user", "keyboard", "display" etc. fields. Alternatively, you could cast the ALLEGRO_EVENT to the event type you're using.

Aaron Santiago

That makes sense.
The thing is, in the docs it says that an ALLEGRO_USER_EVENT is its own object, horribly confusing.
So then, SiegeLord's code should work as posted, correct?

Matthew Leverton

Have you looked at ex_user_events.c?

Edgar Reynaldo

I don't think the documentation for user events is that great either. The manual entry for ALLEGRO_EVENT doesn't mention how to test for a user event type when processing events from a queue. Are we supposed to set event.type to some magic number that we remember later or what? Also, the 'user' field of an ALLEGRO_EVENT that represents an ALLEGRO_USER_EVENT is not documented anywhere either, and it should probably be on the ALLEGRO_EVENT manual page.

Aaron Santiago

Yeah, the user field kind of blew my mind a little. There's like 0 documentation on this, really.
But ex_user_events.c did clear enough of it up that I think I can use it as a reference to utilize user events for myself.

Thread #607803. Printed from