Struct sdl2::EventSubsystem

source ·
pub struct EventSubsystem { /* private fields */ }

Implementations§

source§

impl EventSubsystem

source

pub fn flush_event(&self, event_type: EventType)

Removes all events in the event queue that match the specified event type.

source

pub fn flush_events(&self, min_type: u32, max_type: u32)

Removes all events in the event queue that match the specified type range.

source

pub fn peek_events<B>(&self, max_amount: u32) -> B
where B: FromIterator<Event>,

Reads the events at the front of the event queue, until the maximum amount of events is read.

The events will not be removed from the queue.

Example
use sdl2::event::Event;

let sdl_context = sdl2::init().unwrap();
let event_subsystem = sdl_context.event().unwrap();

// Read up to 1024 events
let events: Vec<Event> = event_subsystem.peek_events(1024);

// Print each one
for event in events {
    println!("{:?}", event);
}
source

pub fn push_event(&self, event: Event) -> Result<(), String>

Pushes an event to the event queue.

source

pub unsafe fn register_event(&self) -> Result<u32, String>

Register a custom SDL event.

When pushing a user event, you must make sure that the type_ field is set to a registered SDL event number.

The code, data1, and data2 fields can be used to store user defined data.

See the SDL documentation for more information.

Example
let sdl = sdl2::init().unwrap();
let ev = sdl.event().unwrap();

let custom_event_type_id = unsafe { ev.register_event().unwrap() };
let event = sdl2::event::Event::User {
   timestamp: 0,
   window_id: 0,
   type_: custom_event_type_id,
   code: 456,
   data1: 0x1234 as *mut libc::c_void,
   data2: 0x5678 as *mut libc::c_void,
};

ev.push_event(event);
source

pub unsafe fn register_events(&self, nr: u32) -> Result<Vec<u32>, String>

Registers custom SDL events.

Returns an error, if no more user events can be created.

source

pub fn register_custom_event<T: Any>(&self) -> Result<(), String>

Register a custom event

It returns an error when the same type is registered twice.

Example

See push_custom_event

source

pub fn push_custom_event<T: Any>(&self, event: T) -> Result<(), String>

Push a custom event

If the event type T was not registered using register_custom_event, this method will panic.

Example: pushing and receiving a custom event
struct SomeCustomEvent {
    a: i32
}

let sdl = sdl2::init().unwrap();
let ev = sdl.event().unwrap();
let mut ep = sdl.event_pump().unwrap();

ev.register_custom_event::<SomeCustomEvent>().unwrap();

let event = SomeCustomEvent { a: 42 };

ev.push_custom_event(event);

let received = ep.poll_event().unwrap(); // or within a for event in ep.poll_iter()
if received.is_user_event() {
    let e2 = received.as_user_event_type::<SomeCustomEvent>().unwrap();
    assert_eq!(e2.a, 42);
}
source

pub fn event_sender(&self) -> EventSender

Create an event sender that can be sent to other threads.

An EventSender will not keep the event subsystem alive. If the event subsystem is shut down calls to push_event and push_custom_event will return errors.

source

pub fn add_event_watch<'a, CB: EventWatchCallback + 'a>( &self, callback: CB ) -> EventWatch<'a, CB>

Create an event watcher which is called every time an event is added to event queue.

The watcher is disabled when the return value is dropped. Just calling this function without binding to a variable immediately disables the watcher. In order to make it persistent, you have to bind in a variable and keep it until it’s no longer needed.

Example: dump every event to stderr
let sdl = sdl2::init().unwrap();
let ev = sdl.event().unwrap();

// `let _ = ...` is insufficient, as it is dropped immediately.
let _event_watch = ev.add_event_watch(|event| {
    dbg!(event);
});
source§

impl EventSubsystem

source

pub fn sdl(&self) -> Sdl

Obtain an SDL context.

Trait Implementations§

source§

impl Clone for EventSubsystem

source§

fn clone(&self) -> EventSubsystem

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for EventSubsystem

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Sync for EventSubsystem

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.