Improve delta time (as in it actually works)

Also improve the debug screen
This commit is contained in:
2024-10-01 20:18:26 -06:00
parent 7766c261df
commit c7060bec70

View File

@@ -1,13 +1,57 @@
use core::f32;
use std::{io, time::{Duration, Instant}};
use macroquad::prelude::*;
#[macroquad::main("InputKeys")]
const DOT_RADIUS: f32 = 1.0;
struct Dot {
x: f32,
y: f32,
}
impl Dot {
fn new(x: f32, y: f32) -> Self {
Self {x,y}
}
fn draw(&self) {
draw_circle(self.x, self.y,DOT_RADIUS, GREEN);
}
}
struct DebugWindow {
text: Vec<String>,
text_margin: f32,
}
impl DebugWindow {
fn new() -> Self {
Self {
text: Vec::new(),
text_margin: 20.0,
}
}
fn add_line(&mut self, line: String) {
self.text.push(line);
}
fn draw(&self) {
let params = TextParams { color: WHITE, ..Default::default()} ;
self.text.iter().enumerate().for_each(|(index, text)| {
let x = self.text_margin;
let y = self.text_margin+20.*(index as f32 + 1.);
// let measurement = measure_text(text, None, params.font_size, params.font_scale);
// draw_rectangle(x,y,measurement.width,-measurement.height, Color { r: 1., g: 1., b: 1., a: 0.25 });
draw_text_ex(text, x, y, params.clone());
});
}
}
#[macroquad::main("Graph")]
async fn main() {
// Dot position
// Dot params
let y_offset = screen_height() / 2.0;
let mut x_offset = screen_width();
let mut trail: Vec<Dot> = Vec::new();
// Selection box
let mut inital_x_pos = 0.;
@@ -15,46 +59,36 @@ async fn main() {
// Tooling
let mut show_debug = false;
let mut last_frame = Instant::now();
let mut px_per_ms: f32 = 0.5;
let mut px_per_s: f32 = 100.;
loop {
clear_background(BLACK);
let delta_time = last_frame.elapsed().as_millis() as f32;
let delta_time = get_frame_time();
if x_offset >= screen_width() {
// wrap once you've hit the end of the line
x_offset = 0.;
trail.clear();
} else {
x_offset = x_offset + delta_time * px_per_ms;
x_offset = x_offset + (delta_time * px_per_s);
}
let x = 0.+x_offset;
let y = 0.+y_offset;
draw_circle(x, y, 3.0, GREEN);
fn text(text: &str, index: usize) {
let index = index as f32;
let text_h = 20.0;
let text_margin = 20.0;
draw_text(text, text_margin, text_h*index+text_margin, text_h, WHITE);
}
if show_debug {
text(&format!("FPS {}", get_fps()), 0);
text(&format!("Pos {:?}", (x,y)), 1);
text(&format!("Cursor {:?}, Last Click {:?}", mouse_position(), (inital_x_pos, inital_y_pos)), 2);
text(&format!("px/ms {px_per_ms}"), 3);
}
let dot = Dot::new(x,y);
trail.push(dot);
trail.iter().for_each(|d| d.draw());
// toggle debug box
if is_key_pressed(KeyCode::D) {
show_debug = !show_debug;
}
if is_key_pressed(KeyCode::Up) {
px_per_ms += 0.1;
px_per_s += 5.;
}
if is_key_pressed(KeyCode::Down) {
px_per_ms -= 0.1;
px_per_s -= 5.;
}
// selection box
if is_mouse_button_pressed(MouseButton::Left) {
@@ -66,11 +100,19 @@ async fn main() {
let width = x - inital_x_pos;
let height = y - inital_y_pos;
draw_rectangle(inital_x_pos, inital_y_pos, width, height, Color { r: 0.2, g: 0.1, b: 1., a: 0.5 });
draw_rectangle(inital_x_pos, inital_y_pos, width, height, Color { r: 0.2, g: 0.1, b: 1., a: 0.3 });
}
// toggle debug box
if show_debug {
let mut debug = DebugWindow::new();
debug.add_line(format!("FPS {}, Latency {:.2}ms", get_fps(), delta_time * 1000.0));
debug.add_line(format!("px/s {}", px_per_s));
debug.add_line(format!("Dots {}", trail.len()));
debug.add_line(format!("Cursor Pos {:?}", mouse_position()));
debug.draw();
}
last_frame = Instant::now();
next_frame().await
next_frame().await;
}
}