Skip to content

Instantly share code, notes, and snippets.

@marethyu
Last active September 2, 2021 04:09
Show Gist options
  • Select an option

  • Save marethyu/bb167ed6f2bf56fa0edb8345ecfded8f to your computer and use it in GitHub Desktop.

Select an option

Save marethyu/bb167ed6f2bf56fa0edb8345ecfded8f to your computer and use it in GitHub Desktop.
use std::process;
use std::thread;
use std::time::Duration;
use rand::Rng;
use sdl2::event::Event;
use sdl2::pixels::PixelFormatEnum;
const WIDTH: usize = 256;
const HEIGHT: usize = 240;
pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("NESTY", (WIDTH * 2) as u32, (HEIGHT * 2) as u32)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let texture_creator = canvas.texture_creator();
let mut texture = texture_creator
.create_texture_streaming(PixelFormatEnum::RGB24, WIDTH as u32, HEIGHT as u32)
.unwrap();
let mut pixels = [0 as u8; WIDTH * HEIGHT * 3];
let mut rng = rand::thread_rng();
let mut event_pump = sdl_context.event_pump().unwrap();
loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. } => process::exit(0),
_ => {}
}
}
for y in 0..HEIGHT {
for x in 0..WIDTH {
let offset = y * WIDTH * 3 + x * 3;
pixels[offset ] = rng.gen::<u8>();
pixels[offset + 1] = rng.gen::<u8>();
pixels[offset + 2] = rng.gen::<u8>();
}
}
texture.update(None, &pixels, WIDTH * 3).unwrap();
canvas.copy(&texture, None, None).unwrap();
canvas.present();
thread::sleep(Duration::from_millis(17));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment