diff --git a/.gitignore b/.gitignore index deb7f379..b746f4ea 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ src/sdl2/generated/ /rustpkg_db.json *.swp .project +src/demo/main diff --git a/src/codegen/main.rs b/src/codegen/main.rs index 4929d651..e4e1a306 100644 --- a/src/codegen/main.rs +++ b/src/codegen/main.rs @@ -21,7 +21,7 @@ fn main() { os::set_exit_status(1); }, 3 => { - let output_dir = GenericPath::new(args.get(2).clone()); + let output_dir = GenericPath::new(args.get(2).as_slice()); match mkdir_recursive(&output_dir, UserDir) { Err(e) => fail!("Could not create directory for generated sources: {:s}", e.desc), Ok(_) => {}, diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index a6e9d653..7753e1e6 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -102,7 +102,8 @@ impl Cursor { } } - pub fn from_surface(surface: surface::Surface, hot_x: int, hot_y: int) -> Result { + // 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, hot_y as i32); diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 14b1aae4..c9750824 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -1,5 +1,7 @@ use video; +use video::Window; use surface; +use surface::Surface; use pixels; use get_error; use std::ptr; @@ -198,20 +200,15 @@ impl RendererInfo { } } -#[deriving(Eq)] -enum RendererParent { - Window(video::Window), - Surface(surface::Surface), -} - #[deriving(Eq)] #[allow(raw_pointer_deriving)] -pub struct Renderer { +pub struct Renderer { pub raw: *ll::SDL_Renderer, - parent: RendererParent, + parent: Option, pub owned: bool } -impl Drop for Renderer { +#[unsafe_destructor] +impl Drop for Renderer { fn drop(&mut self) { if self.owned { unsafe { @@ -221,8 +218,8 @@ impl Drop for Renderer { } } -impl Renderer { - pub fn from_window(window: video::Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> Result { +impl Renderer { + pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> Result, ~str> { let index = match index { DriverAuto => -1, DriverIndex(x) => x @@ -235,41 +232,54 @@ impl Renderer { if raw == ptr::null() { Err(get_error()) } else { - Ok(Renderer{ raw: raw, parent: Window(window), owned: true,}) + Ok(Renderer{ raw: raw, parent: Some(window), owned: true,}) } } - pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> Result { + pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> Result, ~str> { let raw_window: *video::ll::SDL_Window = ptr::null(); 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 = video::Window { + let window = Window { raw: raw_window, owned: true }; Ok(Renderer { raw: raw_renderer, - parent: Window(window), + parent: Some(window), owned: true }) } else { Err(get_error()) } } +} - pub fn from_surface(surface: surface::Surface) -> Result { +impl Renderer { + pub fn from_surface(surface: surface::Surface) -> Result, ~str> { let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw) }; if result == ptr::null() { Ok(Renderer { raw: result, - parent: Surface(surface), + parent: Some(surface), owned: true }) } else { Err(get_error()) } } +} + +impl Renderer { + #[inline] + pub fn get_parent<'a>(&'a self) -> &'a S { self.parent.get_ref() } + + #[inline] + pub fn unwrap_parent(mut self) -> S { + use std::mem; + mem::replace(&mut self.parent, None).unwrap() + } pub fn set_draw_color(&self, color: pixels::Color) -> Result<(), ~str> { let ret = match color { diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index e0c57436..70d03e14 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -82,7 +82,8 @@ bitflags!(flags SurfaceFlag: u32 { static DontFree = ll::SDL_DONTFREE as u32 }) -#[deriving(Eq)] #[allow(raw_pointer_deriving)] +#[deriving(Eq)] +#[allow(raw_pointer_deriving)] pub struct Surface { pub raw: *ll::SDL_Surface, pub owned: bool diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 006e555a..d7a5a943 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -312,7 +312,8 @@ impl Drop for GLContext { } -#[deriving(Eq)] #[allow(raw_pointer_deriving)] +#[deriving(Eq)] +#[allow(raw_pointer_deriving)] pub struct Window { pub raw: *ll::SDL_Window, pub owned: bool