### Home Classes Methods

### In Files

- lib/rubygame/queue.rb

### Parent

Array

### Methods

- ::new
- #_old_each
- #each
- #enable_new_style_events
- #fetch_sdl_events
- #peek_each
- #post
- #push
- #wait

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::EventQueue

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.

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.

In Rubygame 2.4 and later, you can call enable_new_style_events to make EventQueue fetch the new event classes (in the Rubygame::Events module). Otherwise, the old classes will be used, for backwards compatibility.

It is **strongly recommended** that you use the new event classes. The old classes are deprecated as of Rubygame 2.4, and will be removed entirely in Rubygame 3.0.

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.

### Attributes

autofetch[RW]

Whether to fetch SDL events automatically when each and wait are used. Enabled by default.

ignore[RW]

Array of classes to be ignored by push.

### Public Class Methods

new() click to toggle source

Create a new EventQueue.

    # File lib/rubygame/queue.rb, line 63
    def initialize()
      @autofetch = true
      @ignore = []
      @new_style_events = false
      yield self if block_given?
    end

### Public Instance Methods

_old_each( &block ) click to toggle source
Alias for: each
each( &block ) click to toggle source

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.

    # File lib/rubygame/queue.rb, line 108
    def each( &block )
      fetch_sdl_events if @autofetch
      _old_each( &block )
      self.clear
    end

Also aliased as: _old_each
enable_new_style_events() click to toggle source

Enable new-style events. These are the event classes in the Rubygame::Events module, which were added in Rubygame 2.4.

If you call this method, the new event classes will be used. Otherwise, the old classes will be used, for backwards compatibility.

It is **strongly recommended** that you use the new event classes. The old classes are deprecated as of Rubygame 2.4, and will be removed entirely in Rubygame 3.0.

    # File lib/rubygame/queue.rb, line 82
    def enable_new_style_events
      @new_style_events = true
    end

fetch_sdl_events() click to toggle source

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!

    # File lib/rubygame/queue.rb, line 127
              def fetch_sdl_events
    if @new_style_events
      self.push( Rubygame::Events.fetch_sdl_events() )
    else
      Rubygame.deprecated("Rubygame::EventQueue with old event classes",
                          "3.0")
      self.push( Rubygame.fetch_sdl_events() )
    end
              end

peek_each( &block ) click to toggle source

Like each, but doesn't remove the events from the queue after iterating.

    # File lib/rubygame/queue.rb, line 116
    def peek_each( &block )
      fetch_sdl_events if @autofetch
      _old_each( &block )
    end

post(*events) click to toggle source
Alias for: push
push(*events) click to toggle source

Append events to the EventQueue. Silently ignores events whose class is in @ignore.

    # File lib/rubygame/queue.rb, line 89
    def push(*events)
      events = events.flatten.delete_if {|e| @ignore.include?(e.class)}
      events.each do |e|
        super( e )
      end
    end

Also aliased as: post
wait(delay=10, &block) click to toggle source

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:

1. Waiting for an SDL event when @autofetch is disabled. (This is not a problem if the block will post an event.)

2. Waiting for any event when all possible event types are ignored.

    # File lib/rubygame/queue.rb, line 159
    def wait(delay=10, &block)
      iterations = 0
      if block_given?
        loop do
          fetch_sdl_events() if @autofetch
          if self.length >= 1
            s = self.shift
            return s unless s == nil
          end
          yield iterations
          iterations += 1
          Rubygame::Clock.delay(delay)
        end
      else
        loop do
          fetch_sdl_events() if @autofetch
          s = self.shift
          return s unless s == nil
          iterations += 1
          Rubygame::Clock.delay(delay)
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.