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() {
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_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(user_input_system.system())
.run();
}
struct PerformanceInfo;
struct TextureOptions {
zoom: f64,
horizontal: f64,
vertical: f64,
step: f64,
iteration_limit: u32,
}
impl Default for TextureOptions {
fn default() -> Self {
Self {
zoom: 1.0,
horizontal: 0.0,
vertical: 0.0,
step: 0.01,
iteration_limit: 64,
}
}
}
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
// R to increase the steps taken by zoom, horizontal movement, and vertical movement
// T to increase the Iteration Limit, G to decrease the iteration limit
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_change = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8;
let limit_change = input.just_pressed(KeyCode::T) as i8 - input.just_pressed(KeyCode::G) as i8;
scale.step += (scale.step / 10.0) * step_change as f64;
scale.vertical += scale.step * vertical as f64;
scale.horizontal += scale.step * horizontal as f64;
scale.zoom += scale.step * zoom as f64;
if limit_change == 1 {
scale.iteration_limit *= 2;
if scale.iteration_limit > MAX_ITERATION_COUNT {
scale.iteration_limit = MAX_ITERATION_COUNT;
}
} else if limit_change == -1 {
scale.iteration_limit /= 2;
if scale.iteration_limit < 32 {
scale.iteration_limit = 32;
}
}
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 performance_info_system(
// time: Res