diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/benoit/benoit/application/initialise.rs | 6 | ||||
-rw-r--r-- | source/benoit/benoit/application/poll_events.rs | 8 | ||||
-rw-r--r-- | source/benoit/benoit/application/render.rs | 57 |
3 files changed, 47 insertions, 24 deletions
diff --git a/source/benoit/benoit/application/initialise.rs b/source/benoit/benoit/application/initialise.rs index beddf54..012e1b0 100644 --- a/source/benoit/benoit/application/initialise.rs +++ b/source/benoit/benoit/application/initialise.rs @@ -25,8 +25,8 @@ use crate::benoit::application::Application; impl Application { pub fn initialise() -> Application { - let canvas_width = 0x400; - let canvas_height = 0x400; + let canvas_width = 0x200; + let canvas_height = 0x200; let sdl = sdl2::init().expect("unable to initialise sdl2"); let sdl_video = sdl.video().expect("unable to initialise video"); @@ -46,7 +46,7 @@ impl Application { position_x: 0.0, position_y: 0.0, zoom: 1.0, - max_iteration_count: 0xFF, + max_iteration_count: 0x100, do_render: true, }; diff --git a/source/benoit/benoit/application/poll_events.rs b/source/benoit/benoit/application/poll_events.rs index 3936f60..5057054 100644 --- a/source/benoit/benoit/application/poll_events.rs +++ b/source/benoit/benoit/application/poll_events.rs @@ -68,11 +68,19 @@ impl Application { _ => 0.0, }; + self.max_iteration_count = match scancode { + Scancode::F => self.max_iteration_count * 0x2, + Scancode::R => self.max_iteration_count / 0x2, + _ => self.max_iteration_count, + }; + self.do_render = match scancode { Scancode::A => true, Scancode::D => true, Scancode::E => true, + Scancode::F => true, Scancode::Q => true, + Scancode::R => true, Scancode::S => true, Scancode::W => true, _ => false, diff --git a/source/benoit/benoit/application/render.rs b/source/benoit/benoit/application/render.rs index 5334d26..eca2fe6 100644 --- a/source/benoit/benoit/application/render.rs +++ b/source/benoit/benoit/application/render.rs @@ -26,37 +26,42 @@ use crate::benoit::application::Application; extern crate sdl2; use sdl2::pixels::Color; -use sdl2::rect::Rect; +use sdl2::rect::Point; +use std::time::Instant; impl Application { pub fn render(&mut self) { eprintln!("rendering: {} + {}i @ {}", self.position_x, self.position_y, self.zoom); - //let mut data: [i8; 0x100] = [0; 0x100]; + let canvas_size = self.canvas_height* self.canvas_width; - for y in 0x0..self.canvas_width { - for x in 0x0..self.canvas_height { - let pixel = y * 0xFF + x; + let mut data: Vec<u32> = Vec::with_capacity(self.canvas_height as usize * self.canvas_width as usize); + let time_start = Instant::now(); + + for y in 0x0..self.canvas_height { + for x in 0x0..self.canvas_width { let canvas_width = self.canvas_width as f64; let canvas_height = self.canvas_height as f64; - let ca = (x as f64 - canvas_width / 2.0) / (canvas_width / 4.0 * self.zoom) + self.position_x; - let cb = (y as f64 - canvas_height / 2.0) / (canvas_height / 4.0 * self.zoom) + self.position_y; + let ca = (x as f64 - canvas_width / 2.0) / canvas_width * 4.0 / self.zoom + self.position_x; + let cb = (y as f64 - canvas_height / 2.0) / canvas_height * 4.0 / self.zoom + self.position_y; let mut za: f64 = 0.0; let mut zb: f64 = 0.0; - let mut iteration_count = 0x0u32; + let mut iteration_count: u32 = 0x0; while iteration_count < self.max_iteration_count { let square_distance = (za * za + zb * zb).sqrt(); if square_distance > 2.0 * 2.0 { break } - // z = z^2 + c { + // z = z^2 + c + // Complex square: // a = a^2 - b^2 // b = 2abi + let za_temporary = za; za = za * za - zb * zb + ca; zb = za_temporary * zb * 2.0 + cb; @@ -65,22 +70,32 @@ impl Application { iteration_count += 0x1; } - let value = (iteration_count as f32 / self.max_iteration_count as f32 * 255.0).round() as u8; - let colour = Color::RGB(value, value, value); - self.canvas.set_draw_color(colour); - - let square = Rect::new( - x as i32, - y as i32, - 0x1, - 0x1, - ); - self.canvas.fill_rects(&[square]).unwrap(); + data.push(iteration_count); } } + let duration = time_start.elapsed(); + + for pixel in 0x0..canvas_size { + let y = pixel / self.canvas_width; + let x = pixel - y * self.canvas_width; + + let iteration_count = data[pixel as usize]; + + let value: u8 = if iteration_count != self.max_iteration_count { + (iteration_count as f32 / 64.0 % 1.0 * 255.0).round() as u8 + } else { + 0x0 + }; + let colour = Color::RGB(value, value, value); + self.canvas.set_draw_color(colour); + + let point = Point::new(x as i32, y as i32); + self.canvas.draw_point(point).unwrap(); + } + self.canvas.present(); - eprintln!("done"); + eprintln!("done ({}ms)", duration.as_millis()); } } |