64 lines
3.1 KiB
Rust
64 lines
3.1 KiB
Rust
use cgmath::{Vector4, vec4};
|
|
|
|
use crate::{input::InputState, text::{create_text_object, update_text}, vulkan::{MeshHandle, PERF_COUNTER_SIZE, TextVertex, VulkanRenderer, gameobject::{GameObject, GameObjectHandle, Updatable}, mesh::{CPUMesh, CPUVertexList}}};
|
|
|
|
use super::{GameState, TestGame};
|
|
|
|
pub struct FpsCounter {
|
|
pub game_object: GameObjectHandle
|
|
}
|
|
|
|
impl FpsCounter {
|
|
pub fn new(game: &mut TestGame, renderer: &mut VulkanRenderer) -> FpsCounter {
|
|
let text_mesh = create_text_object(&mut game.game_state.brush, renderer, "", 30.);
|
|
FpsCounter { game_object: game.add_game_object(renderer, text_mesh) }
|
|
}
|
|
}
|
|
|
|
impl Updatable for FpsCounter {
|
|
fn update(&mut self, _delta_time: f32, input: &InputState, game_state: &mut GameState, game_objects: &mut Vec<GameObject>, renderer: &mut VulkanRenderer) {
|
|
if input.button_just_pressed("toggle_framerate") {
|
|
let go = &mut game_objects[self.game_object];
|
|
go.visible = !go.visible;
|
|
}
|
|
|
|
let update_duration = renderer.game_data.update_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
|
|
let render_duration = renderer.game_data.render_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
|
|
let other_duration = renderer.game_data.other_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
|
|
let other_variance = renderer.game_data.other_perf_counters.iter().fold(0, |acc, b| acc + (*b as i128 - other_duration as i128) * (*b as i128 - other_duration as i128)) / PERF_COUNTER_SIZE as i128;
|
|
let text = format!("Update: {:.0}ms\nRender: {:.0}ms\nTotal: {:.0}ms (±{:.1})\nDelta: {:.0}ms", update_duration as f64 / 1000., render_duration as f64 / 1000., other_duration as f64 / 1000., f64::sqrt(other_variance as f64) / 1000., _delta_time * 1000.);
|
|
update_text(self.game_object, &text, 30., renderer, &mut game_state.brush, game_objects);
|
|
}
|
|
}
|
|
|
|
pub struct UiQuad {
|
|
pub game_object: GameObjectHandle,
|
|
pub background_color: Vector4<f32>,
|
|
}
|
|
|
|
impl UiQuad {
|
|
pub fn new(game: &mut TestGame, renderer: &mut VulkanRenderer) -> UiQuad {
|
|
let quad_verts = vec![
|
|
TextVertex { position: [0., 0., 0.], uv: [0., 0.] },
|
|
TextVertex { position: [0., 1., 0.], uv: [0., 1.] },
|
|
TextVertex { position: [1., 0., 0.], uv: [1., 0.] },
|
|
TextVertex { position: [1., 1., 0.], uv: [1., 1.] },
|
|
];
|
|
let cpu_mesh = CPUMesh { vertices: CPUVertexList::VertexText(quad_verts), indices: vec![0, 1, 2, 1, 3, 2], local_texture_index: None, local_normal_map_index: None, name: None };
|
|
let mesh_index = renderer.upload_mesh(cpu_mesh, None);
|
|
let mesh_handle = MeshHandle {
|
|
index: mesh_index,
|
|
textures: vec![],
|
|
original_path: None,
|
|
pipeline_index: 1
|
|
};
|
|
let go = game.add_game_object(renderer, mesh_handle);
|
|
UiQuad { game_object: go, background_color: vec4(0., 0., 255., 0.) }
|
|
}
|
|
}
|
|
|
|
impl Updatable for UiQuad {
|
|
fn update(&mut self, delta_time: f32, input: &InputState, game_state: &mut GameState, game_objects: &mut Vec<GameObject>, renderer: &mut VulkanRenderer) {
|
|
|
|
}
|
|
} |