From 10c789f5690ed69a97b708d15270506a40cdac39 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 6 Apr 2021 23:53:54 -0500 Subject: [PATCH] feat: add fps and frametime ui information --- README.md | 2 +- src/main.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 118 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 30a354b..80508d7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Key | Action R | Increase Scale F | Decrease Scale -#### Mandelbrot Iteration Limit (Max 512 by default) +#### Mandelbrot Iteration Limit (Can go up to 1024) Key | Action --- | --- T | Increment by a factor of 2 diff --git a/src/main.rs b/src/main.rs index d728941..bbdb168 100644 --- a/src/main.rs +++ b/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>, mut scale: ResMut) { +fn user_input_system(input: Res>, mut scale: ResMut) { // 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>, mut scale: ResMut 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>, mut scale: ResMut, + diag: Res, + mut query: Query<&mut Text, With>, +) { + 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>, mut textures: ResMut>, @@ -106,11 +151,14 @@ fn mandelbrot_render_system( } } -fn startup( +fn setup( mut commands: Commands, mut textures: ResMut>, mut materials: ResMut>, + asset_server: Res, ) { + 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); }