real FPS performance

This commit is contained in:
Nilton Constantino 2026-01-13 08:42:17 +00:00
parent 04ce8c195e
commit 54ed1d0eaf
No known key found for this signature in database

View File

@ -30,7 +30,8 @@ struct PrometeuApp {
input_signals: InputSignals, input_signals: InputSignals,
frame_target_dt: Duration, frame_target_dt: Duration,
next_frame: Instant, last_frame_time: Instant,
accumulator: Duration,
last_stats_update: Instant, last_stats_update: Instant,
frames_since_last_update: u64, frames_since_last_update: u64,
@ -38,6 +39,8 @@ struct PrometeuApp {
impl PrometeuApp { impl PrometeuApp {
fn new() -> Self { fn new() -> Self {
let target_fps = 60;
Self { Self {
window: None, window: None,
pixels: None, pixels: None,
@ -46,8 +49,9 @@ impl PrometeuApp {
input_signals: InputSignals::default(), input_signals: InputSignals::default(),
frame_target_dt: Duration::from_nanos(1_000_000_000 / 60), frame_target_dt: Duration::from_nanos(1_000_000_000 / target_fps),
next_frame: Instant::now(), last_frame_time: Instant::now(),
accumulator: Duration::ZERO,
last_stats_update: Instant::now(), last_stats_update: Instant::now(),
frames_since_last_update: 0, frames_since_last_update: 0,
@ -98,7 +102,6 @@ impl ApplicationHandler for PrometeuApp {
self.pixels = Some(pixels); self.pixels = Some(pixels);
self.next_frame = Instant::now();
event_loop.set_control_flow(ControlFlow::Poll); event_loop.set_control_flow(ControlFlow::Poll);
} }
@ -185,43 +188,43 @@ impl ApplicationHandler for PrometeuApp {
} }
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
// Pacing simples a ~60Hz
let now = Instant::now(); let now = Instant::now();
let mut frame_delta = now.duration_since(self.last_frame_time);
if now >= self.next_frame { // Limitador para evitar a "espiral da morte" se o SO travar (máximo de 100ms por volta)
self.next_frame += self.frame_target_dt; if frame_delta > Duration::from_millis(100) {
frame_delta = Duration::from_millis(100);
}
// executa 1 frame do PROMETEU self.last_frame_time = now;
self.accumulator += frame_delta;
// 🔥 O coração do determinismo: consome o tempo em fatias exatas de 60Hz
while self.accumulator >= self.frame_target_dt {
self.machine.step_frame(&self.input_signals); self.machine.step_frame(&self.input_signals);
self.accumulator -= self.frame_target_dt;
self.frames_since_last_update += 1; self.frames_since_last_update += 1;
}
let elapsed = now.duration_since(self.last_stats_update); // Atualiza estatísticas a cada 1 segundo real
if elapsed >= Duration::from_secs(1) { let stats_elapsed = now.duration_since(self.last_stats_update);
if let Some(window) = self.window { if stats_elapsed >= Duration::from_secs(1) {
let fps = self.frames_since_last_update as f64 / elapsed.as_secs_f64(); if let Some(window) = self.window {
let usage_bytes = self.machine.gfx.memory_usage_bytes(); let fps = self.frames_since_last_update as f64 / stats_elapsed.as_secs_f64();
let kb = usage_bytes as f64 / 1024.0; let kb = self.machine.gfx.memory_usage_bytes() as f64 / 1024.0;
let title = format!( let title = format!(
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Frame: {}", "PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Frame: {}",
kb, kb, fps, self.machine.frame_index
fps, );
self.machine.frame_index); window.set_title(&title);
window.set_title(&title);
}
self.last_stats_update = now;
self.frames_since_last_update = 0;
} }
// pede redraw self.last_stats_update = now;
self.request_redraw(); self.frames_since_last_update = 0;
} else {
// dorme pouco para não fritar CPU
let sleep_for = self.next_frame - now;
std::thread::sleep(sleep_for.min(Duration::from_millis(2)));
} }
self.request_redraw();
} }
} }