From d20835baa4b4095f883190be84caa88eff780cb8 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 22 May 2014 23:07:16 +0400 Subject: [PATCH 1/3] Made raw/owned fields private * Removed `pub` modifier from these fields * Implemented accessors for these fields where appropriate * Implemented `new_from_raw` unsafe constructors because they are sometimes needed --- src/sdl2/audio.rs | 9 ++++++--- src/sdl2/keyboard.rs | 4 ++-- src/sdl2/lib.rs | 1 + src/sdl2/macros.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/sdl2/mouse.rs | 6 +++--- src/sdl2/pixels.rs | 9 +++++++-- src/sdl2/render.rs | 21 +++++++++++--------- src/sdl2/rwops.rs | 7 +++++-- src/sdl2/surface.rs | 18 ++++++++++------- src/sdl2/video.rs | 27 +++++++++++++++++++------- 10 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 src/sdl2/macros.rs diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 7702f616..42ca095d 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -231,7 +231,7 @@ impl<'a> AudioSpec<'a> { let audio_buf = ptr::null::(); let audio_len = 0u32; unsafe { - let ret = ll::SDL_LoadWAV_RW(src.raw, 0, mem::transmute(&spec), &audio_buf, &audio_len); + let ret = ll::SDL_LoadWAV_RW(src.raw(), 0, mem::transmute(&spec), &audio_buf, &audio_len); if ret.is_null() { Err(get_error()) } else { @@ -317,10 +317,13 @@ impl AudioDevice { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct AudioCVT { - pub raw: *mut ll::SDL_AudioCVT, - pub owned: bool, + raw: *mut ll::SDL_AudioCVT, + owned: bool, } +impl_raw_accessors!(AudioCVT, *mut ll::SDL_AudioCVT) +impl_owned_accessors!(AudioCVT, owned) + impl Drop for AudioCVT { fn drop(&mut self) { if self.owned { diff --git a/src/sdl2/keyboard.rs b/src/sdl2/keyboard.rs index 212c38d8..62efb8a5 100644 --- a/src/sdl2/keyboard.rs +++ b/src/sdl2/keyboard.rs @@ -71,7 +71,7 @@ pub fn get_keyboard_focus() -> Option { if raw == ptr::null() { None } else { - Some(Window{ raw: raw, owned: false }) + unsafe { Some(Window::new_from_raw(raw, false)) } } } @@ -163,5 +163,5 @@ pub fn has_screen_keyboard_support() -> bool { } pub fn is_screen_keyboard_shown(window: &Window) -> bool { - unsafe { ll::SDL_IsScreenKeyboardShown(window.raw) == 1 } + unsafe { ll::SDL_IsScreenKeyboardShown(window.raw()) == 1 } } diff --git a/src/sdl2/lib.rs b/src/sdl2/lib.rs index 9c5fa286..a8acdd57 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -16,6 +16,7 @@ pub mod keycode; #[path = "generated/scancode.rs"] pub mod scancode; +pub mod macros; pub mod event; pub mod gesture; pub mod touch; diff --git a/src/sdl2/macros.rs b/src/sdl2/macros.rs new file mode 100644 index 00000000..61221ffd --- /dev/null +++ b/src/sdl2/macros.rs @@ -0,0 +1,46 @@ +#![macro_escape] + +macro_rules! impl_raw_accessors( + ($($t:ty, $raw:ty);+) => ( + $( + impl $t { + #[inline] + pub fn raw(&self) -> $raw { self.raw } + } + )+ + ) +) + +macro_rules! impl_owned_accessors( + ($($t:ty, $owned:ident);+) => ( + $( + impl $t { + #[inline] + pub fn $owned(&self) -> bool { self.$owned } + } + )+ + ) +) + +macro_rules! impl_raw_constructor( + ($($t:ty -> $te:ident ($r:ident:$rt:ty));+) => ( + $( + impl $t { + #[inline] + pub unsafe fn new_from_raw($r:$rt) -> $t { + $te { $r: $r } + } + } + )+ + ); + ($($t:ty -> $te:ident ($r:ident:$rt:ty, $o:ident:$ot:ty));+) => ( + $( + impl $t { + #[inline] + pub unsafe fn new_from_raw($r:$rt, $o:$ot) -> $t { + $te { $r: $r, $o: $o } + } + } + )+ + ) +) diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index 7753e1e6..ba892a28 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -105,7 +105,7 @@ impl Cursor { // TODO: figure out how to pass Surface in here correctly pub fn from_surface(surface: &surface::Surface, hot_x: int, hot_y: int) -> Result { unsafe { - let raw = ll::SDL_CreateColorCursor(surface.raw, hot_x as i32, + let raw = ll::SDL_CreateColorCursor(surface.raw(), hot_x as i32, hot_y as i32); if raw == ptr::null() { @@ -167,7 +167,7 @@ pub fn get_mouse_focus() -> Option { if raw == ptr::null() { None } else { - Some(video::Window{ raw: raw, owned: false }) + unsafe { Some(video::Window::new_from_raw(raw, false)) } } } @@ -190,7 +190,7 @@ pub fn get_relative_mouse_state() -> (MouseState, int, int) { } pub fn warp_mouse_in_window(window: &video::Window, x: i32, y: i32) { - unsafe { ll::SDL_WarpMouseInWindow(window.raw, x, y); } + unsafe { ll::SDL_WarpMouseInWindow(window.raw(), x, y); } } pub fn set_relative_mouse_mode(on: bool) { diff --git a/src/sdl2/pixels.rs b/src/sdl2/pixels.rs index 58df04fa..1a253671 100644 --- a/src/sdl2/pixels.rs +++ b/src/sdl2/pixels.rs @@ -89,9 +89,11 @@ pub mod ll { } #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct Palette { - pub raw: *ll::SDL_Palette + raw: *ll::SDL_Palette } +impl_raw_accessors!(Palette, *ll::SDL_Palette) + #[deriving(Eq)] pub enum Color { RGB(u8, u8, u8), @@ -132,9 +134,12 @@ impl rand::Rand for Color { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct PixelFormat { - pub raw: *ll::SDL_PixelFormat + raw: *ll::SDL_PixelFormat } +impl_raw_accessors!(PixelFormat, *ll::SDL_PixelFormat) +impl_raw_constructor!(PixelFormat -> PixelFormat (raw: *ll::SDL_PixelFormat)) + #[deriving(Eq, Show, FromPrimitive)] pub enum PixelFormatFlag { Unknown = ll::SDL_PIXELFORMAT_UNKNOWN as int, diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index c9750824..0a2d4866 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -202,9 +202,9 @@ impl RendererInfo { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct Renderer { - pub raw: *ll::SDL_Renderer, + raw: *ll::SDL_Renderer, parent: Option, - pub owned: bool + owned: bool } #[unsafe_destructor] @@ -226,7 +226,7 @@ impl Renderer { }; let raw = unsafe { - ll::SDL_CreateRenderer(window.raw, index as c_int, renderer_flags.bits()) + ll::SDL_CreateRenderer(window.raw(), index as c_int, renderer_flags.bits()) }; if raw == ptr::null() { @@ -241,10 +241,7 @@ impl Renderer { let raw_renderer: *ll::SDL_Renderer = ptr::null(); let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.bits(), &raw_window, &raw_renderer) == 0}; if result { - let window = Window { - raw: raw_window, - owned: true - }; + let window = unsafe { Window::new_from_raw(raw_window, true) }; Ok(Renderer { raw: raw_renderer, parent: Some(window), @@ -258,7 +255,7 @@ impl Renderer { impl Renderer { pub fn from_surface(surface: surface::Surface) -> Result, ~str> { - let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw) }; + let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw()) }; if result == ptr::null() { Ok(Renderer { raw: result, @@ -281,6 +278,12 @@ impl Renderer { mem::replace(&mut self.parent, None).unwrap() } + #[inline] + pub fn raw(&self) -> *ll::SDL_Renderer { self.raw } + + #[inline] + pub fn owned(&self) -> bool { self.owned } + pub fn set_draw_color(&self, color: pixels::Color) -> Result<(), ~str> { let ret = match color { pixels::RGB(r, g, b) => { @@ -340,7 +343,7 @@ impl Renderer { } pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> Result { - let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw) }; + let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw()) }; if result == ptr::null() { Err(get_error()) } else { diff --git a/src/sdl2/rwops.rs b/src/sdl2/rwops.rs index cf63a00b..448ea86b 100644 --- a/src/sdl2/rwops.rs +++ b/src/sdl2/rwops.rs @@ -43,10 +43,13 @@ pub mod ll { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct RWops { - pub raw: *ll::SDL_RWops, - pub close_on_drop: bool + raw: *ll::SDL_RWops, + close_on_drop: bool } +impl_raw_accessors!(RWops, *ll::SDL_RWops) +impl_owned_accessors!(RWops, close_on_drop) + /// A structure that provides an abstract interface to stream I/O. impl RWops { pub fn from_file(path: &Path, mode: &str) -> Result { diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index 70d03e14..289704a4 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -85,8 +85,8 @@ bitflags!(flags SurfaceFlag: u32 { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct Surface { - pub raw: *ll::SDL_Surface, - pub owned: bool + raw: *ll::SDL_Surface, + owned: bool } impl Drop for Surface { @@ -99,6 +99,10 @@ impl Drop for Surface { } } +impl_raw_accessors!(Surface, *ll::SDL_Surface) +impl_owned_accessors!(Surface, owned) +impl_raw_constructor!(Surface -> Surface (raw: *ll::SDL_Surface, owned: bool)) + impl Surface { pub fn new(surface_flags: SurfaceFlag, width: int, height: int, bpp: int, rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result { @@ -137,8 +141,8 @@ impl Surface { } pub fn get_pixel_format(&self) -> pixels::PixelFormat { - pixels::PixelFormat { - raw: unsafe { (*self.raw).format } + unsafe { + pixels::PixelFormat::new_from_raw((*self.raw).format) } } @@ -164,7 +168,7 @@ impl Surface { pub fn from_bmp(path: &Path) -> Result { let raw = unsafe { - ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw, 0) + ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw(), 0) }; if raw.is_null() { Err(get_error()) } @@ -173,7 +177,7 @@ impl Surface { pub fn save_bmp(&self, path: &Path) -> Result<(), ~str> { let ret = unsafe { - ll::SDL_SaveBMP_RW(self.raw, try!(rwops::RWops::from_file(path, "rb")).raw, 0) + ll::SDL_SaveBMP_RW(self.raw, try!(rwops::RWops::from_file(path, "rb")).raw(), 0) }; if ret == 0 { Ok(()) } else { Err(get_error()) } @@ -181,7 +185,7 @@ impl Surface { pub fn set_palette(&self, palette: &pixels::Palette) -> bool { unsafe { - ll::SDL_SetSurfacePalette(self.raw, palette.raw) == 0 + ll::SDL_SetSurfacePalette(self.raw, palette.raw()) == 0 } } diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index d7a5a943..afe9bac2 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -297,8 +297,8 @@ fn unwrap_windowpos (pos: WindowPos) -> ll::SDL_WindowPos { #[deriving(Eq)] pub struct GLContext { - pub raw: ll::SDL_GLContext, - pub owned: bool + raw: ll::SDL_GLContext, + owned: bool } impl Drop for GLContext { @@ -311,14 +311,27 @@ impl Drop for GLContext { } } - #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct Window { - pub raw: *ll::SDL_Window, - pub owned: bool + raw: *ll::SDL_Window, + owned: bool } +impl_raw_accessors!( + GLContext, ll::SDL_GLContext; + Window, *ll::SDL_Window +) + +impl_owned_accessors!( + GLContext, owned; + Window, owned +) + +impl_raw_constructor!( + Window -> Window (raw: *ll::SDL_Window, owned: bool) +) + impl Drop for Window { fn drop(&mut self) { if self.owned { @@ -427,7 +440,7 @@ impl Window { } pub fn set_icon(&self, icon: &Surface) { - unsafe { ll::SDL_SetWindowIcon(self.raw, icon.raw) } + unsafe { ll::SDL_SetWindowIcon(self.raw, icon.raw()) } } //pub fn SDL_SetWindowData(window: *SDL_Window, name: *c_char, userdata: *c_void) -> *c_void; //TODO: Figure out what this does @@ -515,7 +528,7 @@ impl Window { if raw == ptr::null() { Err(get_error()) } else { - Ok(Surface {raw: raw, owned: false}) //Docs say that it releases with the window + unsafe { Ok(Surface::new_from_raw(raw, false)) } //Docs say that it releases with the window } } From 75ab435dacbb3dd9ea3d7791e0226046bc2b8bef Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 23 May 2014 19:26:20 +0400 Subject: [PATCH 2/3] Made impl_raw_constructors more general --- src/sdl2/macros.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/sdl2/macros.rs b/src/sdl2/macros.rs index 61221ffd..c78d760e 100644 --- a/src/sdl2/macros.rs +++ b/src/sdl2/macros.rs @@ -23,22 +23,12 @@ macro_rules! impl_owned_accessors( ) macro_rules! impl_raw_constructor( - ($($t:ty -> $te:ident ($r:ident:$rt:ty));+) => ( + ($($t:ty -> $te:ident ($($r:ident:$rt:ty),+));+) => ( $( impl $t { #[inline] - pub unsafe fn new_from_raw($r:$rt) -> $t { - $te { $r: $r } - } - } - )+ - ); - ($($t:ty -> $te:ident ($r:ident:$rt:ty, $o:ident:$ot:ty));+) => ( - $( - impl $t { - #[inline] - pub unsafe fn new_from_raw($r:$rt, $o:$ot) -> $t { - $te { $r: $r, $o: $o } + pub unsafe fn new_from_raw($($r:$rt),+) -> $t { + $te { $($r: $r),+ } } } )+ From 84c957375608c5713bce5f8f5009bf226741860f Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sat, 24 May 2014 13:45:11 +0400 Subject: [PATCH 3/3] Changed `new_from_raw` to `from_ll` for consistency --- src/sdl2/keyboard.rs | 2 +- src/sdl2/macros.rs | 2 +- src/sdl2/mouse.rs | 2 +- src/sdl2/render.rs | 2 +- src/sdl2/surface.rs | 2 +- src/sdl2/video.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sdl2/keyboard.rs b/src/sdl2/keyboard.rs index 62efb8a5..a5efca54 100644 --- a/src/sdl2/keyboard.rs +++ b/src/sdl2/keyboard.rs @@ -71,7 +71,7 @@ pub fn get_keyboard_focus() -> Option { if raw == ptr::null() { None } else { - unsafe { Some(Window::new_from_raw(raw, false)) } + unsafe { Some(Window::from_ll(raw, false)) } } } diff --git a/src/sdl2/macros.rs b/src/sdl2/macros.rs index c78d760e..94e401e4 100644 --- a/src/sdl2/macros.rs +++ b/src/sdl2/macros.rs @@ -27,7 +27,7 @@ macro_rules! impl_raw_constructor( $( impl $t { #[inline] - pub unsafe fn new_from_raw($($r:$rt),+) -> $t { + pub unsafe fn from_ll($($r:$rt),+) -> $t { $te { $($r: $r),+ } } } diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index ba892a28..f3b5516b 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -167,7 +167,7 @@ pub fn get_mouse_focus() -> Option { if raw == ptr::null() { None } else { - unsafe { Some(video::Window::new_from_raw(raw, false)) } + unsafe { Some(video::Window::from_ll(raw, false)) } } } diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 0a2d4866..53831d87 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -241,7 +241,7 @@ impl Renderer { let raw_renderer: *ll::SDL_Renderer = ptr::null(); let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.bits(), &raw_window, &raw_renderer) == 0}; if result { - let window = unsafe { Window::new_from_raw(raw_window, true) }; + let window = unsafe { Window::from_ll(raw_window, true) }; Ok(Renderer { raw: raw_renderer, parent: Some(window), diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index 289704a4..b98c4dfb 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -142,7 +142,7 @@ impl Surface { pub fn get_pixel_format(&self) -> pixels::PixelFormat { unsafe { - pixels::PixelFormat::new_from_raw((*self.raw).format) + pixels::PixelFormat::from_ll((*self.raw).format) } } diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index afe9bac2..9a035006 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -528,7 +528,7 @@ impl Window { if raw == ptr::null() { Err(get_error()) } else { - unsafe { Ok(Surface::new_from_raw(raw, false)) } //Docs say that it releases with the window + unsafe { Ok(Surface::from_ll(raw, false)) } //Docs say that it releases with the window } }