Struct Timer

Source
pub struct Timer { /* private fields */ }
Expand description

Timer instance

Implementations§

Source§

impl Timer

Source

pub fn log_interval(self, log_interval: Duration) -> Self

Sets the logging interval of this timer to log_interval.

§Arguments
  • log_interval - logging interval as used by Self::log
§Returns

Self the (modified) timer

§Example
use std::time::Duration;
use fps_timer::Timer;
let mut timer = Timer::default()
    .log_interval(Duration::from_millis(100))
    .fps(240.);
Examples found in repository?
examples/fps/main.rs (line 13)
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    let fps = args
8        .get(1)
9        .and_then(|arg| arg.parse().ok())
10        .unwrap_or(420.69);
11
12    let mut timer = Timer::default()
13        .log_interval(Duration::from_millis(100))
14        .high_precision(true)
15        .fps(fps);
16
17    loop {
18        let _dt = timer.frame();
19        if let Some(log) = timer.log() {
20            print!(
21                "{:>15.6}ms ({:>10.3}fps)      \r",
22                log.delta_time_avg_ms(),
23                log.fps_average()
24            );
25            let _ = std::io::stdout().flush();
26        }
27    }
28}
Source

pub fn frame_time(self, delta: Duration) -> Self

Sets the target frametime to the specified amount.

§Arguments
  • delta - target frametime
§Returns

Self the (modified) timer

§Example
use std::time::Duration;
use fps_timer::Timer;
let mut timer = Timer::default()
    .frame_time(Duration::from_secs_f64(1. / 60.));
Source

pub fn fps(self, fps: f64) -> Self

Sets the framerate target to the specified amount.

§Arguments
  • fps - target framerate
§Returns

Self the (modified) timer

§Example
use fps_timer::Timer;
let mut timer = Timer::default()
    .fps(60.);
Examples found in repository?
examples/fps/main.rs (line 15)
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    let fps = args
8        .get(1)
9        .and_then(|arg| arg.parse().ok())
10        .unwrap_or(420.69);
11
12    let mut timer = Timer::default()
13        .log_interval(Duration::from_millis(100))
14        .high_precision(true)
15        .fps(fps);
16
17    loop {
18        let _dt = timer.frame();
19        if let Some(log) = timer.log() {
20            print!(
21                "{:>15.6}ms ({:>10.3}fps)      \r",
22                log.delta_time_avg_ms(),
23                log.fps_average()
24            );
25            let _ = std::io::stdout().flush();
26        }
27    }
28}
Source

pub fn high_precision(self, enabled: bool) -> Self

Enable or disable improved accuracy for this timer.

Enabling high precision makes the timer more precise at the cost of higher power consumption because part of the duration is awaited in a busy spinloop.

Defaults to true

§Arguments
  • enable - whether or not to enable higher precision
§Returns

Self the (modified) timer

§Example
use fps_timer::Timer;
let mut timer = Timer::default()
    .fps(60.);
Examples found in repository?
examples/fps/main.rs (line 14)
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    let fps = args
8        .get(1)
9        .and_then(|arg| arg.parse().ok())
10        .unwrap_or(420.69);
11
12    let mut timer = Timer::default()
13        .log_interval(Duration::from_millis(100))
14        .high_precision(true)
15        .fps(fps);
16
17    loop {
18        let _dt = timer.frame();
19        if let Some(log) = timer.log() {
20            print!(
21                "{:>15.6}ms ({:>10.3}fps)      \r",
22                log.delta_time_avg_ms(),
23                log.fps_average()
24            );
25            let _ = std::io::stdout().flush();
26        }
27    }
28}
Source

pub fn frame(&mut self) -> Duration

Waits until the specified frametime target is reached and returns the Duration since the last call to Self::frame() of this Timer (= frametime).

§Example
use std::time::Duration;
use fps_timer::Timer;

fn update(dt: Duration) {
    // game logic
}

fn main()  {
    let mut timer = Timer::default();
    loop {
        let delta_time = timer.frame();
        update(delta_time);
    }
}
Examples found in repository?
examples/fps/main.rs (line 18)
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    let fps = args
8        .get(1)
9        .and_then(|arg| arg.parse().ok())
10        .unwrap_or(420.69);
11
12    let mut timer = Timer::default()
13        .log_interval(Duration::from_millis(100))
14        .high_precision(true)
15        .fps(fps);
16
17    loop {
18        let _dt = timer.frame();
19        if let Some(log) = timer.log() {
20            print!(
21                "{:>15.6}ms ({:>10.3}fps)      \r",
22                log.delta_time_avg_ms(),
23                log.fps_average()
24            );
25            let _ = std::io::stdout().flush();
26        }
27    }
28}
Source

pub fn log(&mut self) -> Option<Log>

returns Some<Log>, holding information about the previous logging interval, every time the interval specified by Timer::log_interval has passed and None otherwise

Examples found in repository?
examples/fps/main.rs (line 19)
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    let fps = args
8        .get(1)
9        .and_then(|arg| arg.parse().ok())
10        .unwrap_or(420.69);
11
12    let mut timer = Timer::default()
13        .log_interval(Duration::from_millis(100))
14        .high_precision(true)
15        .fps(fps);
16
17    loop {
18        let _dt = timer.frame();
19        if let Some(log) = timer.log() {
20            print!(
21                "{:>15.6}ms ({:>10.3}fps)      \r",
22                log.delta_time_avg_ms(),
23                log.fps_average()
24            );
25            let _ = std::io::stdout().flush();
26        }
27    }
28}

Trait Implementations§

Source§

impl Default for Timer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Timer

§

impl RefUnwindSafe for Timer

§

impl Send for Timer

§

impl Sync for Timer

§

impl Unpin for Timer

§

impl UnwindSafe for Timer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.