Class | Rubygame::EventQueue |
In: |
lib/rubygame/queue.rb
|
Parent: | Array |
EventQueue provides a simple way to manage SDL‘s events, allowing the application to detect keyboard presses, mouse movements and clicks, joystick movement, etc. You can also post custom events to the EventQueue to help manage the game state.
This class replaces the old Rubygame::Queue class, which is no longer available. While EventQueue class serves the same purpose as the old class, they are somewhat different in behavior. Please note that while the old class was a Singleton, this class is not; you may have as many separate instances of EventQueue as you wish (although it is strongly recommended that only one be used to fetch_sdl_events).
For basic usage, create a new EventQueue with autofetch, then call the each method once per game loop, passing a block which handles events. See the sample applications for examples of this.
If you wish to ignore all events of a certain class, append those classes the instance variable @ignore (accessors are provided). You can ignore as many classes of events as you want, but make sure you don‘t ignore ALL event classes, or the user won‘t be able to control the game!
If the program has to pause and wait for an event (for example, if the player must press a button to begin playing), you might find the wait method to be convenient.
For reference, the full list of SDL events is:
each | -> | peek_each |
Iterate through all events in the EventQueue, yielding them one at a time to the given block. The EventQueue is flushed after all events have been yielded. You can use peek_each if you want to keep the events.
If the internal variable @autofetch is true, this method will call fetch_sdl_events once before iterating.
Posts pending SDL hardware events to the EventQueue. Only one EventQueue should call this method per application, and only if you are not using Rubygame#fetch_sdl_events to manually process events! Otherwise, some events may be removed from SDL‘s event stack before they can be properly processed!
Wait for an event to be posted, then return that event. If there is already an event in the queue, this method will immediately return that event. Events that are ignored will not trigger the return.
This method takes this argument:
time: | how long (in milliseconds) to delay between each check for new events. Defaults to 10 ms. |
If a block is given to this method, it will be run after each unsuccessful check for new events. This method will pass to the block the number of times it has checked for new events.
If the internal variable @autofetch is true, this method will call fetch_sdl_events before every check for new events.
Please be cautious when using this method, as it is rather easy to cause an infinite loop. Two ways an infinite loop might occur are: