mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
add fps printer system to diagnostics mod
This commit is contained in:
parent
41e15a2d90
commit
ad654a3c25
7 changed files with 39 additions and 64 deletions
|
@ -1,6 +1,5 @@
|
|||
use bevy::prelude::*;
|
||||
use rand::{random, rngs::StdRng, Rng, SeedableRng};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
struct Person;
|
||||
|
||||
|
@ -25,7 +24,7 @@ fn main() {
|
|||
.add_system(build_wander_system())
|
||||
.add_system(build_navigate_system())
|
||||
.add_system(build_move_system())
|
||||
.add_system(build_print_status_system())
|
||||
.add_system(bevy::diagnostics::build_fps_printer_system())
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -141,37 +140,6 @@ fn build_move_system() -> Box<dyn Schedulable> {
|
|||
})
|
||||
}
|
||||
|
||||
fn build_print_status_system() -> Box<dyn Schedulable> {
|
||||
let mut elapsed = 0.0;
|
||||
let mut frame_time_total = 0.0;
|
||||
let mut frame_time_count = 0;
|
||||
let frame_time_max = 10;
|
||||
let mut frame_time_values = VecDeque::new();
|
||||
SystemBuilder::new("PrintStatus")
|
||||
.read_resource::<Time>()
|
||||
.with_query(<(Read<Person>,)>::query())
|
||||
.build(move |_, world, time, person_query| {
|
||||
elapsed += time.delta_seconds;
|
||||
frame_time_values.push_front(time.delta_seconds);
|
||||
frame_time_total += time.delta_seconds;
|
||||
frame_time_count += 1;
|
||||
if frame_time_count > frame_time_max {
|
||||
frame_time_count = frame_time_max;
|
||||
frame_time_total -= frame_time_values.pop_back().unwrap();
|
||||
}
|
||||
if elapsed > 1.0 {
|
||||
if frame_time_count > 0 && frame_time_total > 0.0 {
|
||||
println!(
|
||||
"fps: {}",
|
||||
1.0 / (frame_time_total / frame_time_count as f32)
|
||||
)
|
||||
}
|
||||
println!("peeps: {}", person_query.iter(world).count());
|
||||
elapsed = 0.0;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Translation) {
|
||||
world.insert(
|
||||
(),
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use bevy::prelude::*;
|
||||
use rand::{rngs::StdRng, Rng, SeedableRng};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
fn main() {
|
||||
AppBuilder::new()
|
||||
.add_defaults()
|
||||
.add_system(build_move_system())
|
||||
.add_system(build_print_status_system())
|
||||
.add_system(bevy::diagnostics::build_fps_printer_system())
|
||||
.setup_world(setup)
|
||||
.run();
|
||||
}
|
||||
|
@ -27,35 +26,6 @@ fn build_move_system() -> Box<dyn Schedulable> {
|
|||
})
|
||||
}
|
||||
|
||||
fn build_print_status_system() -> Box<dyn Schedulable> {
|
||||
let mut elapsed = 0.0;
|
||||
let mut frame_time_total = 0.0;
|
||||
let mut frame_time_count = 0;
|
||||
let frame_time_max = 10;
|
||||
let mut frame_time_values = VecDeque::new();
|
||||
SystemBuilder::new("PrintStatus")
|
||||
.read_resource::<Time>()
|
||||
.build(move |_, _world, time, _queries| {
|
||||
elapsed += time.delta_seconds;
|
||||
frame_time_values.push_front(time.delta_seconds);
|
||||
frame_time_total += time.delta_seconds;
|
||||
frame_time_count += 1;
|
||||
if frame_time_count > frame_time_max {
|
||||
frame_time_count = frame_time_max;
|
||||
frame_time_total -= frame_time_values.pop_back().unwrap();
|
||||
}
|
||||
if elapsed > 1.0 {
|
||||
if frame_time_count > 0 && frame_time_total > 0.0 {
|
||||
println!(
|
||||
"fps: {}",
|
||||
1.0 / (frame_time_total / frame_time_count as f32)
|
||||
)
|
||||
}
|
||||
elapsed = 0.0;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn setup(world: &mut World, resources: &mut Resources) {
|
||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||
let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube));
|
||||
|
|
32
src/diagnostics/mod.rs
Normal file
32
src/diagnostics/mod.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
use crate::{core::Time, prelude::SystemBuilder};
|
||||
use legion::prelude::Schedulable;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub fn build_fps_printer_system() -> Box<dyn Schedulable> {
|
||||
let mut elapsed = 0.0;
|
||||
let mut frame_time_total = 0.0;
|
||||
let mut frame_time_count = 0;
|
||||
let frame_time_max = 10;
|
||||
let mut frame_time_values = VecDeque::new();
|
||||
SystemBuilder::new("FpsPrinter")
|
||||
.read_resource::<Time>()
|
||||
.build(move |_, _world, time, _queries| {
|
||||
elapsed += time.delta_seconds;
|
||||
frame_time_values.push_front(time.delta_seconds);
|
||||
frame_time_total += time.delta_seconds;
|
||||
frame_time_count += 1;
|
||||
if frame_time_count > frame_time_max {
|
||||
frame_time_count = frame_time_max;
|
||||
frame_time_total -= frame_time_values.pop_back().unwrap();
|
||||
}
|
||||
if elapsed > 1.0 {
|
||||
if frame_time_count > 0 && frame_time_total > 0.0 {
|
||||
println!(
|
||||
"fps: {}",
|
||||
1.0 / (frame_time_total / frame_time_count as f32)
|
||||
)
|
||||
}
|
||||
elapsed = 0.0;
|
||||
}
|
||||
})
|
||||
}
|
|
@ -8,6 +8,7 @@ pub mod prelude;
|
|||
pub mod render;
|
||||
pub mod serialization;
|
||||
pub mod ui;
|
||||
pub mod diagnostics;
|
||||
|
||||
pub use bevy_transform as transform;
|
||||
pub use glam as math;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Anchors {
|
||||
pub left: f32,
|
||||
pub right: f32,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Margins {
|
||||
pub left: f32,
|
||||
pub right: f32,
|
||||
|
|
|
@ -5,11 +5,13 @@ use crate::{
|
|||
ui::{Anchors, Margins},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum GrowDirection {
|
||||
Negative,
|
||||
Positive,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Node {
|
||||
pub position: Vec2,
|
||||
pub global_position: Vec2,
|
||||
|
|
Loading…
Reference in a new issue