Skip to content

Instantly share code, notes, and snippets.

@Skrylar
Created January 2, 2014 12:07
Show Gist options
  • Save Skrylar/8218341 to your computer and use it in GitHub Desktop.
Save Skrylar/8218341 to your computer and use it in GitHub Desktop.
Heinous hackery to transmit zero-copy events in Rust.
#[inline]
unsafe fn with_window_event_db(_in: &CommonEventInfo, block: |&mut DelegateBoard|) {
let event : &MouseMotionEventInfo = cast::transmute(_in);
EventPump::with_window_db(event.window_id(), block)
}
unsafe fn with_window_db(id: u32, block: |&mut DelegateBoard|) {
let win = ffi::SDL_GetWindowFromID(id);
if win.is_not_null() {
let dbp = ffi::SDL_GetWindowData(win, bytes!("rust_cb", 0).as_ptr() as *i8);
if dbp.is_not_null() {
let db : &mut DelegateBoard = cast::transmute(dbp);
block(db)
}
}
}
pub fn do_all(&mut self) {
unsafe {
while (ffi::SDL_PollEvent(&mut self.data as *mut EventInfo) == 1) {
let event : &CommonEventInfo = cast::transmute(&self.data);
match event.etype {
event::MouseMotion => EventPump::with_window_event_db(event, |db| {
db.mouse.borrow().with_mut(|this| {
this.mouse_did_move(cast::transmute(event))
})
}),
event::MouseButtonDown => {},
event::MouseButtonUp => {},
event::MouseWheel => {},
_ => {}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment