use core::time::Duration;
#[derive(Debug, Clone, Copy)]
pub struct EventLoopConfig {
pub fps: u32,
pub delta_time: f32,
}
pub struct EventLoop {
pub config: EventLoopConfig,
}
impl EventLoop {
#[must_use]
pub fn new(config: EventLoopConfig) -> Self {
Self { config }
}
pub fn start<F, SF>(&self, code: F, sleep: SF)
where
F: Fn(EventLoopConfig),
SF: Fn(Duration),
{
let dt = 1 / self.config.fps;
loop {
code(self.config);
sleep(Duration::from_secs(u64::from(dt)));
}
}
pub fn start_mut<F, SF>(&self, mut code: F, sleep: SF)
where
F: FnMut(EventLoopConfig),
SF: Fn(Duration),
{
let dt = 1 / self.config.fps;
loop {
code(self.config);
sleep(Duration::from_secs(u64::from(dt)));
}
}
}
#[derive(Debug)]
pub struct EventLoopBuilder {
config: EventLoopConfig,
}
impl EventLoopBuilder {
pub fn new() -> Self {
EventLoopBuilder {
config: EventLoopConfig {
fps: 60,
delta_time: 1.0 / 60.0,
},
}
}
pub fn fps(mut self, fps: u32) -> Self {
self.config = EventLoopConfig {
fps,
delta_time: 1.0 / fps as f32,
};
self
}
pub fn build(&self) -> EventLoop {
EventLoop {
config: self.config,
}
}
}
impl Default for EventLoopBuilder {
fn default() -> Self {
EventLoopBuilder {
config: EventLoopConfig {
fps: 60,
delta_time: 1.0 / 60.0,
},
}
}
}