Skip to content

Commit

Permalink
feature: Add debug FPS counter
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed May 10, 2021
1 parent a27053f commit 023cfe6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ First, thank you for considering contributing to Hourai! It's people like you th
We welcome any type of contribution, not only code. You can help with
- **QA**: file bug reports, the more details you can give the better (e.g.
screenshots of the bot's behavior)
- **Marketing**: writing howto's, voting for the bot on
[top.gg](https://top.gg/), ...
- **Translation**: Translating documentation, bot messages, etc. to other
languages.
- **Documentaion**: Hourai is a complicated bot, improving documentation and
Expand Down
Binary file added game/assets/fonts/FiraMono-Medium.ttf
Binary file not shown.
Binary file added game/assets/fonts/FiraSans-Bold.ttf
Binary file not shown.
56 changes: 56 additions & 0 deletions game/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*
};

fn start_debug(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
},
text: Text {
sections: vec![
TextSection {
value: "FPS: ".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 90.0,
color: Color::WHITE,
},
},
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 90.0,
color: Color::GOLD,
},
},
],
..Default::default()
},
..Default::default()
});
}

fn update_fps_counter(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text>) {
for mut text in query.iter_mut() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() {
text.sections[1].value = format!("{:.2}", average);
}
}
}
}

pub struct FcDebugPlugin;

impl Plugin for FcDebugPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.add_plugin(FrameTimeDiagnosticsPlugin)
.add_startup_system(start_debug.system())
.add_system(update_fps_counter.system());
}
}
26 changes: 17 additions & 9 deletions game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use fc_core::input::*;
use std::collections::HashMap;

mod data;
#[cfg(debug_assertions)]
mod debug;
mod input;
mod r#match;

Expand All @@ -30,8 +32,8 @@ fn create_input_source(arrow: ButtonAxis2D<KeyCode>) -> InputSource {
}

fn main() {
App::build()
.insert_resource(WindowDescriptor {
let mut app = App::build();
app .insert_resource(WindowDescriptor {
title: "Fantasy Crescendo".to_string(),
vsync: true,
..Default::default()
Expand Down Expand Up @@ -81,8 +83,13 @@ fn main() {
None,
],
})
.add_startup_system(setup.system())
.run();
.add_startup_system(setup.system());

// Optional Plugins
#[cfg(debug_assertions)]
app.add_plugin(debug::FcDebugPlugin);

app.run();
}

/// set up a simple 3D scene
Expand All @@ -91,6 +98,12 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// cameras
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 500.0 })),
Expand All @@ -102,9 +115,4 @@ fn setup(
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}

0 comments on commit 023cfe6

Please sign in to comment.