Explorar el Código

Implemented FrameCounter

raster
Bergmann89 hace 5 años
padre
commit
98c01e543b
Se han modificado 5 ficheros con 81 adiciones y 7 borrados
  1. +61
    -0
      space-crush/src/app/misc/frame_counter.rs
  2. +1
    -0
      space-crush/src/app/misc/mod.rs
  3. +1
    -1
      space-crush/src/app/misc/window.rs
  4. +10
    -2
      space-crush/src/app/render/init.rs
  5. +8
    -4
      space-crush/src/app/render/test.rs

+ 61
- 0
space-crush/src/app/misc/frame_counter.rs Ver fichero

@@ -0,0 +1,61 @@
#![allow(dead_code)]

use std::mem::take;
use std::time::Instant;

pub struct FrameCounter {
last_frame: Instant,
time: f32,
count: usize,
fps: usize,
delta: f32,
}

impl FrameCounter {
#[inline]
pub fn fps(&self) -> usize {
self.fps
}

#[inline]
pub fn delta(&self) -> f32 {
self.delta
}

#[inline]
pub fn next(&mut self) -> bool {
let now = Instant::now();

self.delta = (now - self.last_frame).as_secs_f32();
self.last_frame = now;
self.time += self.delta;
self.count += 1;

if self.time >= 2.0 {
self.time = 0.0;
self.fps = 0;
self.count = 0;

true
} else if self.time >= 1.0 {
self.time -= 1.0;
self.fps = take(&mut self.count);

true
} else {
false
}
}
}

impl Default for FrameCounter {
fn default() -> Self {
Self {
last_frame: Instant::now(),
time: 0.0,
count: 0,
fps: 0,
delta: 0.0,
}
}
}

+ 1
- 0
space-crush/src/app/misc/mod.rs Ver fichero

@@ -1,4 +1,5 @@
pub mod camera;
pub mod events;
pub mod frame_counter;
pub mod geometry;
pub mod window;

+ 1
- 1
space-crush/src/app/misc/window.rs Ver fichero

@@ -33,7 +33,7 @@ impl Window {
.with_hardware_acceleration(Some(true))
.with_pixel_format(24, 8)
.with_multisampling(*multisampling)
.with_vsync(true)
.with_vsync(false)
.with_gl(GlRequest::Specific(Api::OpenGl, (4, 5)))
.with_gl_profile(GlProfile::Core)
.build_windowed(window_builder, event_loop);


+ 10
- 2
space-crush/src/app/render/init.rs Ver fichero

@@ -2,24 +2,28 @@ use glc::{
misc::Bindable,
shader::{Program, Shader, Type},
};
use log::error;
use log::{error, info};
use shrev::{EventChannel, ReaderId};
use specs::{ReadExpect, System, World, WriteExpect};

use crate::Error;

use super::super::misc::{camera::Camera, events::WindowEvent, geometry::Geometry};
use super::super::misc::{
camera::Camera, events::WindowEvent, frame_counter::FrameCounter, geometry::Geometry,
};

/* Global */

pub struct Global {
pub camera: Camera,
pub frame_counter: FrameCounter,
}

impl Global {
pub fn new() -> Result<Self, Error> {
Ok(Self {
camera: Camera::new()?,
frame_counter: FrameCounter::default(),
})
}
}
@@ -82,6 +86,10 @@ impl<'a> System<'a> for Init {
}
}

if global.frame_counter.next() {
info!("FPS: {}", global.frame_counter.fps());
}

self.program.bind();
geometry.render_quad();
self.program.unbind();


+ 8
- 4
space-crush/src/app/render/test.rs Ver fichero

@@ -40,10 +40,14 @@ impl Test {
}

impl<'a> System<'a> for Test {
type SystemData = ReadExpect<'a, Geometry>;

fn run(&mut self, geometry: Self::SystemData) {
self.model = self.model * Matrix4f::rotate(Vector3f::new(0.0, 0.0, 1.0), Angle::Deg(0.01));
type SystemData = (ReadExpect<'a, Global>, ReadExpect<'a, Geometry>);

fn run(&mut self, (global, geometry): Self::SystemData) {
self.model = self.model
* Matrix4f::rotate(
Vector3f::new(0.0, 0.0, 1.0),
Angle::Deg(10.0 * global.frame_counter.delta()),
);

self.program.bind();
self.program.uniform(1, Uniform::Matrix4f(&self.model));


Cargando…
Cancelar
Guardar