Skip to content

Commit

Permalink
Add first_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
branpk committed Oct 8, 2023
1 parent 67c6e5a commit 09b4ab7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub trait AppEnv {
/// The config that was used when running the application.
fn config(&self) -> &AppConfig;

/// True if this is the first time that the app callback has been called.
fn first_run(&self) -> bool;

/// A recent fps measurement.
fn fps(&self) -> f32;

Expand Down
21 changes: 15 additions & 6 deletions wafel_window/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ use wafel_viz_wgpu::VizRenderer;
use winit::{event::WindowEvent, window::Window};

use crate::{
egui_state::EguiState, fps_counter::FpsCounter, logging, wgpu_util::CachedTexture,
window_env::AppEnv, AppConfig, Input,
app_env::AppEnv, egui_state::EguiState, fps_counter::FpsCounter, logging,
wgpu_util::CachedTexture, AppConfig, Input,
};

#[derive(Debug)]
struct WindowEnvImpl<'a> {
struct AppEnvImpl<'a> {
config: &'a AppConfig,
first_run: bool,
fps: f32,
mspf: f32,
egui_ctx: egui::Context,
viz_scenes: Mutex<Vec<VizScene>>,
input: &'a Input,
}

static_assertions::assert_impl_all!(WindowEnvImpl<'_>: Send, Sync);
static_assertions::assert_impl_all!(AppEnvImpl<'_>: Send, Sync);

impl AppEnv for WindowEnvImpl<'_> {
impl AppEnv for AppEnvImpl<'_> {
fn config(&self) -> &AppConfig {
self.config
}

fn first_run(&self) -> bool {
self.first_run
}

fn fps(&self) -> f32 {
self.fps
}
Expand Down Expand Up @@ -67,6 +72,7 @@ pub struct Container<D> {
depth_texture: CachedTexture,
fps_counter: FpsCounter,
input: Input,
first_run: bool,
}

impl<D: FnMut(&dyn AppEnv)> Container<D> {
Expand Down Expand Up @@ -112,6 +118,7 @@ impl<D: FnMut(&dyn AppEnv)> Container<D> {
depth_texture: CachedTexture::new(),
fps_counter: FpsCounter::new(),
input: Input::new(),
first_run: true,
}
}

Expand All @@ -130,8 +137,9 @@ impl<D: FnMut(&dyn AppEnv)> Container<D> {
});

egui_state.run(window, |ctx| {
let env = WindowEnvImpl {
let env = AppEnvImpl {
config: &self.config,
first_run: self.first_run,
fps: self.fps_counter.fps(),
mspf: self.fps_counter.mspf(),
egui_ctx: ctx.clone(),
Expand All @@ -142,6 +150,7 @@ impl<D: FnMut(&dyn AppEnv)> Container<D> {
(self.draw)(&env);

self.viz_scenes = env.viz_scenes.into_inner().unwrap();
self.first_run = false;
});

self.input.end_frame();
Expand Down
4 changes: 2 additions & 2 deletions wafel_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
#![warn(rust_2018_idioms, missing_debug_implementations, missing_docs)]
#![allow(clippy::too_many_arguments)]

pub use app_env::*;
pub use config::*;
pub use input::*;
pub use window_env::*;
pub use winit::event::{MouseButton, VirtualKeyCode};

mod app_env;
mod config;
mod container;
mod egui_state;
Expand All @@ -36,7 +37,6 @@ mod input;
mod logging;
mod wgpu_util;
mod window;
mod window_env;

/// Initializes logging, opens a window and runs the application.
///
Expand Down
2 changes: 1 addition & 1 deletion wafel_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winit::{
window::{WindowBuilder, WindowLevel},
};

use crate::{container::Container, window_env::AppEnv, AppConfig};
use crate::{container::Container, AppConfig, AppEnv};

/// Opens a maximized window and runs the application.
///
Expand Down

0 comments on commit 09b4ab7

Please sign in to comment.