diff --git a/Cargo.toml b/Cargo.toml index fb7a60e..801589f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,4 @@ edition = "2018" [dependencies] mat4 = "0.2.1" vec3 = "0.2.1" -arrayvec = "0.5.2" sdl2 = "0.34.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 311c2e8..47cc2aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,26 +3,60 @@ extern crate sdl2; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; +use sdl2::pixels::PixelFormatEnum; +use sdl2::surface::Surface; use std::time::Duration; -pub fn main() { + +fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) { + data[y * 3 * 800 + x * 3] = color.r; + data[y * 3 * 800 + x * 3 + 1] = color.g; + data[y * 3 * 800 + x * 3 + 2] = color.b; +} + +fn main() { let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); let window = video_subsystem .window("rust-sdl2 demo", 800, 600) .position_centered() + .opengl() .build() .unwrap(); + // let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap(); + let mut data = Box::new([255u8; 1_440_000]); + // println!("{:?}", data); let mut canvas = window.into_canvas().build().unwrap(); - canvas.set_draw_color(Color::RGB(0, 255, 255)); - canvas.clear(); - canvas.present(); + // let mut canvas = surface.into_canvas().unwrap(); + //canvas.set_draw_color(Color::RGB(0, 255, 255)); + // let surface = Surface::from_data(&mut data, 800, 600, 3, PixelFormatEnum::RGB24).unwrap(); + let texture_creator = canvas.texture_creator(); let mut event_pump = sdl_context.event_pump().unwrap(); let mut i = 0; 'running: loop { - i = (i + 1) % 255; - canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); - canvas.clear(); + put_pixel( + &mut *data, + i, + i, + Color { + r: 0, + g: 0, + b: 0, + a: 0, + }, + ); + if i < 599 { + i += 1; + } + // canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); + let surface = + Surface::from_data(&mut *data, 800, 600, 2400, PixelFormatEnum::RGB24).unwrap(); + let texture = texture_creator + .create_texture_from_surface(surface) + .unwrap(); + canvas.copy(&texture, None, None).unwrap(); + + canvas.present(); for event in event_pump.poll_iter() { match event { Event::Quit { .. } @@ -34,8 +68,6 @@ pub fn main() { } } // The rest of the game loop goes here... - - canvas.present(); - ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); + // ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); } } diff --git a/src/object.rs b/src/object.rs index 5dcd436..08b1491 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,7 +1,6 @@ use super::Ray; -use arrayvec::ArrayVec; pub trait Object { fn get_intersection(&self, ray: Ray) -> Option; - fn get_normal(&self) -> ArrayVec<[f64; 3]>; + fn get_normal(&self) -> [f64; 3]; } diff --git a/src/ray.rs b/src/ray.rs index 3283fa9..94a8304 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -1,6 +1,4 @@ -use arrayvec::ArrayVec; - pub struct Ray { - pub origin: ArrayVec<[f64; 3]>, - pub direction: ArrayVec<[f64; 3]>, + pub origin: [f64; 3], + pub direction: [f64; 3], } diff --git a/src/sphere.rs b/src/sphere.rs index bb04767..581a299 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -1,6 +1,6 @@ +extern crate vec3; use super::Object; use super::Ray; -use arrayvec::ArrayVec; struct Sphere {} @@ -31,7 +31,7 @@ impl Object for Sphere { None } } - fn get_normal(&self) -> ArrayVec<[f64; 3]> { - ArrayVec::<[f64; 3]>::new() + fn get_normal(&self) -> [f64; 3] { + vec3::new_zero() } }