use bevy::{prelude::*, render::texture::TextureFormat}; use mandelbrot::Mandelbrot; use std::time::Instant; fn main() { // Mandelbrot::escape_time_image(); App::build() .add_plugins(DefaultPlugins) .add_startup_system(startup.system()) .add_resource(SpriteScale::default()) .add_system(mandelbrot_render_system.system()) .add_system(transform_mandelbrot_system.system()) .run(); } struct SpriteScale { zoom: f64, horiz: f64, verti: f64, step: f64, } impl Default for SpriteScale { fn default() -> Self { Self { zoom: 1.0, horiz: 0.0, verti: 0.0, step: 0.01, } } } fn transform_mandelbrot_system( input: Res>, mut scale: ResMut, // camera: Res, ) { let zoom = input.pressed(KeyCode::Q) as i8 - input.pressed(KeyCode::E) as i8; let horizontal = input.pressed(KeyCode::D) as i8 - input.pressed(KeyCode::A) as i8; let vertical = input.pressed(KeyCode::W) as i8 - input.pressed(KeyCode::S) as i8; let step_mod = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8; scale.step += (scale.step / 10.0) * step_mod as f64; scale.verti += scale.step * vertical as f64; scale.horiz += scale.step * horizontal as f64; scale.zoom += scale.step * zoom as f64; if scale.zoom < 0.0 { // We can't go below 0 scale.zoom -= scale.step * zoom as f64; // slow down our step function scale.step /= 10.0; } } fn mandelbrot_render_system( materials: Res>, mut textures: ResMut>, scale: Res, mut query: Query<(&mut Mandelbrot, &Handle)>, ) { for (mut fractal, handle) in query.iter_mut() { if let Some(material) = materials.get(handle) { if let Some(texture_handle) = &material.texture { if let Some(texture) = textures.get_mut(texture_handle) { let z = scale.zoom; let h = scale.horiz; let v = scale.verti; let start = Instant::now(); texture.data = fractal.generate_scaled_image( ((-2.5 * z) + h, (1.0 * z) + h), ((-1.0 * z) - v, (1.0 * z) - v), ); let diff = Instant::now() - start; println!( "Frametime: {:?} Framerate: {:.2}", diff, 1.0 / diff.as_secs_f32() ); } } } } } fn startup( mut commands: Commands, mut textures: ResMut>, mut materials: ResMut>, ) { let img_size = Vec2::new(Mandelbrot::width() as f32, Mandelbrot::height() as f32); let img_buf = Mandelbrot::new().generate_image(); let texture_handle = textures.add(Texture::new( img_size, img_buf, TextureFormat::Rgba8UnormSrgb, )); commands .spawn(Camera2dComponents::default()) .spawn(SpriteComponents { material: materials.add(texture_handle.into()), ..Default::default() }) .with(Mandelbrot::new()); }