aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/profiler.rs
blob: 98bf8196eecd463a476d11bc812da0842f0658ec (plain)
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
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use crate::render::{sprite::SpriteDraw, Renderer};
use hurrycurry_protocol::glam::Vec2;
use std::time::Instant;

pub struct ProfilerOverlay {
    frames: usize,
    fps_timer_start: Instant,
    fps: f32,
}

impl ProfilerOverlay {
    pub fn new() -> Self {
        Self {
            fps: 0.,
            fps_timer_start: Instant::now(),
            frames: 0,
        }
    }
    pub fn update(&mut self, renderer: &mut Renderer) {
        self.frames += 1;

        let t = self.fps_timer_start.elapsed();

        if t.as_secs_f32() > 0.2 {
            self.fps = self.frames as f32 / t.as_secs_f32();
            self.frames = 0;
            self.fps_timer_start += t;
        }
        let size = renderer.draw_text(
            Vec2::ZERO,
            &format!("FPS: {:.0}\nSprites: {}", self.fps, renderer.num_sprites()),
            0.3,
            Some([255, 150, 255, 255]),
        );
        renderer.draw_ui(SpriteDraw::screen(
            renderer.misc_textures.solid,
            i32::MAX - 1,
            Vec2::ZERO,
            size,
            Some([0, 0, 0, 200]),
        ))
    }
}