scissor attempt

This commit is contained in:
Aleksander
2025-09-23 17:43:19 +02:00
parent 5e6852e5d0
commit 8d41d8bbd1
8 changed files with 143 additions and 70 deletions
+37
View File
@@ -0,0 +1,37 @@
use crate::timestep::get_micros;
#[derive(Default)]
pub struct RateLimiter {
rate: u16,
start_us: u64,
end_us: u64,
}
impl RateLimiter {
pub fn new() -> Self {
Self {
rate: 0,
end_us: 0,
start_us: 0,
}
}
pub fn start(&mut self, rate: u16) {
self.rate = rate;
self.start_us = get_micros();
}
pub fn end(&mut self) {
if self.rate == 0 {
return;
}
self.end_us = get_micros();
let microseconds = self.end_us - self.start_us;
let frametime_microseconds = ((1000.0 / self.rate as f32) * 1000.0) as u64;
let delay = frametime_microseconds as i64 - microseconds as i64;
if delay > 0 {
std::thread::sleep(std::time::Duration::from_micros(delay as u64));
}
}
}