mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
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
This commit is contained in:
+6
-3
@@ -231,7 +231,7 @@ impl<'a> AudioSpec<'a> {
|
||||
let audio_buf = ptr::null::<u8>();
|
||||
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 {
|
||||
|
||||
@@ -71,7 +71,7 @@ pub fn get_keyboard_focus() -> Option<Window> {
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
)+
|
||||
)
|
||||
)
|
||||
+3
-3
@@ -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<Cursor, ~str> {
|
||||
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<video::Window> {
|
||||
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) {
|
||||
|
||||
+7
-2
@@ -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,
|
||||
|
||||
+12
-9
@@ -202,9 +202,9 @@ impl RendererInfo {
|
||||
|
||||
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
|
||||
pub struct Renderer<S> {
|
||||
pub raw: *ll::SDL_Renderer,
|
||||
raw: *ll::SDL_Renderer,
|
||||
parent: Option<S>,
|
||||
pub owned: bool
|
||||
owned: bool
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
@@ -226,7 +226,7 @@ impl Renderer<Window> {
|
||||
};
|
||||
|
||||
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<Window> {
|
||||
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<Window> {
|
||||
|
||||
impl Renderer<Surface> {
|
||||
pub fn from_surface(surface: surface::Surface) -> Result<Renderer<Surface>, ~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<S> Renderer<S> {
|
||||
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<S> Renderer<S> {
|
||||
}
|
||||
|
||||
pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> Result<Texture, ~str> {
|
||||
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 {
|
||||
|
||||
+5
-2
@@ -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<RWops, ~str> {
|
||||
|
||||
+11
-7
@@ -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<Surface, ~str> {
|
||||
@@ -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<Surface, ~str> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-7
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user