From 8fce91e74456d37cd95da085e4f2e16e37093e0b Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sun, 13 Apr 2025 16:17:18 +0200 Subject: [PATCH] add fps counter plugin --- Cargo.lock | 29 +++++++++++++++++++++ Cargo.toml | 2 +- src/fps_counter.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 ++++-- 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/fps_counter.rs diff --git a/Cargo.lock b/Cargo.lock index e80d908..8f1fb0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -512,6 +512,34 @@ dependencies = [ "syn", ] +[[package]] +name = "bevy_dev_tools" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e59e16048d567e986929860aa7f9da847770f0a6a244069deacb2addeeb78b3" +dependencies = [ + "bevy_app", + "bevy_asset", + "bevy_color", + "bevy_core", + "bevy_core_pipeline", + "bevy_diagnostic", + "bevy_ecs", + "bevy_gizmos", + "bevy_hierarchy", + "bevy_input", + "bevy_math", + "bevy_reflect", + "bevy_render", + "bevy_state", + "bevy_text", + "bevy_time", + "bevy_transform", + "bevy_ui", + "bevy_utils", + "bevy_window", +] + [[package]] name = "bevy_diagnostic" version = "0.15.3" @@ -724,6 +752,7 @@ dependencies = [ "bevy_core", "bevy_core_pipeline", "bevy_derive", + "bevy_dev_tools", "bevy_diagnostic", "bevy_ecs", "bevy_gilrs", diff --git a/Cargo.toml b/Cargo.toml index 157e0ee..0f3864b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -bevy = "0.15.0" +bevy = { version = "0.15.0", features = ["bevy_dev_tools"] } diff --git a/src/fps_counter.rs b/src/fps_counter.rs new file mode 100644 index 0000000..759e53a --- /dev/null +++ b/src/fps_counter.rs @@ -0,0 +1,63 @@ +use bevy::{ + dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin}, + prelude::*, + text::FontSmoothing, +}; +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 FpsCounterPlugin; +impl Plugin for FpsCounterPlugin { + 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(Update, customize_config); + } +} +fn setup(mut commands: Commands) { + 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 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 5ff9d23..c521725 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ use bevy::prelude::*; +mod fps_counter; fn main() { App::new() - .add_plugins(DefaultPlugins) - .add_plugins(HelloPlugin) + .add_plugins(( + DefaultPlugins, + fps_counter::FpsCounterPlugin, + )) .run(); } //region first app