summaryrefslogtreecommitdiff
path: root/src/time.rs
blob: 5978b573871e3849aecbeb84f6899c42fd6441d3 (plain)
use std::time::{Duration, Instant, SystemTime};

pub struct TimeContext {
	start_time: Instant,
	last_frame: Instant,
	current_frame: Instant,
	system_time: SystemTime,
}

impl TimeContext {
	pub fn new() -> Self {
		let now = Instant::now();
		Self {
			start_time: now,
			last_frame: now,
			current_frame: now,
			system_time: SystemTime::now(),
		}
	}

	pub fn next_frame(&mut self) {
		puffin::profile_scope!("update time");
		self.last_frame = self.current_frame;
		self.current_frame = Instant::now();
		self.system_time = SystemTime::now()
	}

	pub fn delta_time(&self) -> Duration {
		self.current_frame - self.last_frame
	}

	pub fn monotonic_now(&self) -> Duration {
		self.current_frame - self.start_time
	}

	pub fn system_now(&self) -> SystemTime {
		self.system_time
	}
}

impl Default for TimeContext {
	fn default() -> Self {
		Self::new()
	}
}