extern crate sdl2;

use sdl2::pixels::PixelFormatEnum;
use sdl2::rect::Rect;
use sdl2::keyboard::Keycode;

pub fn test() {
	let mut sdl_context = sdl2::init().video().unwrap();

	let window = sdl_context.window("rust-sdl2 demo: Video", 800, 600)
		.position_centered()
		.opengl()
		.build()
		.unwrap();

	let mut renderer = window.renderer().build().unwrap();

	let mut texture = renderer.create_texture_streaming(PixelFormatEnum::RGB24, (256, 256)).unwrap();

	texture.with_lock(None, |buffer: &mut [u8], pitch: usize| {
		for y in (0..256) {
			for x in (0..256) {
				let offset = y*pitch + x*3;
				buffer[offset + 0] = x as u8;
				buffer[offset + 1] = y as u8;
				buffer[offset + 2] = 0;
			}
		}
	}).unwrap();

	renderer.clear();
	renderer.copy(&texture, None, Some(Rect::new_unwrap(100, 100, 256, 256)));
	renderer.copy_ex(&texture, None, Some(Rect::new_unwrap(450, 100, 256, 256)), 30.0, None, (false, false));
	renderer.present();

	let mut running = true;

	while running {
		for event in sdl_context.event_pump().poll_iter() {
			use sdl2::event::Event;

			match event {
				Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
					running = false
				},
				_ => {}
			}
		}
	}
}
