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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Adds diagnostic logging and a cursor for debugging.

use bevy::{
    core_pipeline::clear_color::ClearColorConfig,
    diagnostic::{Diagnostic, DiagnosticId, Diagnostics},
    prelude::*,
};

/// Adds [`Diagnostics`] data from `bevy_framepace`
pub struct DiagnosticsPlugin;

impl Plugin for DiagnosticsPlugin {
    fn build(&self, app: &mut App) {
        app.add_startup_system(Self::setup_system)
            .add_system(Self::diagnostic_system);
    }
}

impl DiagnosticsPlugin {
    /// [`DiagnosticId`] for the frametime
    pub const FRAMEPACE_FRAMETIME: DiagnosticId =
        DiagnosticId::from_u128(8021378406439507683279787892187089153);
    /// [`DiagnosticId`] for failures to meet frame time target
    pub const FRAMEPACE_OVERSLEEP: DiagnosticId =
        DiagnosticId::from_u128(978023490268634078905367093342937);

    /// Initial setup for framepace diagnostics
    pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
        diagnostics.add(
            Diagnostic::new(Self::FRAMEPACE_FRAMETIME, "framepace::frametime", 128)
                .with_suffix("ms"),
        );
        diagnostics.add(
            Diagnostic::new(Self::FRAMEPACE_OVERSLEEP, "framepace::oversleep", 128)
                .with_suffix("µs"),
        );
    }

    /// Updates diagnostic data from measurements
    pub fn diagnostic_system(
        mut diagnostics: ResMut<Diagnostics>,
        time: Res<Time>,
        stats: Res<crate::FramePaceStats>,
    ) {
        if time.delta_seconds_f64() == 0.0 {
            return;
        }

        let frametime_millis = stats.frametime.try_lock().unwrap().as_secs_f64() * 1_000_f64;
        let error_micros = stats.oversleep.try_lock().unwrap().as_secs_f64() * 1_000_000_f64;

        diagnostics.add_measurement(Self::FRAMEPACE_FRAMETIME, || frametime_millis);
        diagnostics.add_measurement(Self::FRAMEPACE_OVERSLEEP, || error_micros);
    }
}

/// Marks the entity to use for the framepace debug cursor.
#[derive(Component, Debug, Reflect)]
pub struct DebugCursor;

/// Marks the camera to use for rendering the framepace debug cursor.
#[derive(Component, Debug, Reflect)]
pub struct DebugCursorCamera;

/// Adds a simple debug cursor for quickly testing latency.
pub struct CursorPlugin;

impl Plugin for CursorPlugin {
    fn build(&self, app: &mut App) {
        app.add_startup_system(Self::setup_cursor)
            .add_system(Self::update_cursor);
    }
}

impl CursorPlugin {
    /// Spawns the [`DebugCursorCamera`] and [`DebugCursor`] entities.
    pub fn setup_cursor(
        mut commands: Commands,
        mut meshes: ResMut<Assets<Mesh>>,
        mut materials: ResMut<Assets<ColorMaterial>>,
    ) {
        commands.spawn((
            Camera2dBundle {
                camera: Camera {
                    priority: 100,
                    ..Default::default()
                },
                camera_2d: Camera2d {
                    clear_color: ClearColorConfig::None,
                },
                ..Default::default()
            },
            DebugCursorCamera,
        ));
        commands.spawn((
            bevy::sprite::MaterialMesh2dBundle {
                mesh: meshes.add(shape::Circle::new(10.0).into()).into(),
                material: materials.add(ColorMaterial::from(Color::GREEN)),
                transform: Transform::from_translation(Vec3::new(-100., 0., 0.)),
                ..default()
            },
            DebugCursor,
        ));
    }

    /// Updates the position of the [`DebugCursor`].
    pub fn update_cursor(
        windows: Res<Windows>,
        mut cursor: Query<&mut Transform, With<DebugCursor>>,
    ) {
        if let Some(pos) = windows.primary().cursor_position() {
            let offset = -Vec2::new(windows.primary().width(), windows.primary().height()) / 2.0;
            cursor.single_mut().translation = (pos + offset).extend(0.0);
        }
    }
}