Merge pull request #106 from dpx-infinity/master

Parameterized `Renderer` by "parent" type and added accessor methods
This commit is contained in:
Tony Aldridge
2014-05-22 23:31:05 +01:00
6 changed files with 35 additions and 21 deletions
+1
View File
@@ -17,3 +17,4 @@ src/sdl2/generated/
/rustpkg_db.json
*.swp
.project
src/demo/main
+1 -1
View File
@@ -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(_) => {},
+2 -1
View File
@@ -102,7 +102,8 @@ impl Cursor {
}
}
pub fn from_surface(surface: surface::Surface, hot_x: int, hot_y: int) -> Result<Cursor, ~str> {
// 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,
hot_y as i32);
+27 -17
View File
@@ -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<S> {
pub raw: *ll::SDL_Renderer,
parent: RendererParent,
parent: Option<S>,
pub owned: bool
}
impl Drop for Renderer {
#[unsafe_destructor]
impl<S> Drop for Renderer<S> {
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<Renderer, ~str> {
impl Renderer<Window> {
pub fn from_window(window: Window, index: RenderDriverIndex, renderer_flags: RendererFlags) -> Result<Renderer<Window>, ~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<Renderer, ~str> {
pub fn new_with_window(width: int, height: int, window_flags: video::WindowFlags) -> Result<Renderer<Window>, ~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<Renderer, ~str> {
impl Renderer<Surface> {
pub fn from_surface(surface: surface::Surface) -> Result<Renderer<Surface>, ~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<S> Renderer<S> {
#[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 {
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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