├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE ├── README.md ├── examples └── debug.rs └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | *# 4 | *.o 5 | *.so 6 | *.swp 7 | *.old 8 | *.bak 9 | *.kate-swp 10 | *.dylib 11 | *.dSYM 12 | *.dll 13 | *.rlib 14 | *.dummy 15 | *.exe 16 | *-test 17 | /bin/main 18 | /bin/test-internal 19 | /bin/test-external 20 | /doc/ 21 | /target/ 22 | /build/ 23 | /.rust/ 24 | rusti.sh 25 | watch.sh 26 | /examples/** 27 | !/examples/*.rs 28 | !/examples/assets/ 29 | !/bin/assets/ 30 | 31 | Cargo.lock 32 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "fps_counter" 4 | version = "3.0.0" 5 | authors = ["bvssvni ", "eddyb"] 6 | keywords = ["fps", "counter", "frames", "tick", "piston"] 7 | description = "A Frames Per Second (FPS) counter" 8 | license = "MIT" 9 | readme = "README.md" 10 | repository = "https://github.com/pistondevelopers/fps_counter.git" 11 | homepage = "https://github.com/pistondevelopers/fps_counter" 12 | 13 | [lib] 14 | 15 | name = "fps_counter" 16 | path = "src/lib.rs" 17 | 18 | [features] 19 | stdweb = ["instant/stdweb"] 20 | wasm-bindgen = ["instant/wasm-bindgen"] 21 | 22 | [dependencies.instant] 23 | version = "0.1.12" 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 PistonDevelopers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | fps_counter [![Build Status](https://travis-ci.org/PistonDevelopers/fps_counter.svg)](https://travis-ci.org/PistonDevelopers/fps_counter) 2 | =========== 3 | 4 | A Frames Per Second counter 5 | 6 | [How to contribute](https://github.com/PistonDevelopers/piston/blob/master/CONTRIBUTING.md) 7 | -------------------------------------------------------------------------------- /examples/debug.rs: -------------------------------------------------------------------------------- 1 | extern crate fps_counter; 2 | 3 | use fps_counter::*; 4 | 5 | fn main() { 6 | let mut a = FPSCounter::default(); 7 | a.tick(); 8 | println!("{:?}", a); 9 | } 10 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A Frames Per Second counter. 2 | extern crate instant; 3 | use instant::Instant; 4 | use std::{collections::VecDeque, time::Duration}; 5 | 6 | /// Measures Frames Per Second (FPS). 7 | #[derive(Debug)] 8 | pub struct FPSCounter { 9 | /// The last registered frames. 10 | last_second_frames: VecDeque, 11 | } 12 | 13 | impl Default for FPSCounter { 14 | fn default() -> Self { 15 | FPSCounter::new() 16 | } 17 | } 18 | 19 | impl FPSCounter { 20 | /// Creates a new FPSCounter. 21 | pub fn new() -> FPSCounter { 22 | FPSCounter { 23 | last_second_frames: VecDeque::with_capacity(128), 24 | } 25 | } 26 | 27 | /// Updates the FPSCounter and returns number of frames. 28 | pub fn tick(&mut self) -> usize { 29 | let now = Instant::now(); 30 | let a_second_ago = now - Duration::from_secs(1); 31 | 32 | while self 33 | .last_second_frames 34 | .front() 35 | .map_or(false, |t| *t < a_second_ago) 36 | { 37 | self.last_second_frames.pop_front(); 38 | } 39 | 40 | self.last_second_frames.push_back(now); 41 | self.last_second_frames.len() 42 | } 43 | } 44 | --------------------------------------------------------------------------------