Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,24 @@ impl Window {
}
}

/// Returns the current fps of the window
///
/// # Examples
///
/// ```no_run
/// # use minifb::*;
/// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap();
/// let fps = window.get_fps();
/// println!("fps {}", fps);
/// ```
#[inline]
pub fn get_fps(&mut self) -> usize {
match self.0.get_delta_time() {
Some(rate) => (1.0 / rate.as_secs_f32()) as usize,
_ => 0,
}
}

/// Returns the current size of the window
///
/// # Examples
Expand Down
5 changes: 5 additions & 0 deletions src/os/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ impl Window {
self.update_rate.set_rate(rate);
}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
self.update_rate.get_delta_time()
}

#[inline]
pub fn update_rate(&mut self) {
self.update_rate.update();
Expand Down
9 changes: 9 additions & 0 deletions src/os/posix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ impl Window {
}
}

pub fn get_delta_time(&self) -> Option<Duration> {
match self {
#[cfg(feature = "x11")]
Window::X11(w) => w.get_delta_time(),
#[cfg(feature = "wayland")]
Window::Wayland(w) => w.get_delta_time(),
}
}

pub fn update_rate(&mut self) {
match self {
#[cfg(feature = "x11")]
Expand Down
5 changes: 5 additions & 0 deletions src/os/posix/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ impl Window {
self.update_rate.set_rate(rate);
}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
self.update_rate.get_delta_time()
}

#[inline]
pub fn set_key_repeat_rate(&mut self, rate: f32) {
self.key_handler.set_key_repeat_delay(rate);
Expand Down
5 changes: 5 additions & 0 deletions src/os/posix/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@ impl Window {
self.update_rate.set_rate(rate);
}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
self.update_rate.get_delta_time()
}

#[inline]
pub fn update_rate(&mut self) {
self.update_rate.update();
Expand Down
5 changes: 5 additions & 0 deletions src/os/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ impl Window {
#[inline]
pub fn set_rate(&mut self, _rate: Option<Duration>) {}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
None
}

#[inline]
pub fn update_rate(&mut self) {}

Expand Down
5 changes: 5 additions & 0 deletions src/os/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,11 @@ impl Window {
self.update_rate.set_rate(rate);
}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
self.update_rate.get_delta_time()
}

#[inline]
pub fn update_rate(&mut self) {
self.update_rate.update();
Expand Down
33 changes: 22 additions & 11 deletions src/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,45 @@ use std::time::{Duration, Instant};
pub struct UpdateRate {
target_rate: Option<Duration>,
prev_time: Instant,
delta: Option<Duration>,
}

#[cfg_attr(target_arch = "wasm32", allow(unused))]
impl UpdateRate {
pub fn new() -> UpdateRate {
UpdateRate {
// Default limit to 4 ms
pub fn new() -> Self {
Self {
// Default target rate: 4 ms per frame (~250 FPS)
target_rate: Some(Duration::from_millis(4)),
prev_time: Instant::now(),
delta: None,
}
}

#[inline]
pub fn set_rate(&mut self, rate: Option<Duration>) {
self.target_rate = rate
self.target_rate = rate;
}

pub fn update(&mut self) {
if let Some(target_rate) = self.target_rate {
let delta = self.prev_time.elapsed();
let now = Instant::now();
let elapsed = now.duration_since(self.prev_time);

if delta < target_rate {
let sleep_time = target_rate - delta;
//eprintln!("sleeping {} ms", sleep_time.as_secs_f64() * 1000.);
// If a target rate is set, sleep to match it
if let Some(target_rate) = self.target_rate {
if elapsed < target_rate {
let sleep_time = target_rate - elapsed;
std::thread::sleep(sleep_time);
}

self.prev_time = Instant::now();
}

// Now mark the new frame time and compute total delta (including sleep)
let now = Instant::now();
self.delta = Some(now.duration_since(self.prev_time));
self.prev_time = now;
}

#[inline]
pub fn get_delta_time(&self) -> Option<Duration> {
self.delta
}
}
Loading