Commit 7454d259 authored by Florian Blasius's avatar Florian Blasius 🤘

[fix] fps remove thread

parent ef9e9a73
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::thread;
use std::time;
use std::rc::Rc; use std::rc::Rc;
use std::time;
use orbclient::{self, Color, Mode, Renderer as OrbRenderer, Window as OrbWindow}; use orbclient::{self, Color, Mode, Renderer as OrbRenderer, Window as OrbWindow};
...@@ -256,25 +255,30 @@ impl BackendRunner for OrbitalBackendRunner { ...@@ -256,25 +255,30 @@ impl BackendRunner for OrbitalBackendRunner {
self.world = Some(world); self.world = Some(world);
} }
fn run(&mut self, update: Rc<Cell<bool>>, running: Rc<Cell<bool>>) { fn run(&mut self, update: Rc<Cell<bool>>, running: Rc<Cell<bool>>) {
const FRAMES_PER_SECOND: u64 = 60; let target_fps = 60.0;
let duration_limit = time::Duration::from_millis(1000 / FRAMES_PER_SECOND); let target_fps_nanos = (1. / target_fps as f32) * 1_000_000_000.;
let mut last_tick_time = time::Instant::now();
loop { loop {
let sys_time = time::SystemTime::now(); let sys_time = time::SystemTime::now();
if !running.get() { if !running.get() {
break; break;
} }
if let Some(world) = &mut self.world { let time = last_tick_time.elapsed();
world.run(); let total_nanos = time.as_secs() * 1_000_000_000 + time.subsec_nanos() as u64;
} let elapsed = target_fps_nanos - (total_nanos as f32);
update.set(false); if elapsed <= 0. {
if let Some(world) = &mut self.world {
world.run();
}
self.backend.borrow_mut().drain_events(); last_tick_time = time::Instant::now();
let duration_since = sys_time.duration_since(sys_time).unwrap(); update.set(false);
if duration_limit > duration_since {
thread::sleep(duration_limit - duration_since);
} }
self.backend.borrow_mut().drain_events();
} }
} }
} }
...@@ -89,7 +89,7 @@ macro_rules! widget { ...@@ -89,7 +89,7 @@ macro_rules! widget {
} }
/// Sets or shares the constraint property. /// Sets or shares the constraint property.
pub fn constraint<P: Into<PropertySource<Constraint>>>(mut self, constraint: P) -> Self { pub fn constraint<P: Into<PropertySource<Constraint>>>(self, constraint: P) -> Self {
self.attach(constraint) self.attach(constraint)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment