1 //! Framerate control
2 
3 use libc;
4 use libc::{c_void, uint32_t, size_t};
5 use std::mem;
6 use ::get_error;
7 use sys;
8 
9 /// Structure holding the state and timing information of the framerate controller.
10 pub struct FPSManager {
11     raw: *mut sys::gfx::framerate::FPSmanager,
12 }
13 
14 impl FPSManager {
15     /// Create the framerate manager.
new() -> FPSManager16     pub fn new() -> FPSManager {
17         unsafe {
18             let size = mem::size_of::<sys::gfx::framerate::FPSmanager>() as size_t;
19             let raw = libc::malloc(size) as *mut sys::gfx::framerate::FPSmanager;
20             sys::gfx::framerate::SDL_initFramerate(raw);
21             FPSManager { raw: raw }
22         }
23     }
24 
25     /// Set the framerate in Hz.
set_framerate(&mut self, rate: u32) -> Result<(), String>26     pub fn set_framerate(&mut self, rate: u32) -> Result<(), String> {
27         let ret = unsafe { sys::gfx::framerate::SDL_setFramerate(self.raw, rate as uint32_t) };
28         match ret {
29             0 => Ok(()),
30             _ => Err(get_error())
31         }
32     }
33 
34     /// Return the current target framerate in Hz.
get_framerate(&self) -> i3235     pub fn get_framerate(&self) -> i32 {
36         // will not get an error
37         unsafe { sys::gfx::framerate::SDL_getFramerate(self.raw) as i32 }
38     }
39 
40     /// Return the current framecount.
get_frame_count(&self) -> i3241     pub fn get_frame_count(&self) -> i32 {
42         // will not get an error
43         unsafe { sys::gfx::framerate::SDL_getFramecount(self.raw) as i32 }
44     }
45 
46     /// Delay execution to maintain a constant framerate and calculate fps.
delay(&mut self) -> u3247     pub fn delay(&mut self) -> u32 {
48         unsafe { sys::gfx::framerate::SDL_framerateDelay(self.raw) as u32 }
49     }
50 }
51 
52 impl Drop for FPSManager {
drop(&mut self)53     fn drop(&mut self) {
54         unsafe { libc::free(self.raw as *mut c_void) }
55     }
56 }
57