Fps counter
This commit is contained in:
+42
-2
@@ -1,11 +1,50 @@
|
|||||||
pub mod components;
|
pub mod components;
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use osmium::{renderer::Point, terminal};
|
use osmium::{renderer::Point, terminal};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let engine = osmium::init();
|
let engine = osmium::init();
|
||||||
|
|
||||||
engine.entity(|r| {
|
let last_frame = Arc::new(Mutex::new(std::time::Instant::now()));
|
||||||
|
let peak: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
|
||||||
|
let low: Arc<Mutex<u32>> = Arc::new(Mutex::new(1000));
|
||||||
|
|
||||||
|
engine.entity(move |r| {
|
||||||
|
let current_frame = std::time::Instant::now();
|
||||||
|
let since = current_frame
|
||||||
|
.duration_since(last_frame.lock().unwrap().clone())
|
||||||
|
.as_millis();
|
||||||
|
|
||||||
|
if since > 0 {
|
||||||
|
let fps: u32 = 1000 / since as u32;
|
||||||
|
|
||||||
|
r.draw_text(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
format!("{fps}\n{}\n{}", peak.lock().unwrap(), low.lock().unwrap()),
|
||||||
|
);
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut peak = peak.lock().unwrap();
|
||||||
|
let mut low = low.lock().unwrap();
|
||||||
|
|
||||||
|
if fps > *peak {
|
||||||
|
*peak = fps;
|
||||||
|
}
|
||||||
|
|
||||||
|
if fps < *low {
|
||||||
|
*low = fps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*last_frame.lock().unwrap() = current_frame;
|
||||||
|
});
|
||||||
|
|
||||||
|
engine
|
||||||
|
.entity(|r| {
|
||||||
r.draw_vertex(
|
r.draw_vertex(
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@@ -27,7 +66,8 @@ fn main() {
|
|||||||
],
|
],
|
||||||
0xff0000,
|
0xff0000,
|
||||||
);
|
);
|
||||||
});
|
})
|
||||||
|
.set_position(20, 0);
|
||||||
|
|
||||||
terminal::hide_cursor().unwrap();
|
terminal::hide_cursor().unwrap();
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -99,7 +99,7 @@ impl Engine {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
self.render();
|
self.render();
|
||||||
sleep(160);
|
sleep(16);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,16 @@ impl Renderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn draw_text<T: std::fmt::Display>(&mut self, x: u16, y: u16, text: T) {
|
||||||
|
write_liner(
|
||||||
|
&mut self.buf,
|
||||||
|
self.pos.x + x,
|
||||||
|
self.pos.y + y,
|
||||||
|
"",
|
||||||
|
&text.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn draw_rect(&mut self, x: u16, y: u16, width: u16, height: u16, color: u32) {
|
pub fn draw_rect(&mut self, x: u16, y: u16, width: u16, height: u16, color: u32) {
|
||||||
let rl = " ".repeat(width as usize) + "\x1b[0m";
|
let rl = " ".repeat(width as usize) + "\x1b[0m";
|
||||||
let rect = (rl + "\n").repeat(height as usize);
|
let rect = (rl + "\n").repeat(height as usize);
|
||||||
|
|||||||
Reference in New Issue
Block a user