|
| 1 | +//! Game plugin for performance profiling tools. |
| 2 | +
|
| 3 | +use crate::prelude::*; |
| 4 | + |
| 5 | +/// Installs profiler ui plugins |
| 6 | +pub fn game_plugin(game: &mut Game) { |
| 7 | + game.systems.add_before_system(mark_new_frame); |
| 8 | + game.sessions |
| 9 | + .create(SessionNames::PROFILER) |
| 10 | + .install_plugin(session_plugin); |
| 11 | +} |
| 12 | + |
| 13 | +/// Install the profiler UI to profiler session. |
| 14 | +fn session_plugin(session: &mut Session) { |
| 15 | + session |
| 16 | + .stages |
| 17 | + .add_system_to_stage(CoreStage::First, profiler); |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(HasSchema, Clone, Debug, Default)] |
| 21 | +struct ProfilerState { |
| 22 | + pub show_window: bool, |
| 23 | +} |
| 24 | + |
| 25 | +fn profiler( |
| 26 | + ctx: ResMut<EguiCtx>, |
| 27 | + localization: Localization<GameMeta>, |
| 28 | + mut state: ResMutInit<ProfilerState>, |
| 29 | + keyboard_inputs: Res<KeyboardInputs>, |
| 30 | +) { |
| 31 | + puffin::set_scopes_on(true); |
| 32 | + |
| 33 | + let toggle_window = keyboard_inputs |
| 34 | + .key_events |
| 35 | + .iter() |
| 36 | + .any(|x| x.key_code.option() == Some(KeyCode::F10) && !x.button_state.pressed()); |
| 37 | + |
| 38 | + let show_window = &mut state.show_window; |
| 39 | + if toggle_window { |
| 40 | + *show_window = !*show_window; |
| 41 | + } |
| 42 | + |
| 43 | + egui::Window::new(localization.get("profiler")) |
| 44 | + .id(egui::Id::new("profiler")) |
| 45 | + .open(show_window) |
| 46 | + .show(&ctx, |ui| { |
| 47 | + puffin_egui::profiler_ui(ui); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +/// Notify profilers we are at frame boundary |
| 52 | +pub fn mark_new_frame(_game: &mut Game) { |
| 53 | + puffin::GlobalProfiler::lock().new_frame(); |
| 54 | +} |
0 commit comments