diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/benoit/benoit.rs | 24 | ||||
-rw-r--r-- | source/benoit/benoit/application.rs | 42 | ||||
-rw-r--r-- | source/benoit/benoit/application/initialise.rs | 49 | ||||
-rw-r--r-- | source/benoit/benoit/application/render.rs | 86 | ||||
-rw-r--r-- | source/benoit/benoit/application/run.rs | 45 | ||||
-rw-r--r-- | source/benoit/main.rs | 31 |
6 files changed, 277 insertions, 0 deletions
diff --git a/source/benoit/benoit.rs b/source/benoit/benoit.rs new file mode 100644 index 0000000..5b4b9e2 --- /dev/null +++ b/source/benoit/benoit.rs @@ -0,0 +1,24 @@ +/* + 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/>. +*/ + +pub mod application; diff --git a/source/benoit/benoit/application.rs b/source/benoit/benoit/application.rs new file mode 100644 index 0000000..b9f1645 --- /dev/null +++ b/source/benoit/benoit/application.rs @@ -0,0 +1,42 @@ +/* + 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/>. +*/ + +extern crate sdl2; + +use sdl2::{Sdl, VideoSubsystem}; +use sdl2::render::WindowCanvas; + +pub mod initialise; +pub mod render; +pub mod run; + +pub struct Application { + sdl: Sdl, + sdl_video: VideoSubsystem, + canvas: WindowCanvas, + + canvas_width: u32, + canvas_height: u32, + + max_iteration_count: u32, +} diff --git a/source/benoit/benoit/application/initialise.rs b/source/benoit/benoit/application/initialise.rs new file mode 100644 index 0000000..724b8d1 --- /dev/null +++ b/source/benoit/benoit/application/initialise.rs @@ -0,0 +1,49 @@ +/* + 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; + +impl Application { + pub fn initialise() -> Application { + let canvas_width = 0x400; + let canvas_height = 0x400; + + let sdl = sdl2::init().expect("unable to initialise sdl2"); + let sdl_video = sdl.video().expect("unable to initialise video"); + + let window = sdl_video.window("Benoit", canvas_width, canvas_height).position_centered().build().expect("unable to open window"); + + let canvas = window.into_canvas().build().expect("unable to create canvas"); + + return Application { + sdl: sdl, + sdl_video: sdl_video, + canvas: canvas, + + canvas_width: canvas_width, + canvas_height: canvas_height, + + max_iteration_count: 0xFF, + }; + } +} diff --git a/source/benoit/benoit/application/render.rs b/source/benoit/benoit/application/render.rs new file mode 100644 index 0000000..6b0dc2f --- /dev/null +++ b/source/benoit/benoit/application/render.rs @@ -0,0 +1,86 @@ +/* + 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::pixels::Color; +use sdl2::rect::Rect; + +impl Application { + pub fn render(&mut self) { + println!("rendering"); + + //let mut data: [i8; 0x100] = [0; 0x100]; + + for y in 0x0..self.canvas_width { + for x in 0x0..self.canvas_height { + let pixel = y * 0xFF + x; + + 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 mut za = 0.0f64; + let mut zb = 0.0f64; + + 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 } + + // z = z^2 + c + { + // Complex square: + // a = a^2 - b^2 + // b = 2abi + let za_temporary = za; + za = za.powf(2.0) - zb.powf(2.0) + ca; + zb = za_temporary * zb * 2.0 + cb; + } + + iteration_count += 0x1; + } + + let value = (iteration_count / self.max_iteration_count * 0xFF) 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(); + } + } + + self.canvas.present(); + + println!("done"); + } +} diff --git a/source/benoit/benoit/application/run.rs b/source/benoit/benoit/application/run.rs new file mode 100644 index 0000000..2505e7e --- /dev/null +++ b/source/benoit/benoit/application/run.rs @@ -0,0 +1,45 @@ +/* + 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::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(); + + 'main_loop: loop { + for event in event_pump.poll_iter() { + match event { + Event::Quit {..} => break 'main_loop, + _ => {}, + } + } + } + } +} diff --git a/source/benoit/main.rs b/source/benoit/main.rs new file mode 100644 index 0000000..bffc05d --- /dev/null +++ b/source/benoit/main.rs @@ -0,0 +1,31 @@ +/* + 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/>. +*/ + +mod benoit; + +use benoit::application::Application; + +fn main() { + let mut application = Application::initialise(); + application.run(); +} |