Adds FPS counter

master
Joey Pollack 2 years ago
parent ef8bbe0c55
commit 09f8c2a49d

@ -1,5 +1,10 @@
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{
prelude::*,
diagnostic::{FrameTimeDiagnosticsPlugin, DiagnosticsStore},
window::{PresentMode, WindowTheme},
sprite::MaterialMesh2dBundle};
use std::time::Instant; use std::time::Instant;
mod neural_net; mod neural_net;
@ -7,6 +12,9 @@ mod neural_net;
#[derive(Component)] #[derive(Component)]
struct Agent; struct Agent;
#[derive(Component)]
struct FpsText;
#[derive(Resource)] #[derive(Resource)]
struct SimulationState struct SimulationState
{ {
@ -29,11 +37,27 @@ impl SimulationState
fn main() fn main()
{ {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Life Sim".into(),
resolution: (1280., 720.).into(),
present_mode: PresentMode::AutoVsync,
// Tells wasm to resize the window according to the available canvas
fit_canvas_to_parent: true,
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
prevent_default_event_handling: false,
window_theme: Some(WindowTheme::Dark),
..default()
}),
..default()
}),
FrameTimeDiagnosticsPlugin
))
.insert_resource(SimulationState::new(200)) .insert_resource(SimulationState::new(200))
.insert_resource(Time::new(Instant::now())) .insert_resource(Time::new(Instant::now()))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, tick_simulation) .add_systems(Update, (tick_simulation, ui_update))
.run(); .run();
} }
@ -53,6 +77,29 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
}, },
Agent Agent
)); ));
// UI
// Text with multiple sections
commands.spawn((
// Create a TextBundle that has a Text with a list of sections.
TextBundle::from_sections([
TextSection::new(
"FPS: ",
TextStyle {
font_size: 60.0,
color: Color::WHITE,
..default()
},
),
TextSection::from_style(TextStyle {
font_size: 60.0,
color: Color::GOLD,
..default()
}),
]),
FpsText,
));
} }
fn tick_simulation(mut state: ResMut<SimulationState>, delta_time: Res<Time>, mut query: Query<&mut Transform, With<Agent>>) fn tick_simulation(mut state: ResMut<SimulationState>, delta_time: Res<Time>, mut query: Query<&mut Transform, With<Agent>>)
@ -66,7 +113,7 @@ fn tick_simulation(mut state: ResMut<SimulationState>, delta_time: Res<Time>, mu
// Process agent actions // Process agent actions
let mut agent_transform = query.single_mut(); let mut agent_transform = query.single_mut();
agent_transform.translation.x += 0.5; agent_transform.translation.x += 50.0 * delta_time.delta_seconds();
let elapsed_time = now.elapsed(); let elapsed_time = now.elapsed();
state.last_gen_time += state.last_step_time; state.last_gen_time += state.last_step_time;
@ -81,3 +128,18 @@ fn tick_simulation(mut state: ResMut<SimulationState>, delta_time: Res<Time>, mu
} }
} }
fn ui_update(mut state: Res<SimulationState>, diagnostics: Res<DiagnosticsStore>, mut query: Query<&mut Text, With<FpsText>>)
{
for mut text in &mut query
{
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS)
{
if let Some(value) = fps.smoothed()
{
// Update the value of the second section
text.sections[1].value = format!("{value:.2}");
}
}
}
}
Loading…
Cancel
Save