Skip to main content

nadir_debug/
stats.rs

1// Copyright 2026 Joseph Anthony Abbott III
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The stats overlay: the numbers of the last frame, as plain values the
16//! engine copies in (`debug-tools.md`, "Tools").
17
18use std::time::Duration;
19
20use nadir_core::Color;
21
22use crate::Ui;
23
24/// The color of a count that should stay 0, once it is not.
25const WARNING: Color = Color::rgb(255, 176, 64);
26
27/// What the stats overlay shows: plain numbers, which the engine copies in
28/// each frame from its own resources.
29#[non_exhaustive]
30#[derive(Clone, Copy, Debug, Default, PartialEq)]
31pub struct Stats {
32    /// Frames a second, averaged over the last frames.
33    pub frame_rate: f32,
34    /// How long the processor worked on the last frame.
35    pub frame_time: Duration,
36    /// How long the frame's ticks took, together.
37    pub tick_time: Duration,
38    /// How many ticks the frame ran.
39    pub ticks: u32,
40    /// The draw calls of the last frame drawn.
41    pub draw_calls: u32,
42    /// The world's sprites it drew.
43    pub sprites: u32,
44    /// The entities alive.
45    pub entities: usize,
46    /// The heap allocations of the last frame, on the main thread.
47    pub allocations: u64,
48    /// The bytes those allocations asked for.
49    pub allocated_bytes: u64,
50    /// The voices sounding.
51    pub voices: u32,
52    /// The audio device's underruns so far.
53    pub underruns: u64,
54}
55
56impl Stats {
57    /// Fills a window with the numbers, a line each; underruns, once there
58    /// are some, in amber.
59    pub fn show(&self, ui: &mut Ui<'_>) {
60        ui.label(format_args!("{:.0} fps", self.frame_rate));
61        ui.label(format_args!("frame {:.2} ms CPU", millis(self.frame_time)));
62        ui.label(format_args!(
63            "ticks {} in {:.2} ms",
64            self.ticks,
65            millis(self.tick_time)
66        ));
67        ui.label(format_args!("draw calls {}", self.draw_calls));
68        ui.label(format_args!("sprites {}", self.sprites));
69        ui.label(format_args!("entities {}", self.entities));
70        ui.label(format_args!(
71            "allocs {} ({} bytes)",
72            self.allocations, self.allocated_bytes
73        ));
74        ui.label(format_args!("voices {}", self.voices));
75        if self.underruns > 0 {
76            ui.label_colored(WARNING, format_args!("underruns {}", self.underruns));
77        } else {
78            ui.label(format_args!("underruns {}", self.underruns));
79        }
80    }
81}
82
83fn millis(duration: Duration) -> f64 {
84    duration.as_secs_f64() * 1000.0
85}