diff --git a/Cargo.toml b/Cargo.toml index 02ed77c1..0f3bfa73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,25 +1,27 @@ [package] - name = "sdl2_gfx" version = "0.0.3" -authors = [ "ShuYu Wang " ] +authors = ["ShuYu Wang "] description = "SDL2_gfx bindings for Rust" homepage = "https://github.com/andelf/rust-sdl2_gfx" repository = "https://github.com/andelf/rust-sdl2_gfx" readme = "README.md" -keywords = [ "SDL", "windowing", "graphics" ] +keywords = ["SDL", "windowing", "graphics"] license = "MIT" [lib] - name = "sdl2_gfx" path = "src/sdl2_gfx/lib.rs" [dependencies] +c_vec = "^1.0.0" -sdl2 = "~0.0.3" +[dependencies.sdl2] +git = "https://github.com/AngryLawyer/rust-sdl2/" + +[dependencies.sdl2-sys] +git = "https://github.com/AngryLawyer/rust-sdl2/" [[bin]] - name = "demo" path = "src/demo/gfx_demo.rs" diff --git a/src/demo/gfx_demo.rs b/src/demo/gfx_demo.rs old mode 100644 new mode 100755 index cd4287f4..c760d47c --- a/src/demo/gfx_demo.rs +++ b/src/demo/gfx_demo.rs @@ -1,93 +1,80 @@ -#![feature(macro_rules)] - -extern crate rand; extern crate sdl2; extern crate sdl2_gfx; -use rand::Rand; -use sdl2::event; use sdl2::event::Event; use sdl2::pixels; use sdl2::keycode::KeyCode; - use sdl2_gfx::primitives::DrawRenderer; -use sdl2_gfx::framerate::FPSManager; -static SCREEN_WIDTH : int = 800; -static SCREEN_HEIGHT : int = 600; +static SCREEN_WIDTH: i32 = 800; +static SCREEN_HEIGHT: i32 = 600; // hadle the annoying Rect i32 macro_rules! rect( ($x:expr, $y:expr, $w:expr, $h:expr) => ( sdl2::rect::Rect::new($x as i32, $y as i32, $w as i32, $h as i32) ) -) +); fn main() { - match mainloop() { - Ok(_) => (), - Err(e) => println!("error while running mainloop: {}", e), - } -} + let context = sdl2::init(sdl2::INIT_VIDEO).unwrap(); -fn mainloop() -> Result<(), String> { - sdl2::init(sdl2::INIT_VIDEO); + let window = sdl2::video::Window::new( + "rust-sdl2_gfx: draw line & FPSManager", + sdl2::video::WindowPos::PosCentered, + sdl2::video::WindowPos::PosCentered, + SCREEN_WIDTH, + SCREEN_HEIGHT, + sdl2::video::OPENGL).unwrap(); - let window = try!(sdl2::video::Window::new( - "rust-sdl2_gfx: draw line & FPSManager", sdl2::video::WindowPos::PosCentered, - sdl2::video::WindowPos::PosCentered, SCREEN_WIDTH, SCREEN_HEIGHT, - sdl2::video::OPENGL)); + let renderer = sdl2::render::Renderer::from_window( + window, + sdl2::render::RenderDriverIndex::Auto, + sdl2::render::ACCELERATED).unwrap(); - let renderer = try!(sdl2::render::Renderer::from_window( - window, sdl2::render::RenderDriverIndex::Auto, sdl2::render::ACCELERATED)); + let mut drawer = renderer.drawer(); + drawer.set_draw_color(pixels::Color::RGB(0, 0, 0)); + drawer.clear(); + drawer.present(); - try!(renderer.set_draw_color(pixels::Color::RGB(0, 0, 0))); + let mut lastx = 0; + let mut lasty = 0; - try!(renderer.clear()); + let mut events = context.event_pump(); - renderer.present(); + 'main: loop { - let mut rng = rand::XorShiftRng::new_unseeded(); - let (mut lastx, mut lasty) = (0, 0); + for event in events.poll_iter() { - let mut fpsm = FPSManager::new(); - // default 30 is not good - try!(fpsm.set_framerate(100)); + match event { - 'main : loop { - 'event : loop { - // this will avoid program to run 100% CPU - fpsm.delay(); + Event::Quit {..} => break 'main, - match event::poll_event() { - Event::Quit(_) => break 'main, - Event::KeyDown(_, _, key, _, _, _) => { - if key == KeyCode::Escape { + Event::KeyDown {keycode, ..} => { + if keycode == KeyCode::Escape { break 'main - } else if key == KeyCode::Space { - for i in range(0u, 400) { - try!(renderer.pixel(i as i16, i as i16, 0xFF000FFu32)); + } else if keycode == KeyCode::Space { + for i in 0..400 { + let _ = renderer.pixel(i as i16, i as i16, 0xFF000FFu32); } - renderer.present(); + drawer.present(); } } - Event::MouseButtonDown(_, _, _, _, x, y) => { - let color : pixels::Color = Rand::rand(&mut rng); - // println!("color => {:}", color); - try!(renderer.line(lastx, lasty, x as i16, y as i16, color)); + + Event::MouseButtonDown {x, y, ..} => { + let color = pixels::Color::RGB(x as u8, y as u8, 255); + let _ = renderer.line(lastx, lasty, x as i16, y as i16, color); lastx = x as i16; lasty = y as i16; println!("mouse btn down at ({},{})", x, y); - renderer.present(); + drawer.present(); } _ => {} } } } - sdl2::quit(); - Ok(()) } diff --git a/src/sdl2_gfx/framerate.rs b/src/sdl2_gfx/framerate.rs index 540f75a4..3e1526e6 100644 --- a/src/sdl2_gfx/framerate.rs +++ b/src/sdl2_gfx/framerate.rs @@ -44,27 +44,27 @@ impl FPSManager { } /// Set the framerate in Hz. - pub fn set_framerate(&mut self, rate: uint) -> SdlResult<()> { + pub fn set_framerate(&mut self, rate: u32) -> SdlResult<()> { let ret = unsafe { ll::SDL_setFramerate(self.raw, rate as uint32_t) }; if ret == 0 { Ok(()) } else { Err("set_framerate error: beyond lower/upper limit.".to_string()) } } /// Return the current target framerate in Hz. - pub fn get_framerate(&self) -> int { + pub fn get_framerate(&self) -> i32 { // will not get an error - unsafe { ll::SDL_getFramerate(self.raw) as int } + unsafe { ll::SDL_getFramerate(self.raw) as i32 } } /// Return the current framecount. - pub fn get_frame_count(&self) -> int { + pub fn get_frame_count(&self) -> i32 { // will not get an error - unsafe { ll::SDL_getFramecount(self.raw) as int } + unsafe { ll::SDL_getFramecount(self.raw) as i32 } } /// Delay execution to maintain a constant framerate and calculate fps. - pub fn delay(&mut self) -> uint { - unsafe { ll::SDL_framerateDelay(self.raw) as uint } + pub fn delay(&mut self) -> u32 { + unsafe { ll::SDL_framerateDelay(self.raw) as u32 } } } diff --git a/src/sdl2_gfx/imagefilter.rs b/src/sdl2_gfx/imagefilter.rs index e873ba3b..8d518fb9 100644 --- a/src/sdl2_gfx/imagefilter.rs +++ b/src/sdl2_gfx/imagefilter.rs @@ -1,10 +1,13 @@ //! MMX image filters +extern crate c_vec; + use std::mem; -use std::c_vec::CVec; +use std::ptr::Unique; use libc; use libc::{size_t, c_void, c_uint, c_int}; use sdl2::SdlResult; +use self::c_vec::CVec; mod ll { /* automatically generated by rust-bindgen */ @@ -102,10 +105,10 @@ pub fn mmx_on() { } #[inline] -fn cvec_with_size(sz: uint) -> CVec { +fn cvec_with_size(sz: usize) -> CVec { unsafe { let p = libc::malloc(sz as size_t) as *mut u8; - CVec::new_with_dtor(p, sz, move || { + CVec::new_with_dtor(Unique::new(p), sz, move |p| { libc::free(p as *mut c_void) }) } @@ -420,7 +423,7 @@ pub fn clip_to_range(src1: CVec, tmin: u8, tmax: u8) -> SdlResult> } /// Filter using NormalizeLinear: D = saturation255((Nmax - Nmin)/(Cmax - Cmin)*(S - Cmin) + Nmin). -pub fn normalize_linear(src1: CVec, cmin: int, cmax: int, nmin: int, nmax: int) -> SdlResult> { +pub fn normalize_linear(src1: CVec, cmin: isize, cmax: isize, nmin: isize, nmax: isize) -> SdlResult> { let size = src1.len(); let dest = cvec_with_size(size); let ret = unsafe { ll::SDL_imageFilterNormalizeLinear(mem::transmute(src1.get(0)), diff --git a/src/sdl2_gfx/lib.rs b/src/sdl2_gfx/lib.rs index 7d178bfd..c8d2de0f 100644 --- a/src/sdl2_gfx/lib.rs +++ b/src/sdl2_gfx/lib.rs @@ -4,14 +4,14 @@ A binding for SDL2_gfx. #![crate_name="sdl2_gfx"] #![crate_type = "lib"] -#![desc = "SDL2_gfx bindings and wrappers"] -#![comment = "SDL2_gfx bindings and wrappers"] -#![license = "MIT"] -#![feature(globs, macro_rules)] +#![feature(libc)] +#![feature(unique)] +#![feature(core)] extern crate libc; extern crate sdl2; +extern crate "sdl2-sys" as sys; // Setup linking for all targets. #[link(name="SDL2_gfx")] diff --git a/src/sdl2_gfx/primitives.rs b/src/sdl2_gfx/primitives.rs index e139b71a..ff520f7e 100644 --- a/src/sdl2_gfx/primitives.rs +++ b/src/sdl2_gfx/primitives.rs @@ -2,6 +2,7 @@ use std::mem; use std::ptr; +use std::ffi::CString; use std::num::ToPrimitive; use libc::{c_void, c_int, c_char}; use sdl2::render::Renderer; @@ -15,8 +16,8 @@ mod ll { /* automatically generated by rust-bindgen */ use libc::*; - use sdl2::render::ll::SDL_Renderer; - use sdl2::surface::ll::SDL_Surface; + use sys::render::SDL_Renderer; + use sys::surface::SDL_Surface; extern "C" { pub fn pixelColor(renderer: *const SDL_Renderer, x: int16_t, y: int16_t, color: uint32_t) -> c_int; @@ -226,7 +227,7 @@ impl ToColor for u32 { } // for 0xXXXXXXXX -impl ToColor for int { +impl ToColor for isize { #[inline] fn as_rgba(&self) -> (u8, u8, u8, u8) { unsafe { mem::transmute(self.to_u32().expect("Can't convert to Color Type")) } @@ -266,7 +267,7 @@ pub trait DrawRenderer { fn aa_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>; fn filled_polygon(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>; fn textured_polygon(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> SdlResult<()>; - fn bezier(&self, vx: &[i16], vy: &[i16], s: int, color: C) -> SdlResult<()>; + fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> SdlResult<()>; fn character(&self, x: i16, y: i16, c: char, color: C) -> SdlResult<()>; fn string(&self, x: i16, y: i16, s: &str, color: C) -> SdlResult<()>; } @@ -461,7 +462,7 @@ impl DrawRenderer for Renderer { unimplemented!() } - fn bezier(&self, vx: &[i16], vy: &[i16], s: int, color: C) -> SdlResult<()> { + fn bezier(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> SdlResult<()> { assert_eq!(vx.len(), vy.len()); let n = vx.len() as c_int; let ret = unsafe { @@ -481,9 +482,8 @@ impl DrawRenderer for Renderer { fn string(&self, x: i16, y: i16, s: &str, color: C) -> SdlResult<()> { let ret = unsafe { - s.with_c_str(|buf| { - ll::stringColor(self.raw(), x, y, buf as *mut i8, color.as_u32()) - }) + let buf = CString::new(s).unwrap().as_bytes().as_ptr(); + ll::stringColor(self.raw(), x, y, buf as *mut i8, color.as_u32()) }; if ret == 0 { Ok(()) } else { Err(get_error()) } @@ -491,17 +491,17 @@ impl DrawRenderer for Renderer { } /// Sets or resets the current global font data. -pub fn set_font(fontdata: Option<&[u8]>, cw: uint, ch: uint) { +pub fn set_font(fontdata: Option<&[u8]>, cw: u32, ch: u32) { let actual_fontdata = match fontdata { None => ptr::null(), Some(v) => v.as_ptr() }; unsafe { - ll::gfxPrimitivesSetFont(actual_fontdata as *const c_void, cw as u32, ch as u32) + ll::gfxPrimitivesSetFont(actual_fontdata as *const c_void, cw, ch) } } /// Sets current global font character rotation steps. -pub fn set_font_rotation(rotation: uint) { +pub fn set_font_rotation(rotation: u32) { unsafe { ll::gfxPrimitivesSetFontRotation(rotation as u32) } } diff --git a/src/sdl2_gfx/rotozoom.rs b/src/sdl2_gfx/rotozoom.rs index 160dbd1d..ba35a15e 100644 --- a/src/sdl2_gfx/rotozoom.rs +++ b/src/sdl2_gfx/rotozoom.rs @@ -10,7 +10,7 @@ mod ll { /* automatically generated by rust-bindgen */ use libc::*; - use sdl2::surface::ll::SDL_Surface; + use sys::surface::SDL_Surface; extern "C" { pub fn rotozoomSurface(src: *const SDL_Surface, angle: c_double, zoom: c_double, smooth: c_int) -> *const SDL_Surface; @@ -45,9 +45,9 @@ pub trait RotozoomSurface { /// Zoom a surface by independent horizontal and vertical factors with optional smoothing. fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> SdlResult; /// Shrink a surface by an integer ratio using averaging. - fn shrink(&self, factorx: int, factory: int) -> SdlResult; + fn shrink(&self, factorx: isize, factory: isize) -> SdlResult; /// Rotates a 8/16/24/32 bit surface in increments of 90 degrees. - fn rotate_90deg(&self, turns: int) -> SdlResult; + fn rotate_90deg(&self, turns: isize) -> SdlResult; } impl RotozoomSurface for Surface { @@ -81,7 +81,7 @@ impl RotozoomSurface for Surface { unsafe { Ok(Surface::from_ll(raw, true)) } } } - fn shrink(&self, factorx: int, factory: int) -> SdlResult { + fn shrink(&self, factorx: isize, factory: isize) -> SdlResult { let raw = unsafe { ll::shrinkSurface(self.raw(), factorx as c_int, factory as c_int) }; @@ -91,7 +91,7 @@ impl RotozoomSurface for Surface { unsafe { Ok(Surface::from_ll(raw, true)) } } } - fn rotate_90deg(&self, turns: int) -> SdlResult { + fn rotate_90deg(&self, turns: isize) -> SdlResult { let raw = unsafe { ll::rotateSurface90Degrees(self.raw(), turns as c_int) }; @@ -103,23 +103,23 @@ impl RotozoomSurface for Surface { } } -pub fn get_zoom_size(width: int, height: int, zoomx: f64, zoomy: f64) -> (int, int) { +pub fn get_zoom_size(width: isize, height: isize, zoomx: f64, zoomy: f64) -> (isize, isize) { let mut w: c_int = 0; let mut h: c_int = 0; unsafe { ll::zoomSurfaceSize(width as c_int, height as c_int, zoomx, zoomy, &mut w, &mut h) } - (w as int, h as int) + (w as isize, h as isize) } -pub fn get_rotozoom_size(width: int, height: int, angle: f64, zoom: f64) -> (int, int) { +pub fn get_rotozoom_size(width: isize, height: isize, angle: f64, zoom: f64) -> (isize, isize) { let mut w: c_int = 0; let mut h: c_int = 0; unsafe { ll::rotozoomSurfaceSize(width as c_int, height as c_int, angle, zoom, &mut w, &mut h) } - (w as int, h as int) + (w as isize, h as isize) } -pub fn get_rotozoom_xy_size(width: int, height: int, angle: f64, zoomx: f64, zoomy: f64) -> (int, int) { +pub fn get_rotozoom_xy_size(width: isize, height: isize, angle: f64, zoomx: f64, zoomy: f64) -> (isize, isize) { let mut w: c_int = 0; let mut h: c_int = 0; unsafe { ll::rotozoomSurfaceSizeXY(width as c_int, height as c_int, angle, zoomx, zoomy, &mut w, &mut h) } - (w as int, h as int) + (w as isize, h as isize) }