diff options
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | changelog.md | 9 | ||||
-rw-r--r-- | source/benoit/benoit/application.rs | 6 | ||||
-rw-r--r-- | source/benoit/benoit/application/initialise.rs | 5 | ||||
-rw-r--r-- | source/benoit/benoit/application/poll_events.rs | 88 | ||||
-rw-r--r-- | source/benoit/benoit/application/render.rs | 20 | ||||
-rw-r--r-- | source/benoit/benoit/application/run.rs | 15 |
7 files changed, 125 insertions, 20 deletions
@@ -1,6 +1,6 @@ [package] name = "benoit" -version = "0.12.0" +version = "0.13.0" authors = ["Gabriel Bjørnager Jensen"] edition = "2021" description = "Mandelbrot renderer." diff --git a/changelog.md b/changelog.md index 918f095..5735e25 100644 --- a/changelog.md +++ b/changelog.md @@ -1,9 +1,18 @@ +# 11 + +* Remove old makefile +* Optimise renderer +* Modulise code +* Check keyboard input (allow viewpoint movement) +* Update colouring + # 10 * Rewrite in Rust again * Update gitignore * Update readme * Update changelog format +* Use git tagging for versioning # ↋ diff --git a/source/benoit/benoit/application.rs b/source/benoit/benoit/application.rs index b9f1645..6f85bef 100644 --- a/source/benoit/benoit/application.rs +++ b/source/benoit/benoit/application.rs @@ -27,6 +27,7 @@ use sdl2::{Sdl, VideoSubsystem}; use sdl2::render::WindowCanvas; pub mod initialise; +pub mod poll_events; pub mod render; pub mod run; @@ -38,5 +39,10 @@ pub struct Application { canvas_width: u32, canvas_height: u32, + position_x: f64, + position_y: f64, + zoom: f64, max_iteration_count: u32, + + do_render: bool, } diff --git a/source/benoit/benoit/application/initialise.rs b/source/benoit/benoit/application/initialise.rs index 724b8d1..beddf54 100644 --- a/source/benoit/benoit/application/initialise.rs +++ b/source/benoit/benoit/application/initialise.rs @@ -43,7 +43,12 @@ impl Application { canvas_width: canvas_width, canvas_height: canvas_height, + position_x: 0.0, + position_y: 0.0, + zoom: 1.0, max_iteration_count: 0xFF, + + do_render: true, }; } } diff --git a/source/benoit/benoit/application/poll_events.rs b/source/benoit/benoit/application/poll_events.rs new file mode 100644 index 0000000..3936f60 --- /dev/null +++ b/source/benoit/benoit/application/poll_events.rs @@ -0,0 +1,88 @@ +/* + Copyright 2021, 2023 Gabriel Bjørnager Jensen. + + This file is part of Benoit. + + Benoit is free software: you can redistribute it + and/or modify it under the terms of the GNU + Affero General Public License as published by + the Free Software Foundation, either version 3 + of the License, or (at your option) any later + version. + + Benoit is distributed in the hope that it will + be useful, but WITHOUT ANY WARRANTY; without + even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU + Affero General Public License along with Benoit. + If not, see <https://www.gnu.org/licenses/>. +*/ + +use crate::benoit::application::Application; + +extern crate sdl2; + +use sdl2::EventPump; +use sdl2::event::Event; +use sdl2::keyboard::Scancode; + +impl Application { + pub fn poll_events(&mut self, event_pump: &mut EventPump) -> bool { + for event in event_pump.poll_iter() { + match event { + Event::KeyDown { + timestamp: _, + window_id: _, + keycode: _, + scancode, + keymod: _, + repeat: _, + } => { + let scancode = scancode.unwrap(); + + match scancode { + Scancode::Escape => return true, + _ => {}, + } + + self.zoom = match scancode { + Scancode::E => self.zoom * 2.0, + Scancode::Q => self.zoom / 2.0, + _ => self.zoom, + }; + + let translate_ammount: f64 = 1.0 / 16.0 / self.zoom; + + self.position_x += match scancode { + Scancode::A => -translate_ammount, + Scancode::D => translate_ammount, + _ => 0.0, + }; + + self.position_y += match scancode { + Scancode::S => translate_ammount, + Scancode::W => -translate_ammount, + _ => 0.0, + }; + + self.do_render = match scancode { + Scancode::A => true, + Scancode::D => true, + Scancode::E => true, + Scancode::Q => true, + Scancode::S => true, + Scancode::W => true, + _ => false, + }; + }, + Event::Quit { .. } => return true, + _ => {}, + } + } + + return false; + } +} diff --git a/source/benoit/benoit/application/render.rs b/source/benoit/benoit/application/render.rs index 6b0dc2f..5334d26 100644 --- a/source/benoit/benoit/application/render.rs +++ b/source/benoit/benoit/application/render.rs @@ -30,7 +30,7 @@ use sdl2::rect::Rect; impl Application { pub fn render(&mut self) { - println!("rendering"); + eprintln!("rendering: {} + {}i @ {}", self.position_x, self.position_y, self.zoom); //let mut data: [i8; 0x100] = [0; 0x100]; @@ -41,16 +41,16 @@ impl Application { 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); - let cb = (y as f64 - canvas_height / 2.0) / (canvas_height / 4.0); + 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 = 0.0f64; - let mut zb = 0.0f64; + let mut za: f64 = 0.0; + let mut zb: f64 = 0.0; let mut iteration_count = 0x0u32; while iteration_count < self.max_iteration_count { - let distance = (za.powf(2.0) + zb.powf(2.0)).sqrt(); - if distance > 2.0 { break } + let square_distance = (za * za + zb * zb).sqrt(); + if square_distance > 2.0 * 2.0 { break } // z = z^2 + c { @@ -58,14 +58,14 @@ impl Application { // a = a^2 - b^2 // b = 2abi let za_temporary = za; - za = za.powf(2.0) - zb.powf(2.0) + ca; + za = za * za - zb * zb + ca; zb = za_temporary * zb * 2.0 + cb; } iteration_count += 0x1; } - let value = (iteration_count / self.max_iteration_count * 0xFF) as u8; + 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); @@ -81,6 +81,6 @@ impl Application { self.canvas.present(); - println!("done"); + eprintln!("done"); } } diff --git a/source/benoit/benoit/application/run.rs b/source/benoit/benoit/application/run.rs index 2505e7e..a0f9c27 100644 --- a/source/benoit/benoit/application/run.rs +++ b/source/benoit/benoit/application/run.rs @@ -25,20 +25,17 @@ use crate::benoit::application::Application; extern crate sdl2; -use sdl2::event::Event; - impl Application { pub fn run(&mut self) { let mut event_pump = self.sdl.event_pump().expect("unable to get event pump"); - self.render(); + loop { + if self.poll_events(&mut event_pump) { break } + + if self.do_render { + self.render(); - 'main_loop: loop { - for event in event_pump.poll_iter() { - match event { - Event::Quit {..} => break 'main_loop, - _ => {}, - } + self.do_render = false; } } } |