use opengl_rust::*;

struct SdlThings {
	sdl_context: sdl2::Sdl,
	window: sdl2::video::Window,
	gl_ctx: sdl2::video::GLContext,
}

impl SdlThings {
	pub fn new () -> Self 
	{
		let sdl_context = sdl2::init ().unwrap ();
		
		let video_subsystem = sdl_context.video ().unwrap ();
		let window = video_subsystem.window ("Racing game.", 1280, 720)
			.position_centered ()
			.opengl ()
			.build ()
		.unwrap ();
		
		gl::load_with (|s| {
			video_subsystem.gl_get_proc_address (s) as *const _
		});
		
		// The only OpenGL function anyone uses, anyway
		
		assert! (gl::Clear::is_loaded ());
		
		let gl_ctx = window.gl_create_context ().unwrap ();
		
		Self {
			sdl_context,
			window,
			gl_ctx,
		}
	}
}

fn main () {
	use sdl2::event::Event;
	use sdl2::keyboard::{Keycode, Scancode};
	
	let siddle = SdlThings::new ();
	
	let mut time_step = timestep::TimeStep::new (60, 1000);
	
	let mut gl_state = gl_state::GlState::default ();
	
	let mut event_pump = siddle.sdl_context.event_pump ().unwrap ();
	'running: loop {
		let frames_to_do = time_step.step ();
		
		for event in event_pump.poll_iter () {
			match event {
				Event::Quit {..} |
				Event::KeyDown { keycode: Some (Keycode::Escape), .. } => {
					break 'running
				},
				_ => (),
			}
		}
		
		siddle.window.gl_make_current (&siddle.gl_ctx).unwrap ();
		
		// Good graphics go here
		
		siddle.window.gl_swap_window ();
		
		std::thread::sleep (std::time::Duration::from_millis (15));
	}
}
