From 06d29763475c6d2cac6b24fee61c5a52bd08d51b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 11 May 2022 11:18:07 +0200 Subject: [PATCH] Switched to SDL2 --- mas/fractals/mandel/src/main.rs | 32 +++++++------------------------ mas/fractals/mandel/src/mandel.rs | 23 +++++++++++++++++----- mas/fractals/mandel/src/render.rs | 17 ++++++++++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 mas/fractals/mandel/src/render.rs diff --git a/mas/fractals/mandel/src/main.rs b/mas/fractals/mandel/src/main.rs index c52e9c4..83c589a 100644 --- a/mas/fractals/mandel/src/main.rs +++ b/mas/fractals/mandel/src/main.rs @@ -1,13 +1,9 @@ mod mandel; +mod render; use std::env; -use sfml::window::{Window, Event, Style}; -use sfml::graphics::{Color, Rect}; -use sfml::system::Vector2i; -fn render_pixel(x: u32, y: u32, col: Color) -> Rect { - return rect; -} +use sdl2::pixels::Color; fn main() { /* @@ -19,25 +15,11 @@ fn main() { let (width, height) = (640, 480); let (xr, yr) = ([-200, 200], [-200, 200]); - let mut win = Window::new((width, height), "Mandelbrot set", Style::CLOSE, &Default::default()); - win.set_framerate_limit(20); - win.set_position(Vector2i::new(0, 0)); - - let mut rect = Rect::new(200, 200, 1, 1); - rect.set_fill_color(Color::RED); - - while win.is_open() { - while let Some(ev) = win.poll_event() { - match ev { - Event::Closed => {win.close();}, - _ => {} - } - } - - rect.draw(); + let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height); + let mut can = window.into_canvas().unwrap(); - win.set_active(true); - win.display(); - } + can.set_draw_color(Color::RGB(0, 0, 0)); + can.clear(); + can.present(); } diff --git a/mas/fractals/mandel/src/mandel.rs b/mas/fractals/mandel/src/mandel.rs index 68adaab..e8c0758 100644 --- a/mas/fractals/mandel/src/mandel.rs +++ b/mas/fractals/mandel/src/mandel.rs @@ -1,8 +1,21 @@ use num::complex::Complex; use matrix::Matrix; -use sfml::system::Vector2u; -use sfml::window::Window; -use sfml::graphics::Color; +use sdl2::pixels::Color; + +/* +#[derive(Debug)] +pub struct Color { + r: u8, + g: u8, + b: u8 +} + +impl Color { + pub fn rgb(r: u8, g: u8, b: u8) -> Self { + Self { r, g, b } + } +} +*/ fn mandel(n: u64, c: Complex) -> Complex { if n == 0 { @@ -20,9 +33,9 @@ pub fn get_point_color(w: i32, h: i32, x: i32, y: i32, depth: u64) -> Color { let mut norm = mandel(depth, c).norm(); if norm < 2.0 { - return Color::rgb(0, 0, 0); + return Color::RGB(0, 0, 0); } else { - return Color::rgb(255, 255, 255); + return Color::RGB(255, 255, 255); } } diff --git a/mas/fractals/mandel/src/render.rs b/mas/fractals/mandel/src/render.rs new file mode 100644 index 0000000..52cc20e --- /dev/null +++ b/mas/fractals/mandel/src/render.rs @@ -0,0 +1,17 @@ +extern crate sdl2; + +use sdl2::pixels::Color; +use sdl2::event::Event; +use sdl2::keyboard::Keycode; +use std::time::Duration; + +pub fn create_window(title: &str, width: u32, height: u32) -> (sdl2::video::Window, sdl2::Sdl, sdl2::VideoSubsystem) { + let ctx = sdl2::init().unwrap(); + let vid_sys = ctx.video().unwrap(); + + let window = vid_sys.window(title, width, height) + .position_centered() + .build() + .unwrap(); + return (window, ctx, vid_sys); +}