pub struct Timer { /* private fields */ }
Expand description
Timer instance
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn log_interval(self, log_interval: Duration) -> Self
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 bySelf::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}
Sourcepub fn frame_time(self, delta: Duration) -> Self
pub fn frame_time(self, delta: Duration) -> Self
Sourcepub fn fps(self, fps: f64) -> Self
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}
Sourcepub fn high_precision(self, enabled: bool) -> Self
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}
Sourcepub fn frame(&mut self) -> Duration
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}
Sourcepub fn log(&mut self) -> Option<Log>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more