-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fps overlay #12382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fps overlay #12382
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ed2c57a
fps overlay
matiqo15 4a3ffe9
add config for overlay
matiqo15 0cdfe37
add toggle to overlay
matiqo15 7fe771d
basic docs
matiqo15 fe69f27
add example
matiqo15 e237531
use KeyCode instead of Option<KeyCode> for keybind
matiqo15 edb1846
use run_if condition
matiqo15 b05af37
add text in example about toggling overlay
matiqo15 e0a3bec
remove unused import
matiqo15 ede91b8
remove toggling overlay
matiqo15 319744d
take TextStyle as a config
matiqo15 4dba1aa
rename system in fps_overlay example
matiqo15 140433f
add warning to FpsOverlayPlugin
matiqo15 75a97df
fix doc link
matiqo15 c5a733d
add docs about native overlay
matiqo15 db9c98c
fix doc
matiqo15 b2718b4
improve docs
matiqo15 4b380d8
move derive attribute
matiqo15 a6bd2db
use named fields in FpsOverlayConfig
matiqo15 8b160c8
move doc comment to module
matiqo15 2a4629e
add todo comment
matiqo15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! Module containing logic for FPS overlay. | ||
|
||
use bevy_app::{Plugin, Startup, Update}; | ||
use bevy_asset::Handle; | ||
use bevy_color::Color; | ||
use bevy_diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; | ||
use bevy_ecs::{ | ||
component::Component, | ||
query::With, | ||
schedule::{common_conditions::resource_changed, IntoSystemConfigs}, | ||
system::{Commands, Query, Res, Resource}, | ||
}; | ||
use bevy_text::{Font, Text, TextSection, TextStyle}; | ||
use bevy_ui::node_bundles::TextBundle; | ||
|
||
/// A plugin that adds an FPS overlay to the Bevy application. | ||
/// | ||
/// This plugin will add the [`FrameTimeDiagnosticsPlugin`] if it wasn't added before. | ||
/// | ||
/// Note: It is recommended to use native overlay of rendering statistics when possible for lower overhead and more accurate results. | ||
/// The correct way to do this will vary by platform: | ||
/// - **Metal**: setting env variable `MTL_HUD_ENABLED=1` | ||
#[derive(Default)] | ||
pub struct FpsOverlayPlugin { | ||
/// Starting configuration of overlay, this can be later be changed through [`FpsOverlayConfig`] resource. | ||
pub config: FpsOverlayConfig, | ||
} | ||
|
||
impl Plugin for FpsOverlayPlugin { | ||
fn build(&self, app: &mut bevy_app::App) { | ||
// TODO: Use plugin dependencies, see https://github.com/bevyengine/bevy/issues/69 | ||
if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() { | ||
matiqo15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app.add_plugins(FrameTimeDiagnosticsPlugin); | ||
} | ||
app.insert_resource(self.config.clone()) | ||
.add_systems(Startup, setup) | ||
.add_systems( | ||
Update, | ||
( | ||
customize_text.run_if(resource_changed::<FpsOverlayConfig>), | ||
update_text, | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// Configuration options for the FPS overlay. | ||
#[derive(Resource, Clone)] | ||
pub struct FpsOverlayConfig { | ||
/// Configuration of text in the overlay. | ||
pub text_config: TextStyle, | ||
} | ||
|
||
impl Default for FpsOverlayConfig { | ||
fn default() -> Self { | ||
FpsOverlayConfig { | ||
text_config: TextStyle { | ||
font: Handle::<Font>::default(), | ||
font_size: 32.0, | ||
color: Color::WHITE, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
struct FpsText; | ||
|
||
fn setup(mut commands: Commands, overlay_config: Res<FpsOverlayConfig>) { | ||
commands.spawn(( | ||
TextBundle::from_sections([ | ||
TextSection::new("FPS: ", overlay_config.text_config.clone()), | ||
TextSection::from_style(overlay_config.text_config.clone()), | ||
]), | ||
FpsText, | ||
)); | ||
} | ||
|
||
fn update_text(diagnostic: Res<DiagnosticsStore>, mut query: Query<&mut Text, With<FpsText>>) { | ||
for mut text in &mut query { | ||
if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS) { | ||
if let Some(value) = fps.smoothed() { | ||
text.sections[1].value = format!("{value:.2}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn customize_text( | ||
overlay_config: Res<FpsOverlayConfig>, | ||
mut query: Query<&mut Text, With<FpsText>>, | ||
) { | ||
for mut text in &mut query { | ||
for section in text.sections.iter_mut() { | ||
section.style = overlay_config.text_config.clone(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! Showcase how to use and configure FPS overlay. | ||
|
||
use bevy::{ | ||
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin}, | ||
prelude::*, | ||
}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(( | ||
DefaultPlugins, | ||
FpsOverlayPlugin { | ||
config: FpsOverlayConfig { | ||
text_config: TextStyle { | ||
// Here we define size of our overlay | ||
font_size: 50.0, | ||
// We can also change color of the overlay | ||
color: Color::srgb(0.0, 1.0, 0.0), | ||
// If we want, we can use a custom font | ||
font: default(), | ||
}, | ||
}, | ||
}, | ||
)) | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, customize_config) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands) { | ||
// We need to spawn camera to see overlay | ||
commands.spawn(Camera2dBundle::default()); | ||
commands.spawn( | ||
TextBundle::from_sections([ | ||
TextSection::new( | ||
"Press 1 to change color of the overlay.", | ||
TextStyle { | ||
font_size: 25.0, | ||
..default() | ||
}, | ||
), | ||
TextSection::new( | ||
"\nPress 2 to change size of the overlay", | ||
TextStyle { | ||
font_size: 25.0, | ||
..default() | ||
}, | ||
), | ||
]) | ||
.with_style(Style { | ||
justify_self: JustifySelf::Center, | ||
..default() | ||
}), | ||
); | ||
} | ||
|
||
fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) { | ||
if input.just_pressed(KeyCode::Digit1) { | ||
// Changing resource will affect overlay | ||
overlay.text_config.color = Color::srgb(1.0, 0.0, 0.0); | ||
} | ||
if input.just_pressed(KeyCode::Digit2) { | ||
overlay.text_config.font_size -= 2.0; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.