From a72b76a234a40bbc5bb1436cbd0e0bd43a75154b Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 17 Mar 2024 15:23:22 +0100 Subject: [PATCH] add HUD with FPS display --- src/hud.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/hud.rs diff --git a/src/hud.rs b/src/hud.rs new file mode 100644 index 0000000..8fabc71 --- /dev/null +++ b/src/hud.rs @@ -0,0 +1,68 @@ +use bevy::prelude::*; +use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; + +pub struct OutFlyHudPlugin; +impl Plugin for OutFlyHudPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, setup); + app.add_systems(Update, update); + app.insert_resource(FPSUpdateTimer(Timer::from_seconds(0.25, TimerMode::Repeating))); + } +} + +#[derive(Component)] +struct FpsText; + +#[derive(Resource)] +struct FPSUpdateTimer(Timer); + +fn setup( + mut commands: Commands, +) { + commands.spawn(( + TextBundle::from_sections([ + TextSection::new( + "FPS: ", + TextStyle { + //font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 60.0, + ..default() + }, + ), + TextSection::from_style(if cfg!(feature = "default_font") { + TextStyle { + font_size: 60.0, + color: Color::GOLD, + ..default() + } + } else { + // "default_font" feature is unavailable, load a font to use instead. + TextStyle { + //font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font_size: 60.0, + color: Color::GOLD, + ..default() + } + }), + ]), + FpsText, + )); +} + +fn update( + diagnostics: Res, + time:Res