From 1da785c45362a37e96170b0b2957e22bd7f7e64f Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 18 May 2014 15:09:51 +0400 Subject: [PATCH 1/5] Rewrote Renderer to Renderer --- src/sdl2/mouse.rs | 2 +- src/sdl2/render.rs | 36 +++++++++++++++++++----------------- src/sdl2/surface.rs | 3 ++- src/sdl2/video.rs | 3 ++- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index a6e9d653..b5c56f6f 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -102,7 +102,7 @@ impl Cursor { } } - pub fn from_surface(surface: surface::Surface, hot_x: int, hot_y: int) -> Result { + 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..a855d379 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,14 @@ impl RendererInfo { } } -#[deriving(Eq)] -enum RendererParent { - Window(video::Window), - Surface(surface::Surface), -} - #[deriving(Eq)] #[allow(raw_pointer_deriving)] -pub struct Renderer { - pub raw: *ll::SDL_Renderer, - parent: RendererParent, - pub owned: bool +pub struct Renderer { + raw: *ll::SDL_Renderer, + parent: S, + owned: bool } -impl Drop for Renderer { +impl Drop for Renderer { fn drop(&mut self) { if self.owned { unsafe { @@ -221,8 +217,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,16 +231,16 @@ impl Renderer { if raw == ptr::null() { Err(get_error()) } else { - Ok(Renderer{ raw: raw, parent: Window(window), owned: true,}) + Ok(Renderer{ raw: raw, parent: 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 }; @@ -258,7 +254,7 @@ impl Renderer { } } - pub fn from_surface(surface: surface::Surface) -> Result { + pub fn from_surface(surface: &surface::Surface) -> Result { let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw) }; if result == ptr::null() { Ok(Renderer { @@ -271,6 +267,12 @@ impl Renderer { } } + #[inline] + pub fn get_parent(&'a self) -> &'a S { &self.parent } + + #[inline] + pub fn unwrap_parent(self) -> S { self.parent } + pub fn set_draw_color(&self, color: pixels::Color) -> Result<(), ~str> { let ret = match color { pixels::RGB(r, g, b) => { 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 From 36a07542570798b9fc67b841c653384c88eff660 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 18 May 2014 15:21:31 +0400 Subject: [PATCH 2/5] Fixed codegen to use as_slice() instead of clone() for arguments --- src/codegen/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(_) => {}, From e83d9491ae5d165ea70f6888fe72f4a46b479861 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 18 May 2014 15:49:38 +0400 Subject: [PATCH 3/5] Using Option in Renderer to avoid problems with destructor --- .gitignore | 1 + src/sdl2/render.rs | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 393829d9..a78fd338 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ src/generated /rustpkg_db.json *.swp .project +src/demo/main diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index a855d379..1c5a31d7 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -203,10 +203,11 @@ impl RendererInfo { #[deriving(Eq)] #[allow(raw_pointer_deriving)] pub struct Renderer { raw: *ll::SDL_Renderer, - parent: S, + parent: Option, owned: bool } +#[unsafe_destructor] impl Drop for Renderer { fn drop(&mut self) { if self.owned { @@ -217,7 +218,7 @@ impl Drop for Renderer { } } -impl Renderer { +impl Renderer { pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> Result, ~str> { let index = match index { DriverAuto => -1, @@ -231,7 +232,7 @@ impl Renderer { if raw == ptr::null() { Err(get_error()) } else { - Ok(Renderer{ raw: raw, parent: window, owned: true,}) + Ok(Renderer{ raw: raw, parent: Some(window), owned: true,}) } } @@ -246,32 +247,39 @@ impl Renderer { }; 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 get_parent(&'a self) -> &'a S { &self.parent } - - #[inline] - pub fn unwrap_parent(self) -> S { self.parent } + 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 { From eed8bf6110afeca9c12036aa9fab569fd875cf27 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 18 May 2014 15:53:54 +0400 Subject: [PATCH 4/5] Added TODO note in mouse handling code --- src/sdl2/mouse.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index b5c56f6f..7753e1e6 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -102,6 +102,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, From dffb5e89810cf5adfaba2bfb771c5f64d99ec069 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 18 May 2014 23:27:35 +0400 Subject: [PATCH 5/5] Reverted accidental removal of pub modifier in Renderer --- src/sdl2/render.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 1c5a31d7..c9750824 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 { - raw: *ll::SDL_Renderer, + pub raw: *ll::SDL_Renderer, parent: Option, - owned: bool + pub owned: bool } #[unsafe_destructor]