Move OutputImage into it's own module
This commit is contained in:
parent
eaf9ba7b2f
commit
c059aebe11
|
|
@ -0,0 +1,48 @@
|
||||||
|
pub struct OutputImage {
|
||||||
|
pixel_data: Vec<u8>,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
channels: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputImage {
|
||||||
|
pub fn new(width: u32, height: u32) -> OutputImage {
|
||||||
|
OutputImage {
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
channels: 3,
|
||||||
|
pixel_data: vec![0; (width * height * 3) as usize],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) -> &mut OutputImage {
|
||||||
|
for byte in self.pixel_data.iter_mut() {
|
||||||
|
*byte = 0u8;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_color(&mut self, row: u32, column: u32, red: u8, green: u8, blue: u8) {
|
||||||
|
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 + 1] = green;
|
||||||
|
self.pixel_data[index + 2] = blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pixel_data(&self) -> &Vec<u8> {
|
||||||
|
&self.pixel_data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_width(&self) -> u32 {
|
||||||
|
self.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_height(&self) -> u32 {
|
||||||
|
self.height
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_num_channels(&self) -> u32 {
|
||||||
|
self.channels
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
pub mod raycasting;
|
pub mod raycasting;
|
||||||
|
pub mod image;
|
||||||
|
|
|
||||||
41
src/main.rs
41
src/main.rs
|
|
@ -5,47 +5,16 @@ use sdl2::render::{Canvas, Texture};
|
||||||
use sdl2::Sdl;
|
use sdl2::Sdl;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
struct OutputImage {
|
use vanrijn::image::OutputImage;
|
||||||
pixel_data: Vec<u8>,
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
channels: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutputImage {
|
fn update_texture(image: &OutputImage, texture: &mut Texture) {
|
||||||
fn new(width: u32, height: u32) -> OutputImage {
|
|
||||||
OutputImage {
|
|
||||||
width: width,
|
|
||||||
height: height,
|
|
||||||
channels: 3,
|
|
||||||
pixel_data: vec![0; (width * height * 3) as usize],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clear(&mut self) -> &mut OutputImage {
|
|
||||||
for byte in self.pixel_data.iter_mut() {
|
|
||||||
*byte = 0u8;
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_color(&mut self, row: u32, column: u32, red: u8, green: u8, blue: u8) {
|
|
||||||
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 + 1] = green;
|
|
||||||
self.pixel_data[index + 2] = blue;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_texture(&self, texture: &mut Texture) {
|
|
||||||
texture
|
texture
|
||||||
.update(
|
.update(
|
||||||
None,
|
None,
|
||||||
self.pixel_data.as_slice(),
|
image.get_pixel_data().as_slice(),
|
||||||
(self.width * self.channels) as usize,
|
(image.get_width() * image.get_num_channels()) as usize,
|
||||||
)
|
)
|
||||||
.expect("Couldn't update texture.");
|
.expect("Couldn't update texture.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_canvas(
|
fn init_canvas(
|
||||||
|
|
@ -95,7 +64,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output_image.update_texture(&mut rendered_image_texture);
|
update_texture(&output_image, &mut rendered_image_texture);
|
||||||
for row in 0..image_height {
|
for row in 0..image_height {
|
||||||
for column in 0..image_width {
|
for column in 0..image_width {
|
||||||
output_image.set_color(row, column, i, i, i);
|
output_image.set_color(row, column, i, i, i);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue