-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ABoschman/feature/fps_counter
Add FPS counter to top-left of screen
- Loading branch information
Showing
7 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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,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", ())), | ||
), | ||
) |
This file contains 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 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 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 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,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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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,3 @@ | ||
mod fps; | ||
|
||
pub use self::fps::UiFpsSystem; |