Skip to content

Commit

Permalink
make pipeline reports delta_time to imgui (#187)
Browse files Browse the repository at this point in the history
According to imgui's custom backend implementation exhibit
and first-party backend implementations,
io.DeltaTime is "usually" meant to be updated by backend code.

(you can check platform backend impls of ocornut/imgui for example)
  • Loading branch information
ruby3141 authored May 22, 2024
1 parent e34f748 commit 5655ce1
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/renderer/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::mem;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Arc;
use std::time::{Duration, Instant};

use imgui::Context;
use once_cell::sync::{Lazy, OnceCell};
Expand Down Expand Up @@ -45,6 +46,7 @@ pub(crate) struct Pipeline<T: RenderEngine> {
rx: Receiver<PipelineMessage>,
shared_state: Arc<PipelineSharedState>,
queue_buffer: OnceCell<Vec<PipelineMessage>>,
start_of_first_frame: OnceCell<Instant>,
}

impl<T: RenderEngine> Pipeline<T> {
Expand Down Expand Up @@ -96,6 +98,7 @@ impl<T: RenderEngine> Pipeline<T> {
rx,
shared_state: Arc::clone(&shared_state),
queue_buffer,
start_of_first_frame: OnceCell::new(),
})
}

Expand Down Expand Up @@ -123,6 +126,14 @@ impl<T: RenderEngine> Pipeline<T> {
}

pub(crate) fn render(&mut self, render_target: T::RenderTarget) -> Result<()> {
let delta_time = Instant::now()
.checked_duration_since(*self.start_of_first_frame.get_or_init(Instant::now))
.unwrap_or(Duration::ZERO)
.checked_sub(Duration::from_secs_f64(self.ctx.time()))
.unwrap_or(Duration::ZERO);

self.ctx.io_mut().update_delta_time(delta_time);

let [w, h] = self.ctx.io().display_size;
let [fsw, fsh] = self.ctx.io().display_framebuffer_scale;

Expand Down

0 comments on commit 5655ce1

Please sign in to comment.