Remove lifetime from EventPump, move into sdl2::sdl module

The lifetime on EventPump isn't really needed anymore.
This commit is contained in:
Dan Spencer
2015-07-28 01:34:59 -06:00
parent ef5c9016ad
commit edfbfd7690
5 changed files with 54 additions and 59 deletions
+1 -52
View File
@@ -24,7 +24,6 @@ use mouse::{Mouse, MouseState};
use keyboard::Scancode;
use get_error;
use SdlResult;
use Sdl;
use sys::event as ll;
@@ -1001,44 +1000,7 @@ unsafe fn wait_event_timeout(timeout: u32) -> Option<Event> {
else { None }
}
static mut IS_EVENT_PUMP_ALIVE: bool = false;
/// A thread-safe type that encapsulates SDL event-pumping functions.
pub struct EventPump<'sdl> {
_sdl: PhantomData<&'sdl ()>,
// Prevents the event pump from moving to other threads.
// SDL events can only be pumped on the main thread.
_nosend: PhantomData<*mut ()>
}
impl<'sdl> EventPump<'sdl> {
/// Obtains the SDL event pump.
#[inline]
pub fn new(_sdl: &'sdl Sdl) -> SdlResult<EventPump<'sdl>> {
// Called on the main SDL thread.
unsafe {
if IS_EVENT_PUMP_ALIVE {
Err(format!("an `EventPump` instance is already alive - there can only be one `EventPump` in use at a time."))
} else {
// Initialize the events subsystem, just in case none of the other subsystems have done it yet.
let result = ::sys::sdl::SDL_InitSubSystem(::sys::sdl::SDL_INIT_EVENTS);
if result == 0 {
IS_EVENT_PUMP_ALIVE = true;
Ok(EventPump {
_sdl: PhantomData,
_nosend: PhantomData,
})
} else {
Err(get_error())
}
}
}
}
impl ::EventPump {
/// Query if an event type is enabled.
pub fn is_event_enabled(&self, event_type: EventType) -> bool {
let result = unsafe { ll::SDL_EventState(event_type as u32, ll::SDL_QUERY) };
@@ -1130,19 +1092,6 @@ impl<'sdl> EventPump<'sdl> {
}
}
impl<'sdl> Drop for EventPump<'sdl> {
#[inline]
fn drop(&mut self) {
// Called on the main SDL thread.
unsafe {
assert!(IS_EVENT_PUMP_ALIVE);
::sys::sdl::SDL_QuitSubSystem(::sys::sdl::SDL_INIT_EVENTS);
IS_EVENT_PUMP_ALIVE = false;
}
}
}
/// An iterator that calls `EventPump::poll_event()`.
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct EventPollIterator<'a> {
+4 -4
View File
@@ -2,7 +2,7 @@ use num::{ToPrimitive, FromPrimitive};
use std::ptr;
use std::marker::PhantomData;
use event::EventPump;
use EventPump;
use rect::Rect;
use video::Window;
@@ -55,7 +55,7 @@ impl<'a> KeyboardState<'a> {
/// ```no_run
/// use sdl2::keyboard::Scancode;
///
/// fn is_a_pressed(e: &sdl2::event::EventPump) -> bool {
/// fn is_a_pressed(e: &sdl2::EventPump) -> bool {
/// e.keyboard_state().is_scancode_pressed(Scancode::A)
/// }
/// ```
@@ -79,11 +79,11 @@ impl<'a> KeyboardState<'a> {
/// use sdl2::keyboard::Scancode;
/// use std::collections::HashSet;
///
/// fn pressed_scancode_set(e: &sdl2::event::EventPump) -> HashSet<Scancode> {
/// fn pressed_scancode_set(e: &sdl2::EventPump) -> HashSet<Scancode> {
/// e.keyboard_state().pressed_scancodes().collect()
/// }
///
/// fn pressed_keycode_set(e: &sdl2::event::EventPump) -> HashSet<Keycode> {
/// fn pressed_keycode_set(e: &sdl2::EventPump) -> HashSet<Keycode> {
/// e.keyboard_state().pressed_scancodes()
/// .filter_map(Keycode::from_scancode)
/// .collect()
+1 -1
View File
@@ -28,7 +28,7 @@
//! None of the draw methods in `Renderer` are expected to fail.
//! If they do, a panic is raised and the program is aborted.
use event::EventPump;
use EventPump;
use video::{Window, WindowProperties, WindowPropertiesGetters};
use surface;
use surface::{Surface, SurfaceRef};
+47 -1
View File
@@ -2,7 +2,6 @@ use std::ffi::{CStr, CString};
use std::rc::Rc;
use sys::sdl as ll;
use event::EventPump;
use util::CStringExt;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
@@ -191,6 +190,53 @@ subsystem!(TimerSubsystem, ll::SDL_INIT_TIMER, sync);
// The event queue can be read from other threads.
subsystem!(EventSubsystem, ll::SDL_INIT_EVENTS, sync);
static mut IS_EVENT_PUMP_ALIVE: bool = false;
/// A thread-safe type that encapsulates SDL event-pumping functions.
pub struct EventPump {
_sdldrop: Rc<SdlDrop>
}
impl EventPump {
/// Obtains the SDL event pump.
#[inline]
fn new(sdl: &Sdl) -> SdlResult<EventPump> {
// Called on the main SDL thread.
unsafe {
if IS_EVENT_PUMP_ALIVE {
Err(format!("an `EventPump` instance is already alive - there can only be one `EventPump` in use at a time."))
} else {
// Initialize the events subsystem, just in case none of the other subsystems have done it yet.
let result = ll::SDL_InitSubSystem(ll::SDL_INIT_EVENTS);
if result == 0 {
IS_EVENT_PUMP_ALIVE = true;
Ok(EventPump {
_sdldrop: sdl.sdldrop.clone()
})
} else {
Err(get_error())
}
}
}
}
}
impl Drop for EventPump {
#[inline]
fn drop(&mut self) {
// Called on the main SDL thread.
unsafe {
assert!(IS_EVENT_PUMP_ALIVE);
ll::SDL_QuitSubSystem(ll::SDL_INIT_EVENTS);
IS_EVENT_PUMP_ALIVE = false;
}
}
}
/// Initializes the SDL library.
/// This must be called before using any other SDL function.
///
+1 -1
View File
@@ -11,7 +11,7 @@ use render::RendererBuilder;
use surface::SurfaceRef;
use pixels;
use VideoSubsystem;
use event::EventPump;
use EventPump;
use SdlResult;
use num::FromPrimitive;
use util::CStringExt;