Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store gfx::framerate::FPSmanager directly in FPSManager instead of on the heap #1415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 20 additions & 18 deletions src/sdl2/gfx/framerate.rs
Original file line number Diff line number Diff line change
@@ -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::<gfx::framerate::FPSmanager>() 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()),
Expand All @@ -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 }
}
}
Loading