diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index af6f8a05..4c38c227 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -141,19 +141,19 @@ impl RendererInfo { } } -pub enum RendererParent { - Surface(Surface), +pub enum RendererParent<'a> { + Surface(Surface<'a>), Window(Window) } /// 2D rendering context -pub struct Renderer { +pub struct Renderer<'a> { raw: *const ll::SDL_Renderer, - parent: Option, + parent: Option>, is_alive: Rc> } -impl Drop for Renderer { +impl<'a> Drop for Renderer<'a> { fn drop(&mut self) { unsafe { *self.is_alive.get() = false; @@ -162,9 +162,9 @@ impl Drop for Renderer { } } -impl Renderer { +impl<'a> Renderer<'a> { /// Creates a 2D rendering context for a window. - pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> SdlResult { + pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> SdlResult> { let index = match index { RenderDriverIndex::Auto => -1, RenderDriverIndex::Index(x) => x @@ -184,7 +184,7 @@ impl Renderer { } /// Creates a window and default renderer. - pub fn new_with_window(_sdl: &Sdl, width: i32, height: i32, window_flags: video::WindowFlags) -> SdlResult { + pub fn new_with_window(_sdl: &Sdl, width: i32, height: i32, window_flags: video::WindowFlags) -> SdlResult> { use sys::video::SDL_Window; let raw_window: *const SDL_Window = ptr::null(); @@ -201,7 +201,7 @@ impl Renderer { } /// Creates a 2D software rendering context for a surface. - pub fn from_surface(surface: surface::Surface) -> SdlResult { + pub fn from_surface(surface: surface::Surface<'a>) -> SdlResult> { let raw_renderer = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw()) }; if raw_renderer != ptr::null() { unsafe { @@ -256,7 +256,7 @@ impl Renderer { } #[inline] - pub fn unwrap_parent(mut self) -> RendererParent { + pub fn unwrap_parent(mut self) -> RendererParent<'a> { use std::mem; mem::replace(&mut self.parent, None).unwrap() } @@ -270,7 +270,7 @@ impl Renderer { } #[inline] - pub fn unwrap_parent_as_surface(self) -> Option { + pub fn unwrap_parent_as_surface(self) -> Option> { match self.unwrap_parent() { RendererParent::Surface(surface) => Some(surface), _ => None @@ -310,7 +310,7 @@ impl Renderer { } /// Texture-creating methods for the renderer -impl Renderer { +impl<'a> Renderer<'a> { /// Creates a texture for a rendering context. /// /// `size` is the width and height of the texture. diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index e0700a36..da96ab6d 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -1,3 +1,4 @@ +use std::marker::PhantomData; use std::mem; use std::path::Path; use rect::Rect; @@ -21,14 +22,13 @@ bitflags! { } } -#[derive(PartialEq)] -#[allow(raw_pointer_derive, missing_copy_implementations)] -pub struct Surface { +pub struct Surface<'a> { raw: *const ll::SDL_Surface, - owned: bool + owned: bool, + _marker: PhantomData<&'a ()> } -impl Drop for Surface { +impl<'a> Drop for Surface<'a> { fn drop(&mut self) { if self.owned { unsafe { @@ -38,13 +38,21 @@ impl Drop for Surface { } } -impl_raw_accessors!((Surface, *const ll::SDL_Surface)); -impl_owned_accessors!((Surface, owned)); -impl_raw_constructor!((Surface, Surface (raw: *const ll::SDL_Surface, owned: bool))); +impl<'a> Surface<'a> { + pub unsafe fn raw(&self) -> *const ll::SDL_Surface { self.raw } + + pub unsafe fn owned(&self) -> bool { self.owned } + + pub unsafe fn from_ll<'b>(raw: *const ll::SDL_Surface, owned: bool) -> Surface<'b> { + Surface { + raw: raw, + owned: owned, + _marker: PhantomData + } + } -impl Surface { pub fn new(surface_flags: SurfaceFlag, width: i32, height: i32, bpp: i32, - rmask: u32, gmask: u32, bmask: u32, amask: u32) -> SdlResult { + rmask: u32, gmask: u32, bmask: u32, amask: u32) -> SdlResult> { unsafe { let raw = ll::SDL_CreateRGBSurface(surface_flags.bits(), width as c_int, height as c_int, bpp as c_int, rmask, gmask, bmask, amask); @@ -52,13 +60,17 @@ impl Surface { if raw == ptr::null() { Err(get_error()) } else { - Ok(Surface { raw: raw, owned: true }) + Ok(Surface { + raw: raw, + owned: true, + _marker: PhantomData + }) } } } - pub fn from_data(data: &mut [u8], width: i32, height: i32, bpp: i32, pitch: i32, - rmask: u32, gmask: u32, bmask: u32, amask: u32) -> SdlResult { + pub fn from_data(data: &'a mut [u8], width: i32, height: i32, bpp: i32, pitch: i32, + rmask: u32, gmask: u32, bmask: u32, amask: u32) -> SdlResult> { unsafe { let raw = ll::SDL_CreateRGBSurfaceFrom( @@ -68,7 +80,11 @@ impl Surface { if raw == ptr::null() { Err(get_error()) } else { - Ok(Surface { raw: raw, owned: true }) + Ok(Surface { + raw: raw, + owned: true, + _marker: PhantomData + }) } } } @@ -126,13 +142,20 @@ impl Surface { unsafe { ll::SDL_UnlockSurface(self.raw); } } - pub fn from_bmp(path: &Path) -> SdlResult { + pub fn from_bmp(path: &Path) -> SdlResult> { let raw = unsafe { ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw(), 0) }; - if raw.is_null() { Err(get_error()) } - else { Ok(Surface{raw: raw, owned: true}) } + if raw.is_null() { + Err(get_error()) + } else { + Ok(Surface { + raw: raw, + owned: true, + _marker: PhantomData + }) + } } pub fn save_bmp(&self, path: &Path) -> SdlResult<()> { @@ -307,7 +330,7 @@ impl Surface { rect } - pub fn convert(&self, format: &pixels::PixelFormat) -> SdlResult { + pub fn convert(&self, format: &pixels::PixelFormat) -> SdlResult> { // SDL_ConvertSurface takes a flag as the last parameter, which should be 0 by the docs. let surface_ptr = unsafe { ll::SDL_ConvertSurface(self.raw, format.raw(), 0u32) }; @@ -318,7 +341,7 @@ impl Surface { } } - pub fn convert_format(&self, format: pixels::PixelFormatEnum) -> SdlResult { + pub fn convert_format(&self, format: pixels::PixelFormatEnum) -> SdlResult> { let surface_ptr = unsafe { ll::SDL_ConvertSurfaceFormat(self.raw, format as uint32_t, 0u32) }; if surface_ptr == ptr::null() {