Did some refactoring and improved error handling

This commit is contained in:
Matthew Gordon 2019-10-23 21:05:17 -04:00
parent bc4759dfaa
commit c81eec9174
1 changed files with 42 additions and 29 deletions

View File

@ -1,23 +1,24 @@
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::{Color, PixelFormatEnum}; use sdl2::pixels::PixelFormatEnum;
use sdl2::render::Texture; use sdl2::render::{Canvas, Texture};
use sdl2::Sdl;
use std::time::Duration; use std::time::Duration;
struct OutputImage { struct OutputImage {
pixel_data: Vec<u8>, pixel_data: Vec<u8>,
width: usize, width: u32,
height: usize, height: u32,
channels: usize, channels: u32,
} }
impl OutputImage { impl OutputImage {
fn new(width: usize, height: usize) -> OutputImage { fn new(width: u32, height: u32) -> OutputImage {
OutputImage { OutputImage {
width: width, width: width,
height: height, height: height,
channels: 3, channels: 3,
pixel_data: vec![0; width * height * 3], pixel_data: vec![0; (width * height * 3) as usize],
} }
} }
@ -28,8 +29,9 @@ impl OutputImage {
self self
} }
fn set_color(&mut self, row: usize, column: usize, red: u8, green: u8, blue: u8) { fn set_color(&mut self, row: u32, column: u32, red: u8, green: u8, blue: u8) {
let index = (row * self.width + column) * self.channels; assert!(row < self.height && column < self.width);
let index = ((row * self.width + column) * self.channels) as usize;
self.pixel_data[index] = red; self.pixel_data[index] = red;
self.pixel_data[index + 1] = green; self.pixel_data[index + 1] = green;
self.pixel_data[index + 2] = blue; self.pixel_data[index + 2] = blue;
@ -37,38 +39,48 @@ impl OutputImage {
fn update_texture(&self, texture: &mut Texture) { fn update_texture(&self, texture: &mut Texture) {
texture texture
.update(None, self.pixel_data.as_slice(), self.width * self.channels) .update(
None,
self.pixel_data.as_slice(),
(self.width * self.channels) as usize,
)
.expect("Couldn't update texture."); .expect("Couldn't update texture.");
} }
} }
pub fn main() { fn init_canvas(
let image_width = 1200; image_width: u32,
let image_height = 900; image_height: u32,
) -> Result<(Sdl, Canvas<sdl2::video::Window>), Box<dyn std::error::Error>> {
let sdl_context = sdl2::init().unwrap(); let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video().unwrap(); let video_subsystem = sdl_context.video()?;
let window = video_subsystem let window = video_subsystem
.window("van Rijn", image_width as u32, image_height as u32) .window("van Rijn", image_width as u32, image_height as u32)
.position_centered() .position_centered()
.build() .build()?;
.unwrap();
let mut canvas = window.into_canvas().build().unwrap(); let canvas = window.into_canvas().build().unwrap();
Ok((sdl_context, canvas))
}
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let image_width = 1200;
let image_height = 900;
let (sdl_context, mut canvas) = init_canvas(image_width, image_height)?;
let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let mut rendered_image_texture = texture_creator let mut rendered_image_texture = texture_creator.create_texture_streaming(
.create_texture_streaming(
PixelFormatEnum::RGB24, PixelFormatEnum::RGB24,
image_width as u32, image_width as u32,
image_height as u32, image_height as u32,
) )?;
.unwrap();
let mut output_image = OutputImage::new(image_width, image_height); let mut output_image = OutputImage::new(image_width, image_height);
output_image.clear(); output_image.clear();
let mut event_pump = sdl_context.event_pump().unwrap(); let mut event_pump = sdl_context.event_pump()?;
let mut i = 0; let mut i = 0;
'running: loop { 'running: loop {
i = (i + 1) % 255; i = (i + 1) % 255;
@ -90,8 +102,9 @@ pub fn main() {
} }
} }
canvas.copy(&rendered_image_texture, None, None); canvas.copy(&rendered_image_texture, None, None)?;
canvas.present(); canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
} }
Ok(())
} }