add fps counter plugin

This commit is contained in:
OMGeeky
2025-04-13 16:17:18 +02:00
parent 6347b4f4ff
commit 8fce91e744
4 changed files with 98 additions and 3 deletions

29
Cargo.lock generated
View File

@@ -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",

View File

@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
bevy = "0.15.0"
bevy = { version = "0.15.0", features = ["bevy_dev_tools"] }

63
src/fps_counter.rs Normal file
View File

@@ -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<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
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;
}
}

View File

@@ -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