add doc; fix error in impl ToColor for pixels::Color

This commit is contained in:
Andelf
2014-04-16 19:22:34 +08:00
parent 56226ffcdf
commit daa7ad486e
3 changed files with 49 additions and 22 deletions
+13 -8
View File
@@ -23,17 +23,13 @@ mod ll {
}
}
/// Structure holding the state and timing information of the framerate controller.
pub struct FPSManager {
raw: *mut ll::FPSmanager,
}
impl Drop for FPSManager {
fn drop(&mut self) {
unsafe { libc::free(self.raw as *mut c_void) }
}
}
impl FPSManager {
/// Create the framerate manager.
pub fn new() -> FPSManager {
unsafe {
let raw = libc::malloc(mem::size_of::<ll::FPSmanager>() as u64) as *mut ll::FPSmanager;
@@ -42,24 +38,33 @@ impl FPSManager {
}
}
pub fn set_framerate(&mut self, rate: int) -> Result<(), ~str> {
/// Set the framerate in Hz.
pub fn set_framerate(&mut self, rate: uint) -> Result<(), ~str> {
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.") }
}
/// Return the current target framerate in Hz.
pub fn get_framerate(&self) -> int {
// will not get an error
unsafe { ll::SDL_getFramerate(self.raw) as int }
}
/// Return the current framecount.
pub fn get_framecount(&self) -> int {
// will not get an error
unsafe { ll::SDL_getFramecount(self.raw) as int }
}
/// Delay execution to maintain a constant framerate and calculate fps.
pub fn framerate_delay(&mut self) -> uint {
unsafe { ll::SDL_framerateDelay(self.raw) as uint }
}
}
impl Drop for FPSManager {
fn drop(&mut self) {
unsafe { libc::free(self.raw as *mut c_void) }
}
}
+30 -14
View File
@@ -1,5 +1,6 @@
use std::cast;
use std::ptr;
use std::num::ToPrimitive;
use libc::{c_void, c_int, c_char};
use sdl2::render::Renderer;
use sdl2::surface::Surface;
@@ -179,8 +180,7 @@ pub trait ToColor {
#[inline]
fn as_u32(&self) -> u32 {
let (r, g, b, a) = self.as_RGBA();
(r as u32) << 24 + (g as u32) << 16 + (b as u32) << 8 + a
unsafe { cast::transmute(self.as_RGBA()) }
}
}
@@ -198,18 +198,6 @@ impl ToColor for pixels::Color {
}
}
impl ToColor for u32 {
#[inline]
fn as_RGBA(&self) -> (u8, u8, u8, u8) {
unsafe { cast::transmute(*self) }
}
#[inline]
fn as_u32(&self) -> u32 {
*self
}
}
impl ToColor for (u8, u8, u8, u8) {
#[inline]
fn as_RGBA(&self) -> (u8, u8, u8, u8) {
@@ -222,7 +210,32 @@ impl ToColor for (u8, u8, u8, u8) {
}
}
impl ToColor for u32 {
#[inline]
fn as_RGBA(&self) -> (u8, u8, u8, u8) {
unsafe { cast::transmute(*self) }
}
#[inline]
fn as_u32(&self) -> u32 {
*self
}
}
// for 0xXXXXXXXX
impl ToColor for int {
#[inline]
fn as_RGBA(&self) -> (u8, u8, u8, u8) {
unsafe { cast::transmute(self.to_u32().expect("Can't convert to Color Type")) }
}
#[inline]
fn as_u32(&self) -> u32 {
self.to_u32().expect("Can't convert to Color Type")
}
}
/// For drawing with rust-sdl2 Renderer
pub trait DrawRenderer {
fn pixel<C: ToColor>(&self, x: i16, y: i16, color: C) -> Result<(), ~str>;
fn hline<C: ToColor>(&self, x1: i16, x2: i16, y: i16, color: C) -> Result<(), ~str>;
@@ -307,6 +320,7 @@ impl DrawRenderer for Renderer {
}
fn line<C: ToColor>(&self, x1: i16, y1: i16, x2: i16, y2: i16, color: C) -> Result<(), ~str> {
let ret = unsafe {
println!("debug => {:X}", color.as_u32());
ll::lineColor(self.raw, x1, y1, x2, y2, color.as_u32())
};
if ret == 0 { Ok(()) }
@@ -472,6 +486,7 @@ impl DrawRenderer for Renderer {
}
}
/// Sets or resets the current global font data.
pub fn set_font(fontdata: Option<&[u8]>, cw: uint, ch: uint) {
let actual_fontdata = match fontdata {
None => ptr::null(),
@@ -482,6 +497,7 @@ pub fn set_font(fontdata: Option<&[u8]>, cw: uint, ch: uint) {
}
}
/// Sets current global font character rotation steps.
pub fn set_font_rotation(rotation: uint) {
unsafe { ll::gfxPrimitivesSetFontRotation(rotation as u32) }
}
+6
View File
@@ -33,11 +33,17 @@ mod ll {
}
}
/// RotozoomSurface for work with rust-sdl2 Surface type
pub trait RotozoomSurface {
/// Rotates and zooms a surface and optional anti-aliasing.
fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<~Surface, ~str>;
/// Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result<~Surface, ~str>;
/// Zoom a surface by independent horizontal and vertical factors with optional smoothing.
fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<~Surface, ~str>;
/// Shrink a surface by an integer ratio using averaging.
fn shrink(&self, factorx: int, factory: int) -> Result<~Surface, ~str>;
/// Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
fn rotate_90deg(&self, turns: int) -> Result<~Surface, ~str>;
}