diff --git a/ria-weekend/src/demo.rs b/ria-weekend/src/demo.rs index 7fed171..7641057 100644 --- a/ria-weekend/src/demo.rs +++ b/ria-weekend/src/demo.rs @@ -17,7 +17,7 @@ pub trait Demo { .expect("error in writing file header"); for i in buf.chunks(4) { - match file.write(&format!("{} {} {}\n", i[0], i[1], i[2]).as_bytes()) { + match file.write(format!("{} {} {}\n", i[0], i[1], i[2]).as_bytes()) { Ok(_) => (), Err(e) => panic!("couldn't write to {}: {}", self.name(), e.description()), } diff --git a/ria-weekend/src/linear_interpolation_y.rs b/ria-weekend/src/demos/linear_interpolation_y.rs similarity index 95% rename from ria-weekend/src/linear_interpolation_y.rs rename to ria-weekend/src/demos/linear_interpolation_y.rs index fb20378..6b1ea62 100644 --- a/ria-weekend/src/linear_interpolation_y.rs +++ b/ria-weekend/src/demos/linear_interpolation_y.rs @@ -16,7 +16,7 @@ impl crate::Demo for LinearInterpolationY { let origin = Vec3::new(0.0, 0.0, 0.0); let mut offset = 0; - for j in (0..h) { + for j in 0..h { for i in 0..w { // relative offsets // current position to total width/length @@ -32,7 +32,6 @@ impl crate::Demo for LinearInterpolationY { buf[offset] = ir; buf[offset + 1] = ig; buf[offset + 2] = ib; - buf[offset + 3] = 0; offset += 4; } } diff --git a/ria-weekend/src/demos/mod.rs b/ria-weekend/src/demos/mod.rs new file mode 100644 index 0000000..587d108 --- /dev/null +++ b/ria-weekend/src/demos/mod.rs @@ -0,0 +1,7 @@ +mod linear_interpolation_y; +mod simple_sphere; +mod ppm_example; + +pub use linear_interpolation_y::LinearInterpolationY; +pub use simple_sphere::SimpleSphere; +pub use ppm_example::PpmExample; diff --git a/ria-weekend/src/ppm_example.rs b/ria-weekend/src/demos/ppm_example.rs similarity index 91% rename from ria-weekend/src/ppm_example.rs rename to ria-weekend/src/demos/ppm_example.rs index c1206f3..dc750c5 100644 --- a/ria-weekend/src/ppm_example.rs +++ b/ria-weekend/src/demos/ppm_example.rs @@ -8,7 +8,7 @@ impl crate::Demo for PpmExample { fn render(&self, buf: &mut Vec, w: usize, h: usize) { let mut offset = 0; - for j in (0..h) { + for j in 0..h { for i in 0..w { let color = Vec3::new((i as f32) / (w as f32), (j as f32) / (h as f32), 0.2); @@ -19,7 +19,6 @@ impl crate::Demo for PpmExample { buf[offset] = ir; buf[offset + 1] = ig; buf[offset + 2] = ib; - buf[offset + 3] = 255; offset += 4; } diff --git a/ria-weekend/src/simple_sphere.rs b/ria-weekend/src/demos/simple_sphere.rs similarity index 98% rename from ria-weekend/src/simple_sphere.rs rename to ria-weekend/src/demos/simple_sphere.rs index b1a9f01..7a7e6d2 100644 --- a/ria-weekend/src/simple_sphere.rs +++ b/ria-weekend/src/demos/simple_sphere.rs @@ -31,7 +31,6 @@ impl crate::Demo for SimpleSphere { buf[offset] = ir; buf[offset + 1] = ig; buf[offset + 2] = ib; - buf[offset + 3] = 0; offset += 4; } } diff --git a/ria-weekend/src/main.rs b/ria-weekend/src/main.rs index 431e6b6..e378d1d 100644 --- a/ria-weekend/src/main.rs +++ b/ria-weekend/src/main.rs @@ -1,13 +1,10 @@ mod demo; -mod linear_interpolation_y; -mod ppm_example; +mod demos; mod ray; -mod simple_sphere; mod vec3; use demo::Demo; -use linear_interpolation_y::LinearInterpolationY; -use ppm_example::PpmExample; +use demos::{LinearInterpolationY, PpmExample, SimpleSphere}; use sdl2::{ event::{Event, WindowEvent}, keyboard::Keycode, @@ -17,8 +14,6 @@ use sdl2::{ video::Window, EventPump, Sdl, }; -use simple_sphere::SimpleSphere; -use vec3::Vec3; fn main() -> Result<(), String> { let sdl_ctx = sdl2::init()?; @@ -45,11 +40,13 @@ fn main() -> Result<(), String> { let texture_creator = canvas.texture_creator(); let mut texture = texture_creator - .create_texture_static(PixelFormatEnum::RGB888, width as u32, height as u32) + .create_texture_static(PixelFormatEnum::BGR888, width as u32, height as u32) .map_err(|e| e.to_string())?; let mut active_demo: Box = Box::new(LinearInterpolationY); + //println!("{:?} {:?} {:?}", texture.query(), texture.color_mod(), texture.alpha_mod()); + loop { for event in event_pump.poll_iter() { match event { @@ -61,27 +58,19 @@ fn main() -> Result<(), String> { Event::KeyUp { keycode: Some(Keycode::Num1), .. - } => { - active_demo = Box::new(PpmExample); - } + } => active_demo = Box::new(PpmExample), Event::KeyUp { keycode: Some(Keycode::Num2), .. - } => { - active_demo = Box::new(LinearInterpolationY); - } + } => active_demo = Box::new(LinearInterpolationY), Event::KeyUp { keycode: Some(Keycode::Num3), .. - } => { - active_demo = Box::new(SimpleSphere); - } + } => active_demo = Box::new(SimpleSphere), Event::KeyUp { keycode: Some(Keycode::S), .. - } => { - active_demo.save_as_ppm(&buffer, width, height); - } + } => active_demo.save_as_ppm(&buffer, width, height), Event::Window { win_event: WindowEvent::Resized(w, h), .. @@ -90,7 +79,7 @@ fn main() -> Result<(), String> { height = h as usize; buffer.resize(width * height * 4, 0); texture = texture_creator - .create_texture_static(PixelFormatEnum::RGB888, width as u32, height as u32) + .create_texture_static(PixelFormatEnum::BGR888, width as u32, height as u32) .expect("error in resizing texture"); } _ => {}