diff --git a/changelog.md b/changelog.md index 12808ac830..fdb5e7ec1e 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another. ### Next +[PR #1415](https://github.com/Rust-SDL2/rust-sdl2/pull/1415) Store `gfx::framerate::FPSmanager` directly in `FPSManager` instead of on the heap. + [PR #1416](https://github.com/Rust-SDL2/rust-sdl2/pull/1416) Apply clippy fixes, fix deprecations and other code quality improvements. [PR #1408](https://github.com/Rust-SDL2/rust-sdl2/pull/1408) Allow comparing `Version`s, add constant with the version the bindings were compiled with. diff --git a/src/sdl2/gfx/framerate.rs b/src/sdl2/gfx/framerate.rs index 90665864d1..4bb2b1bca2 100644 --- a/src/sdl2/gfx/framerate.rs +++ b/src/sdl2/gfx/framerate.rs @@ -1,30 +1,36 @@ //! Framerate control use get_error; -use libc; -use libc::{c_void, size_t}; -use std::mem; +use std::mem::MaybeUninit; use sys::gfx; /// Structure holding the state and timing information of the framerate controller. +#[derive(Debug, Clone)] pub struct FPSManager { - raw: *mut gfx::framerate::FPSmanager, + raw: gfx::framerate::FPSmanager, +} + +impl Default for FPSManager { + fn default() -> Self { + Self::new() + } } impl FPSManager { /// Create the framerate manager. pub fn new() -> FPSManager { unsafe { - let size = mem::size_of::() as size_t; - let raw = libc::malloc(size) as *mut gfx::framerate::FPSmanager; - gfx::framerate::SDL_initFramerate(raw); - FPSManager { raw } + let mut raw = MaybeUninit::uninit(); + gfx::framerate::SDL_initFramerate(raw.as_mut_ptr()); + FPSManager { + raw: raw.assume_init(), + } } } /// Set the framerate in Hz. pub fn set_framerate(&mut self, rate: u32) -> Result<(), String> { - let ret = unsafe { gfx::framerate::SDL_setFramerate(self.raw, rate) }; + let ret = unsafe { gfx::framerate::SDL_setFramerate(&mut self.raw, rate) }; match ret { 0 => Ok(()), _ => Err(get_error()), @@ -34,23 +40,19 @@ impl FPSManager { /// Return the current target framerate in Hz. pub fn get_framerate(&self) -> i32 { // will not get an error - unsafe { gfx::framerate::SDL_getFramerate(self.raw) as i32 } + // SAFETY: SDL_getFramerate will not mutate self.raw, even though it accepts *mut FPSmanager. + unsafe { gfx::framerate::SDL_getFramerate(&self.raw as *const _ as *mut _) as i32 } } /// Return the current framecount. pub fn get_frame_count(&self) -> i32 { // will not get an error - unsafe { gfx::framerate::SDL_getFramecount(self.raw) as i32 } + // SAFETY: SDL_getFramecount will not mutate self.raw, even though it accepts *mut FPSmanager. + unsafe { gfx::framerate::SDL_getFramecount(&self.raw as *const _ as *mut _) as i32 } } /// Delay execution to maintain a constant framerate and calculate fps. pub fn delay(&mut self) -> u32 { - unsafe { gfx::framerate::SDL_framerateDelay(self.raw) as u32 } - } -} - -impl Drop for FPSManager { - fn drop(&mut self) { - unsafe { libc::free(self.raw as *mut c_void) } + unsafe { gfx::framerate::SDL_framerateDelay(&mut self.raw) as u32 } } }