Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I do not quite understand the custom events mentioned in the user manual

This thread is locked; no one can reply to it. rss feed Print
I do not quite understand the custom events mentioned in the user manual
keprast
Member #16,794
January 2018

Hello.

My English is very bad that I can not understand some of the content on the manual. ???
For example, user-defined queue, which is doing?

Another question is: If I have a lot of rendering animation, how do I submit them to the queue.

Although I try to use some machine translation, but apparently not enough. :-[

I need people to help me, can you help me?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

User events in Allegro 5 aren't too hard.

First, you need an event source :

Then you need to register it with an event queue :

al_register_event_source(event_queue , &evsrc);

Then when you want to emit an event, you fill in the data fields of an ALLEGRO_EVENT and use the event source to emit that event :

ALLEGRO_EVENT my_event;
my_event.type = AL_ID('M' , 'I' , 'N' , 'E');
my_event.data.user1 = (intptr_t)some_data_address;/// Pass whatever custom data you want here

al_emit_user_event(&evsrc , &my_event , 0);

Then during your event loop, you check for events from that source :

ALLEGRO_EVENT ev;
al_wait_for_event(q , &ev);
if (ev.source == &evsrc) {
   /// We got an event from our event source
   if (ev.type == AL_ID('M' , 'I' , 'N' , 'E')) {
      /// We got an event of type MINE from evsrc
      DoStuff(ev.data.user1);
   }
}

Audric
Member #907
January 2001

In case the Allegro functions themselves are not clear :

   // This allocates memory for one queue. It is empty.
   event_queue = al_create_event_queue();

   // This tells Allegro to post keyboard events in this queue, every time the user uses the keyboard.
   al_register_event_source(event_queue, al_get_keyboard_event_source());

   // This tells Allegro to post mouse events in this queue, every time the user uses the mouse.
   al_register_event_source(queue, al_get_mouse_event_source());

Generally, you want to use one queue for everything. This way, when you look in the queue, you get events in the correct order : "First the user pressed A, then he moved the mouse..."

keprast said:

Another question is: If I have a lot of rendering animation, how do I submit them to the queue.

Normally, you separate in two functions :
LOGIC (game logic) :
- runs 60 times per second (for example)
- takes user input into account
- takes all decisions
- performs "physics" : Every execution of the function models what happens in 1/60 second
- selects the current image of animations (ex : Run loop, images 1 2 3 4 1 2 3 4...). The information needs to be stored in memory, not drawn.
- is the one that creates and deletes game elements (bullets,...)
- can start sound effects

DRAWING
- only needed after a logic step has run
- can be done after several logic steps if the computer is too slow (it is called frame skipping)
- only places graphic elements on the screen

When you have this separation, all the code in logic is fast. You can then use a timer event at 1/60s to run logic, and every time the event queue is empty, you can call the draw function to update the screen.

keprast
Member #16,794
January 2018

THANKS!
Belated thanks. :)

Some things make me unable to appear for some time.

Chris Katko
Member #1,881
January 2002
avatar

I agree though even as a 1st language English user, that the docs are really confusing on that subject and I almost always have to re-lookup my old code, or grep though examples, to find the answer on how to do custom events, as well as "multiple events of the same time in one queue" (telling them apart).

Who is up for making an Allegro 5/A.CC FAQ? If you could come up with Q's, I'll start preparing either a Wiki or YouTube walkthrough of the answers...

I'm trying to get disciplined again (::boo:: health problems) and maybe forcing myself to do one a week could help me be more productive...

-----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

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

There's this

https://wiki.allegro.cc/index.php?title=FAQ

and I thought there was another one... but no, I was thinking of something else.

Alas, Tomasu is on vacation or was and doesn't have much time for the wiki anymore. I lost a bunch of pages and have been banned by his spam killers multiple times now and I lost my motivation.

References

  1. Hall of Fame
beoran
Member #12,636
March 2011

If the manual is unclear, please do make a pull request on github with a suggestion for improvement, perhaps with examples. That should be more useful than a separate FAQ.

Go to: