1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*!
 * Time utilities
 */

use game_time;
use rinecs::{Entities, Resources, EntitiesThreadLocal, ResourcesThreadLocal};
use crate::Bundle;
use game_time::framerate::RunningAverageSampler;
pub use game_time::GameClock as Clock;
pub type FrameCounter = game_time::FrameCounter<RunningAverageSampler>;
pub use game_time::FrameCount;
use rin::color::consts::*;
use rin::window::{self, WindowT};
use rin::graphics::Mvp;
use crate::Scene;

#[cfg(any(feature="gl", feature="gles", feature="webgl"))]
use rin::gl;

pub struct Time;

impl Bundle for Time{
    fn setup(self, world: &mut Scene){
        world.add_resource(Clock::new());
        world.add_resource(FrameCounter::new(60.0, RunningAverageSampler::with_max_samples(60)));
        world.add_system_with_name(update_clock, "update clock");
    }

    fn register(_world: &mut Scene){
    }
}

fn update_clock(_entities: Entities, resources: Resources){
    let sim_time = resources.get_mut::<Clock>()
        .unwrap()
        .tick(&game_time::step::VariableStep::new());
    resources.get_mut::<FrameCounter>()
        .unwrap()
        .tick(&sim_time);
}

#[cfg(any(feature="gl", feature="gles", feature="webgl"))]
pub fn fps_renderer(_entities: EntitiesThreadLocal, resources: ResourcesThreadLocal){
    let glin = resources.get::<gl::Renderer<'static>>().unwrap();
    let window = resources.get::<window::Window>().unwrap();
    let viewport = window.viewport();
    if let Some(fps) = resources.get::<FrameCounter>().map(|counter| counter.average_frame_rate()) {
        let glin = glin.with_mvp(Mvp::ortho_top_left(window.viewport()));
        glin.draw_bitmap_string(&format!("{:#.02}", fps), viewport.width as f32 - 60., 20., &WHITE);
        let fps = window.fps();
        glin.draw_bitmap_string(&format!("{:#.02}", fps), viewport.width as f32 - 60., 40., &WHITE);
    }
}