From 9f6d5eb9fa3f4591a179138cdd26690e5f0b0da7 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Mon, 14 Apr 2025 19:48:28 +0200 Subject: [PATCH] allow simple and complex fps counter --- src/fps_counter.rs | 156 +++++++++++++++++++++++++-------------------- src/main.rs | 2 +- 2 files changed, 89 insertions(+), 69 deletions(-) diff --git a/src/fps_counter.rs b/src/fps_counter.rs index 6d58af3..32a3394 100644 --- a/src/fps_counter.rs +++ b/src/fps_counter.rs @@ -1,73 +1,93 @@ -use bevy::{ - // dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin}, - prelude::*, -}; -use iyes_perf_ui::PerfUiPlugin; -use iyes_perf_ui::prelude::*; +#[allow(unused_imports)] +pub use in_depth_fps_overlay::*; +#[allow(unused_imports)] +pub use simple_fps_overlay::*; -// struct OverlayColor; -// impl OverlayColor { -// const RED: Color = Color::srgb(1.0, 0.0, 0.0); -// const GREEN: Color = Color::srgb(0.0, 1.0, 0.0); -// } +mod simple_fps_overlay { + use bevy::dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin}; + use bevy::prelude::*; -pub struct FpsCounterPlugin; -impl Plugin for FpsCounterPlugin { - fn build(&self, app: &mut App) { - app - // app.add_plugins(FpsOverlayPlugin { - // config: FpsOverlayConfig { - // text_config: TextFont { - // font_size: 42.0, - // ..default() - // }, - // text_color: OverlayColor::RED, - // enabled: true, - // }, - // }) - .add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin) - .add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin) - .add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin) - .add_plugins(bevy::render::diagnostic::RenderDiagnosticsPlugin) - .add_plugins(PerfUiPlugin) - .add_systems(Startup, setup) - // .add_systems(Update, customize_config) - ; + struct OverlayColor; + impl OverlayColor { + const RED: Color = Color::srgb(1.0, 0.0, 0.0); + const GREEN: Color = Color::srgb(0.0, 1.0, 0.0); + } + pub struct SimpleFpsCounterPlugin; + impl Plugin for SimpleFpsCounterPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(FpsOverlayPlugin { + config: FpsOverlayConfig { + text_config: TextFont { + font_size: 42.0, + ..default() + }, + text_color: OverlayColor::RED, + enabled: true, + }, + }) + .add_systems(Startup, setup) + .add_systems(PostStartup, set_initial_config) + .add_systems(Update, customize_config); + } + } + fn setup(mut commands: Commands) { + // #[allow(unreachable_code)] + if false { + commands.spawn(( + Text::new(concat!( + "Press 1 to toggle the overlay color.\n", + "Press 2 to decrease the overlay size.\n", + "Press 3 to increase the overlay size.\n", + "Press 4 to toggle the overlay visibility.", + )), + Node { + position_type: PositionType::Absolute, + bottom: Val::Px(12.0), + left: Val::Px(12.0), + ..default() + }, + )); + } + } + fn set_initial_config(mut overlay: ResMut) { + overlay.enabled = false; + } + fn customize_config(input: Res>, mut overlay: ResMut) { + if input.just_pressed(KeyCode::Digit1) { + // Changing resource will affect overlay + if overlay.text_color == OverlayColor::GREEN { + overlay.text_color = OverlayColor::RED; + } else { + overlay.text_color = OverlayColor::GREEN; + } + } + if input.just_pressed(KeyCode::Digit2) { + overlay.text_config.font_size -= 2.0; + } + if input.just_pressed(KeyCode::Digit3) { + overlay.text_config.font_size += 2.0; + } + if input.just_pressed(KeyCode::Digit4) { + overlay.enabled = !overlay.enabled; + } } } -fn setup(mut commands: Commands) { - commands.spawn(PerfUiAllEntries::default()); - // commands.spawn(( - // Text::new(concat!( - // "Press 1 to toggle the overlay color.\n", - // "Press 2 to decrease the overlay size.\n", - // "Press 3 to increase the overlay size.\n", - // "Press 4 to toggle the overlay visibility.", - // )), - // Node { - // position_type: PositionType::Absolute, - // bottom: Val::Px(12.0), - // left: Val::Px(12.0), - // ..default() - // }, - // )); +mod in_depth_fps_overlay { + use bevy::prelude::*; + use iyes_perf_ui::prelude::*; + + pub struct FpsCounterPlugin; + impl Plugin for FpsCounterPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin) + .add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin) + .add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin) + .add_plugins(bevy::render::diagnostic::RenderDiagnosticsPlugin) + .add_plugins(PerfUiPlugin) + .add_systems(Startup, setup); + } + } + fn setup(mut commands: Commands) { + commands.spawn(PerfUiAllEntries::default()); + } } -// fn customize_config(input: Res>, mut overlay: ResMut) { -// if input.just_pressed(KeyCode::Digit1) { -// // Changing resource will affect overlay -// if overlay.text_color == OverlayColor::GREEN { -// overlay.text_color = OverlayColor::RED; -// } else { -// overlay.text_color = OverlayColor::GREEN; -// } -// } -// if input.just_pressed(KeyCode::Digit2) { -// overlay.text_config.font_size -= 2.0; -// } -// if input.just_pressed(KeyCode::Digit3) { -// overlay.text_config.font_size += 2.0; -// } -// if input.just_pressed(KeyCode::Digit4) { -// overlay.enabled = !overlay.enabled; -// } -// } diff --git a/src/main.rs b/src/main.rs index d6d0063..7dde00c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ fn main() { .add_plugins(( DefaultPlugins, CameraPlugin, - // fps_counter::FpsCounterPlugin, + fps_counter::SimpleFpsCounterPlugin, // ShapeFollowPlugin, LogicSimPlugin, ))