mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
70 lines
1.9 KiB
Rust
70 lines
1.9 KiB
Rust
extern crate sdl2;
|
|
|
|
use sdl2::pixels::PixelFormatEnum;
|
|
use sdl2::rect::Rect;
|
|
use sdl2::event::Event;
|
|
use sdl2::keyboard::Keycode;
|
|
|
|
pub fn main() {
|
|
let sdl_context = sdl2::init().unwrap();
|
|
let video_subsystem = sdl_context.video().unwrap();
|
|
|
|
let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
|
|
.position_centered()
|
|
.opengl()
|
|
.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::IYUV, 256, 256).unwrap();
|
|
// Create a U-V gradient
|
|
texture.with_lock(None, |buffer: &mut [u8], pitch: usize| {
|
|
// `pitch` is the width of the Y component
|
|
// The U and V components are half the width and height of Y
|
|
|
|
let w = 256;
|
|
let h = 256;
|
|
|
|
// Set Y (constant)
|
|
for y in 0..h {
|
|
for x in 0..w {
|
|
let offset = y*pitch + x;
|
|
buffer[offset] = 128;
|
|
}
|
|
}
|
|
|
|
let y_size = pitch*h;
|
|
|
|
// Set U and V (X and Y)
|
|
for y in 0..h/2 {
|
|
for x in 0..w/2 {
|
|
let u_offset = y_size + y*pitch/2 + x;
|
|
let v_offset = y_size + (pitch/2 * h/2) + y*pitch/2 + x;
|
|
buffer[u_offset] = (x*2) as u8;
|
|
buffer[v_offset] = (y*2) as u8;
|
|
}
|
|
}
|
|
}).unwrap();
|
|
|
|
canvas.clear();
|
|
canvas.copy(&texture, None, Some(Rect::new(100, 100, 256, 256))).unwrap();
|
|
canvas.present();
|
|
|
|
let mut event_pump = sdl_context.event_pump().unwrap();
|
|
|
|
'running: loop {
|
|
for event in event_pump.poll_iter() {
|
|
match event {
|
|
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
|
|
break 'running
|
|
},
|
|
_ => {}
|
|
}
|
|
}
|
|
// The rest of the game loop goes here...
|
|
}
|
|
}
|