Set up SDL so I can draw the image being rendered in a window

This commit is contained in:
Matthew Gordon 2019-10-21 01:40:58 -04:00
parent f7139444f5
commit bc4759dfaa
3 changed files with 117 additions and 0 deletions

10
.gitignore vendored
View File

@ -1,3 +1,5 @@
*~
# Generated by Cargo # Generated by Cargo
# will have compiled files and executables # will have compiled files and executables
/target/ /target/
@ -8,3 +10,11 @@ Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
#Added by cargo
#
#already existing elements are commented out
/target
#**/*.rs.bk

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "vanrijn"
version = "0.1.0"
authors = ["Matthew Gordon <matthew.scott.gordon@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.32"

97
src/main.rs Normal file
View File

@ -0,0 +1,97 @@
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::{Color, PixelFormatEnum};
use sdl2::render::Texture;
use std::time::Duration;
struct OutputImage {
pixel_data: Vec<u8>,
width: usize,
height: usize,
channels: usize,
}
impl OutputImage {
fn new(width: usize, height: usize) -> OutputImage {
OutputImage {
width: width,
height: height,
channels: 3,
pixel_data: vec![0; width * height * 3],
}
}
fn clear(&mut self) -> &mut OutputImage {
for byte in self.pixel_data.iter_mut() {
*byte = 0u8;
}
self
}
fn set_color(&mut self, row: usize, column: usize, red: u8, green: u8, blue: u8) {
let index = (row * self.width + column) * self.channels;
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
.update(None, self.pixel_data.as_slice(), self.width * self.channels)
.expect("Couldn't update texture.");
}
}
pub fn main() {
let image_width = 1200;
let image_height = 900;
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
.window("van Rijn", image_width as u32, image_height as u32)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let texture_creator = canvas.texture_creator();
let mut rendered_image_texture = texture_creator
.create_texture_streaming(
PixelFormatEnum::RGB24,
image_width as u32,
image_height as u32,
)
.unwrap();
let mut output_image = OutputImage::new(image_width, image_height);
output_image.clear();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0;
'running: loop {
i = (i + 1) % 255;
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
_ => {}
}
}
output_image.update_texture(&mut rendered_image_texture);
for row in 0..image_height {
for column in 0..image_width {
output_image.set_color(row, column, i, i, i);
}
}
canvas.copy(&rendered_image_texture, None, None);
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
}