Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » User Events?

This thread is locked; no one can reply to it. rss feed Print
User Events?
Aaron Santiago
Member #12,074
June 2010
avatar

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:

SiegeLord
Member #7,827
October 2006
avatar

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

Essentially though, the use case would be like this:

ALLEGRO_EVENT_SOURCE source;
al_init_user_event_source(&source);

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

ALLEGRO_EVENT event;
event.user.data1 = 5;

al_emit_user_event(&source, &event, 0);

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

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Aaron Santiago
Member #12,074
June 2010
avatar

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.

SiegeLord
Member #7,827
October 2006
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Aaron Santiago
Member #12,074
June 2010
avatar

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.

ALLEGRO_EVENT event;
ALLEGRO_USER_EVENT userEvent; //?

Evert
Member #794
November 2000
avatar

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
Member #12,074
June 2010
avatar

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
Supreme Loser
January 1999
avatar

Have you looked at ex_user_events.c?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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
Member #12,074
June 2010
avatar

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.

Go to: