Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multiple custom events?

This thread is locked; no one can reply to it. rss feed Print
Multiple custom events?
Werner Kroneman
Member #14,105
March 2012

Hello, I noticed that when I do:

#define USER_EVENT_COLLISION ALLEGRO_GET_EVENT_TYPE('m', 's', 'e', 't')
#define USER_EVENT_BALL_LOST ALLEGRO_GET_EVENT_TYPE('m', 's', 'e', 't')

that these two event types get the same number. I'm totally not experienced with stuff that has a # in front of it, so can anyone explain the right way to do define custom events?

Matthew Leverton
Supreme Loser
January 1999
avatar

If you use the same 4 characters, you'll get the same event id. See the documentation on how to use it properly.

Werner Kroneman
Member #14,105
March 2012

Does ALLEGRO_GET_EVENT_TYPE('m', 's', 'e', 't') simply return an int, with the 4 specified chararcters used as the 4 bytes of the int?

Matthew Leverton
Supreme Loser
January 1999
avatar

It is the same thing as AL_ID:
#define AL_ID(a,b,c,d) (((a)<<24) | ((b)<<16) | ((c)<<8) | (d))
So yes.

Werner Kroneman
Member #14,105
March 2012

Thanks then.

Acturo
Member #13,981
February 2012

It's much simpler to do this.

#SelectExpand
1enum EVENT_ID { 2ID_MYEVENT = 1025, 3ID_MYNEXTEVENT, 4ID_MYOTHEREVENT 5}; 6 7ALLEGRO_EVENT nev; 8nev.type = ID_MYEVENT; 9nev.user.data1 = (int_ptr)&my_data; 10... 11 12void SomeFunc(ALLEGRO_EVENT* ev) 13{ 14my_data* data = (my_data*)ev->user.data1; 15}

Allegro already gives you a custom event. All you need is a list of unique event identifiers to use them, then typecast your data type and poke it in one of the four available slots.

Peter Wang
Member #23
April 2000

Quote:

You should try to make your IDs unique so they don't clash with any 3rd party code you may be using. Be creative. Numbering from 1024 is not creative.

So you started from 1025 instead ;)

weapon_S
Member #7,859
October 2006
avatar

I've been thinking it might be better to do something like:

//ASCII begins with 32 unprintable characters, and has a total of 95 printable characters
1024 + (a-32) + (b-32)*95 + (c-32)*95*95 + (d-32)*95*95*95

Because if you use raw numbers instead of printable characters, it is already possible to get something below 1024. The audience of this helper macro are people who want a safe number that won't clash with anything. And this proposed method would free up a block (anything above 81.450.624?), making it easier to avoid clashes in add-ons/libraries.(If you add a #DEFINE MY_LIBRARY_EVENT_OFFSET, and normally start at 81.450.624).
So in short it would introduce a block deemed 'for add-ons' at the end.

SiegeLord
Member #7,827
October 2006
avatar

weapon_S said:

The audience of this helper macro are people who want a safe number that won't clash with anything.

Allegro's AL_ID macro already provides this guarantee. If you use ASCII characters then the id's it will return will range from 538976288 to 2122219134 inclusively.

Quote:

And this proposed method would free up a block (anything above 81.450.624?), making it easier to avoid clashes in add-ons/libraries.(If you add a #DEFINE MY_LIBRARY_EVENT_OFFSET, and normally start at 81.450.624).
So in short it would introduce a block deemed 'for add-ons' at the end.

What if two libraries define the same event offset?

It's impossible to avoid clashes without a centralized registry.

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

weapon_S
Member #7,859
October 2006
avatar

But that range will have a lot of holes in it. The offset would be something the user of the library can define. But NVM, there's still an open spot of about 2 billion events (? did I count right?). People who use a lot of user-events or want to directly use a number should be advised to use that area, in my uneducated opinion. But there is no need to compact the numbers used.

Go to: