1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Game plugin for performance profiling tools.

use crate::prelude::*;

/// Installs profiler ui plugins
pub fn game_plugin(game: &mut Game) {
    game.systems.add_before_system(mark_new_frame);
    game.sessions
        .create(SessionNames::PROFILER)
        .install_plugin(session_plugin);
}

/// Install the profiler UI to profiler session.
fn session_plugin(session: &mut Session) {
    session
        .stages
        .add_system_to_stage(CoreStage::First, profiler);
}

#[derive(HasSchema, Clone, Debug, Default)]
struct ProfilerState {
    pub show_window: bool,
}

fn profiler(
    ctx: ResMut<EguiCtx>,
    localization: Localization<GameMeta>,
    mut state: ResMutInit<ProfilerState>,
    keyboard_inputs: Res<KeyboardInputs>,
) {
    puffin::set_scopes_on(true);

    let toggle_window = keyboard_inputs
        .key_events
        .iter()
        .any(|x| x.key_code.option() == Some(KeyCode::F10) && !x.button_state.pressed());

    let show_window = &mut state.show_window;
    if toggle_window {
        *show_window = !*show_window;
    }

    egui::Window::new(localization.get("profiler"))
        .id(egui::Id::new("profiler"))
        .open(show_window)
        .show(&ctx, |ui| {
            puffin_egui::profiler_ui(ui);
        });
}

/// Notify profilers we are at frame boundary
pub fn mark_new_frame(_game: &mut Game) {
    puffin::GlobalProfiler::lock().new_frame();
}