Struct FrameCounter

Source
pub struct FrameCounter { /* private fields */ }

Implementations§

Source§

impl FrameCounter

Source

pub fn new(frame_rate: f64) -> Self

Creates a new FrameCounter with the given starting framerate.

§Arguments
  • frame_rate - initial frame rate guess.
Source

pub fn tick(&mut self)

Updates the frame measurements

Examples found in repository?
examples/counter.rs (line 11)
7pub fn main() {
8    let mut frame_counter = FrameCounter::default();
9
10    loop {
11        frame_counter.tick();
12
13        dummy_workload();
14
15        println!("fps stats - {}", frame_counter);
16    }
17}
More examples
Hide additional examples
examples/limiter.rs (line 11)
7pub fn main() {
8    let mut frame_counter = FrameCounter::default();
9
10    loop {
11        frame_counter.tick();
12
13        dummy_workload();
14
15        // hot loop, do not trigger scheduler
16        frame_counter.sleep_until_framerate(60f64);
17
18        println!("fps stats - {}", frame_counter);
19    }
20}
Source

pub fn wait_until_framerate(&self, framerate: f64)

Waits in a hot-loop until the desired frame rate is achieved. This function will not call std::thread:sleep to prevent the OS from scheduling this thread.

This means the function will consume an entire core on the CPU.

§Example:
use frame_counter::FrameCounter;

pub fn dummy_workload() {
    std::thread::sleep(std::time::Duration::from_millis(1));
}

pub fn main() {
    let mut frame_counter = FrameCounter::default();

    loop {
        frame_counter.tick();

        dummy_workload();

        frame_counter.wait_until_framerate(60f64);

        println!("fps stats - {}", frame_counter);
    }
}
Source

pub fn sleep_until_framerate(&self, framerate: f64)

Waits in a cold-loop until the desired frame rate is achieved. This function will call std::thread:sleep to prevent the process from consuming the entire CPU Core.

§Example:
use frame_counter::FrameCounter;

pub fn dummy_workload() {
    std::thread::sleep(std::time::Duration::from_millis(1));
}

pub fn main() {
    let mut frame_counter = FrameCounter::default();

    loop {
        frame_counter.tick();

        dummy_workload();

        frame_counter.sleep_until_framerate(60f64);

        println!("fps stats - {}", frame_counter);
    }
}
Examples found in repository?
examples/limiter.rs (line 16)
7pub fn main() {
8    let mut frame_counter = FrameCounter::default();
9
10    loop {
11        frame_counter.tick();
12
13        dummy_workload();
14
15        // hot loop, do not trigger scheduler
16        frame_counter.sleep_until_framerate(60f64);
17
18        println!("fps stats - {}", frame_counter);
19    }
20}
Source

pub fn frame_time(&self) -> Duration

Returns the frame time of the last frame as a Duration.

Source

pub fn avg_frame_time(&self) -> Duration

Returns the average frame time over the last second as a Duration.

Source

pub fn frame_rate(&self) -> f64

Returns the frame rate of the last frame.

Source

pub fn avg_frame_rate(&self) -> f64

Returns the average frame rate of the last second.

Trait Implementations§

Source§

impl Default for FrameCounter

Source§

fn default() -> Self

Creates a new FrameCounter with a starting framerate of 100.

Source§

impl Display for FrameCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.