Display FPS in hud (copied from example)

This commit is contained in:
Felix Ableitner 2021-01-03 17:43:39 +01:00
parent 001ce7e98c
commit edba5d013d
3 changed files with 61 additions and 1 deletions

58
src/hud.rs Normal file
View file

@ -0,0 +1,58 @@
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
};
pub struct HudPlugin;
impl Plugin for HudPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(setup.system())
.add_system(fps_update_system.system());
}
}
// A unit struct to help identify the FPS UI component, since there may be many Text components
struct FpsText;
fn fps_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text, With<FpsText>>) {
for mut text in query.iter_mut() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.value() {
text.value = format!("{:.0} fps", average);
}
}
}
}
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
commands
// UI camera
.spawn(CameraUiBundle::default())
// texture
.spawn(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
top: Val::Px(5.0),
right: Val::Px(5.0),
..Default::default()
},
..Default::default()
},
text: Text {
value: "".to_string(),
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
style: TextStyle {
font_size: 16.0,
color: Color::WHITE,
..Default::default()
},
},
..Default::default()
})
.with(FpsText);
}

View file

@ -1,2 +1,3 @@
pub mod hud;
pub mod player; pub mod player;
pub mod world; pub mod world;

View file

@ -1,6 +1,6 @@
use bevy::{input::system::exit_on_esc_system, prelude::*}; use bevy::{input::system::exit_on_esc_system, prelude::*};
use bevy_rapier3d::physics::RapierPhysicsPlugin; use bevy_rapier3d::physics::RapierPhysicsPlugin;
use rake::{player::PlayerPlugin, world::WorldPlugin}; use rake::{hud::HudPlugin, player::PlayerPlugin, world::WorldPlugin};
fn main() { fn main() {
App::build() App::build()
@ -16,6 +16,7 @@ fn main() {
// note: debug renderer doesnt work for cylinder // note: debug renderer doesnt work for cylinder
//.add_plugin(RapierRenderPlugin) //.add_plugin(RapierRenderPlugin)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_plugin(HudPlugin)
.add_plugin(WorldPlugin) .add_plugin(WorldPlugin)
.add_system(exit_on_esc_system.system()) .add_system(exit_on_esc_system.system())
.run(); .run();