Skip to content
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

Add FPS counter to top-left of screen #42

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/font/square.ttf
Binary file not shown.
18 changes: 18 additions & 0 deletions assets/ui/fps.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![enable(implicit_some)]
Label(
transform: (
id: "fps_text",
anchor: TopLeft,
x: 100.,
y: -25.,
width: 200.,
height: 50.,
transparent: true,
),
text: (
text: "N/A",
font_size: 25.,
color: (1., 1., 1., 1.),
font: File("font/square.ttf", ("TTF", ())),
),
)
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use amethyst::{
types::DefaultBackend,
RenderingBundle,
},
utils::application_root_dir,
ui::{RenderUi, UiBundle},
utils::{application_root_dir, fps_counter::FpsCounterBundle},
Application, GameDataBuilder,
};

Expand Down Expand Up @@ -56,7 +57,10 @@ fn main() -> amethyst::Result<()> {
.with_dep(&["sprite_animation_control", "sprite_sampler_interpolation"]),
)?
.with_bundle(input_bundle)?
.with_bundle(FpsCounterBundle {})?
.with_bundle(UiBundle::<StringBindings>::new())?
.with(Processor::<Map>::new(), "map_processor", &[])
.with(UiFpsSystem::default(), "ui_fps_system", &[])
.with(MarineInputSystem, "marine_input_system", &[])
.with(
MarineKinematicsSystem,
Expand Down Expand Up @@ -139,7 +143,8 @@ fn main() -> amethyst::Result<()> {
RenderToWindow::from_config_path(display_config_path)
.with_clear([0.008, 0.043, 0.067, 1.0]),
)
.with_plugin(RenderFlat2D::default()),
.with_plugin(RenderFlat2D::default())
.with_plugin(RenderUi::default()),
)?;
let mut game =
Application::build(assets_path, states::LoadState::default())?.build(game_data)?;
Expand Down
3 changes: 3 additions & 0 deletions src/states/load.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use amethyst::{
assets::{AssetStorage, Handle, JsonFormat, Loader, ProgressCounter},
prelude::{GameData, SimpleState, SimpleTrans, StateData, Trans},
ui::UiCreator,
};

use crate::{
Expand Down Expand Up @@ -33,6 +34,8 @@ impl SimpleState for LoadState {
AssetType::Truss,
],
));
let mut progress = ProgressCounter::default();
world.exec(|mut creator: UiCreator<'_>| creator.create("ui/fps.ron", &mut progress));
self.map_handle = {
let loader = world.read_resource::<Loader>();
Some(loader.load(
Expand Down
2 changes: 2 additions & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod kinematics;
mod parallax;
mod pincer;
mod transformation;
mod ui;

pub use self::animation::AnimationControlSystem;
pub use self::animation::BulletImpactAnimationSystem;
Expand All @@ -27,3 +28,4 @@ pub use self::pincer::PincerAiSystem;
pub use self::transformation::BulletTransformationSystem;
pub use self::transformation::CameraTransformationSystem;
pub use self::transformation::TransformationSystem;
pub use self::ui::*;
40 changes: 40 additions & 0 deletions src/systems/ui/fps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use amethyst::{
core::Time,
ecs::prelude::{Entity, Read, System, WriteStorage},
ui::{UiFinder, UiText},
utils::fps_counter::FpsCounter,
};

/// Every 20 frames, this system updates the UI component for the FPS counter.
///
/// Source: The FPS counter, including font, assets/ui/fps.ron and this system was originally
/// copied from the Amethyst examples.
#[derive(Default)]
pub struct UiFpsSystem {
fps_display: Option<Entity>,
}

impl<'a> System<'a> for UiFpsSystem {
type SystemData = (
Read<'a, Time>,
WriteStorage<'a, UiText>,
Read<'a, FpsCounter>,
UiFinder<'a>,
);

fn run(&mut self, (time, mut ui_text, fps_counter, finder): Self::SystemData) {
if self.fps_display.is_none() {
if let Some(fps_entity) = finder.find("fps_text") {
self.fps_display = Some(fps_entity);
}
}
if let Some(fps_entity) = self.fps_display {
if let Some(fps_display) = ui_text.get_mut(fps_entity) {
if time.frame_number() % 20 == 0 {
let fps = fps_counter.sampled_fps();
fps_display.text = format!("FPS: {:.*}", 2, fps);
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/systems/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod fps;

pub use self::fps::UiFpsSystem;