feat: add fps and frametime ui information
This commit is contained in:
parent
9d5a84b142
commit
10c789f569
|
@ -24,7 +24,7 @@ Key | Action
|
|||
<kbd>R</kbd> | Increase Scale
|
||||
<kbd>F</kbd> | Decrease Scale
|
||||
|
||||
#### Mandelbrot Iteration Limit (Max 512 by default)
|
||||
#### Mandelbrot Iteration Limit (Can go up to 1024)
|
||||
Key | Action
|
||||
--- | ---
|
||||
<kbd>T</kbd> | Increment by a factor of 2
|
||||
|
|
124
src/main.rs
124
src/main.rs
|
@ -1,19 +1,35 @@
|
|||
use bevy::diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin};
|
||||
use bevy::prelude::*;
|
||||
use bevy::render::texture::{Extent3d, TextureDimension, TextureFormat};
|
||||
use mandelbrot::{Bounds, Mandelbrot, TEXTURE_HEIGHT, TEXTURE_WIDTH};
|
||||
use std::time::Instant;
|
||||
|
||||
const MAX_ITERATION_COUNT: u32 = 1024;
|
||||
|
||||
fn main() {
|
||||
// Mandelbrot::escape_time_image();
|
||||
let window_settings = WindowDescriptor {
|
||||
width: TEXTURE_WIDTH as f32,
|
||||
height: TEXTURE_HEIGHT as f32,
|
||||
title: "Mandelbrot".to_string(),
|
||||
vsync: false,
|
||||
resizable: false,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
App::build()
|
||||
.insert_resource(window_settings)
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(startup.system())
|
||||
.add_plugin(FrameTimeDiagnosticsPlugin)
|
||||
.add_startup_system(setup.system())
|
||||
.insert_resource(TextureOptions::default())
|
||||
.add_system(performance_info_system.system())
|
||||
.add_system(mandelbrot_render_system.system())
|
||||
.add_system(transform_mandelbrot_system.system())
|
||||
.add_system(user_input_system.system())
|
||||
.run();
|
||||
}
|
||||
|
||||
struct PerformanceInfo;
|
||||
|
||||
struct TextureOptions {
|
||||
zoom: f64,
|
||||
horizontal: f64,
|
||||
|
@ -34,7 +50,7 @@ impl Default for TextureOptions {
|
|||
}
|
||||
}
|
||||
|
||||
fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<TextureOptions>) {
|
||||
fn user_input_system(input: Res<Input<KeyCode>>, mut scale: ResMut<TextureOptions>) {
|
||||
// Zoom: E to Zoom In, Q to Zoom Out
|
||||
// Horizontal Movement: D to move right, A to move left
|
||||
// Vertical Movement: W to move up, S to move Down
|
||||
|
@ -54,8 +70,8 @@ fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<Tex
|
|||
if limit_change == 1 {
|
||||
scale.iteration_limit *= 2;
|
||||
|
||||
if scale.iteration_limit > 512 {
|
||||
scale.iteration_limit = 512;
|
||||
if scale.iteration_limit > MAX_ITERATION_COUNT {
|
||||
scale.iteration_limit = MAX_ITERATION_COUNT;
|
||||
}
|
||||
} else if limit_change == -1 {
|
||||
scale.iteration_limit /= 2;
|
||||
|
@ -74,6 +90,35 @@ fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<Tex
|
|||
}
|
||||
}
|
||||
|
||||
fn performance_info_system(
|
||||
// time: Res<Time>,
|
||||
diag: Res<Diagnostics>,
|
||||
mut query: Query<&mut Text, With<PerformanceInfo>>,
|
||||
) {
|
||||
for mut text in query.iter_mut() {
|
||||
let mut fps = 0.00;
|
||||
let mut frametime = 0.00;
|
||||
|
||||
if let Some(fps_diag) = diag.get(FrameTimeDiagnosticsPlugin::FPS) {
|
||||
if let Some(average) = fps_diag.average() {
|
||||
fps = average;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(frametime_diag) = diag.get(FrameTimeDiagnosticsPlugin::FRAME_TIME) {
|
||||
if let Some(average) = frametime_diag.average() {
|
||||
frametime = average;
|
||||
}
|
||||
}
|
||||
|
||||
// FPS Number
|
||||
text.sections[1].value = format!("{:.1}", fps);
|
||||
|
||||
// Frametime Number
|
||||
text.sections[3].value = format!("{:.3}", frametime * 1000.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn mandelbrot_render_system(
|
||||
materials: Res<Assets<ColorMaterial>>,
|
||||
mut textures: ResMut<Assets<Texture>>,
|
||||
|
@ -106,11 +151,14 @@ fn mandelbrot_render_system(
|
|||
}
|
||||
}
|
||||
|
||||
fn startup(
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut textures: ResMut<Assets<Texture>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
let ui_font = asset_server.load("fonts/vcr_osd_mono.ttf");
|
||||
let white = Color::rgb(1.0, 1.0, 1.0);
|
||||
let mut fractal = Mandelbrot::default();
|
||||
|
||||
let texture_size = Extent3d::new(TEXTURE_WIDTH as u32, TEXTURE_HEIGHT as u32, 1);
|
||||
|
@ -124,10 +172,72 @@ fn startup(
|
|||
));
|
||||
|
||||
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
||||
commands.spawn_bundle(UiCameraBundle::default());
|
||||
|
||||
commands
|
||||
.spawn_bundle(SpriteBundle {
|
||||
material: materials.add(texture_handle.into()),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(fractal);
|
||||
|
||||
commands
|
||||
.spawn_bundle(TextBundle {
|
||||
text: Text {
|
||||
sections: vec![
|
||||
TextSection {
|
||||
value: "FPS: ".to_string(),
|
||||
style: TextStyle {
|
||||
font: ui_font.clone(),
|
||||
font_size: 40.0,
|
||||
color: white,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
TextSection {
|
||||
value: "".to_string(),
|
||||
style: TextStyle {
|
||||
font: ui_font.clone(),
|
||||
font_size: 40.0,
|
||||
color: white,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
TextSection {
|
||||
value: "\nFrametime: ".to_string(),
|
||||
style: TextStyle {
|
||||
font: ui_font.clone(),
|
||||
font_size: 40.0,
|
||||
color: white,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
TextSection {
|
||||
value: "".to_string(),
|
||||
style: TextStyle {
|
||||
font: ui_font.clone(),
|
||||
font_size: 40.0,
|
||||
color: white,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
TextSection {
|
||||
value: "ms".to_string(),
|
||||
style: TextStyle {
|
||||
font: ui_font.clone(),
|
||||
font_size: 40.0,
|
||||
color: white,
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
},
|
||||
style: Style {
|
||||
..Default::default()
|
||||
},
|
||||
|
||||
..Default::default()
|
||||
})
|
||||
.insert(PerformanceInfo);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue