Merge pull request #7 from sbidin/master

Update to newest Rust and Rust-SDL2.
This commit is contained in:
ShuYu Wang
2015-03-23 19:23:51 +08:00
7 changed files with 85 additions and 93 deletions
+8 -6
View File
@@ -1,25 +1,27 @@
[package]
name = "sdl2_gfx"
version = "0.0.3"
authors = [ "ShuYu Wang <andelf@gmail.com>" ]
authors = ["ShuYu Wang <andelf@gmail.com>"]
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"
Regular → Executable
+37 -50
View File
@@ -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(())
}
+7 -7
View File
@@ -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 }
}
}
+7 -4
View File
@@ -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<u8> {
fn cvec_with_size(sz: usize) -> CVec<u8> {
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<u8>, tmin: u8, tmax: u8) -> SdlResult<CVec<u8>>
}
/// Filter using NormalizeLinear: D = saturation255((Nmax - Nmin)/(Cmax - Cmin)*(S - Cmin) + Nmin).
pub fn normalize_linear(src1: CVec<u8>, cmin: int, cmax: int, nmin: int, nmax: int) -> SdlResult<CVec<u8>> {
pub fn normalize_linear(src1: CVec<u8>, cmin: isize, cmax: isize, nmin: isize, nmax: isize) -> SdlResult<CVec<u8>> {
let size = src1.len();
let dest = cvec_with_size(size);
let ret = unsafe { ll::SDL_imageFilterNormalizeLinear(mem::transmute(src1.get(0)),
+4 -4
View File
@@ -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")]
+11 -11
View File
@@ -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<C: ToColor>(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>;
fn filled_polygon<C: ToColor>(&self, vx: &[i16], vy: &[i16], color: C) -> SdlResult<()>;
fn textured_polygon<C: ToColor>(&self, vx: &[i16], vy: &[i16], texture: &Surface, texture_dx: i16, texture_dy: i16, color: C) -> SdlResult<()>;
fn bezier<C: ToColor>(&self, vx: &[i16], vy: &[i16], s: int, color: C) -> SdlResult<()>;
fn bezier<C: ToColor>(&self, vx: &[i16], vy: &[i16], s: isize, color: C) -> SdlResult<()>;
fn character<C: ToColor>(&self, x: i16, y: i16, c: char, color: C) -> SdlResult<()>;
fn string<C: ToColor>(&self, x: i16, y: i16, s: &str, color: C) -> SdlResult<()>;
}
@@ -461,7 +462,7 @@ impl DrawRenderer for Renderer {
unimplemented!()
}
fn bezier<C: ToColor>(&self, vx: &[i16], vy: &[i16], s: int, color: C) -> SdlResult<()> {
fn bezier<C: ToColor>(&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<C: ToColor>(&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) }
}
+11 -11
View File
@@ -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<Surface>;
/// Shrink a surface by an integer ratio using averaging.
fn shrink(&self, factorx: int, factory: int) -> SdlResult<Surface>;
fn shrink(&self, factorx: isize, factory: isize) -> SdlResult<Surface>;
/// Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
fn rotate_90deg(&self, turns: int) -> SdlResult<Surface>;
fn rotate_90deg(&self, turns: isize) -> SdlResult<Surface>;
}
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<Surface> {
fn shrink(&self, factorx: isize, factory: isize) -> SdlResult<Surface> {
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<Surface> {
fn rotate_90deg(&self, turns: isize) -> SdlResult<Surface> {
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)
}