use bezier::Bezier; use sdl2::event::Event; use sdl2::keyboard::Keycode; use gl::types::*; fn main() { let mut curve = Bezier::new(); let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); let gl_attr = video_subsystem.gl_attr(); gl_attr.set_context_profile(sdl2::video::GLProfile::Core); gl_attr.set_context_version(4, 1); let window_w: u32 = 800; let window_h: u32 = 600; let window = video_subsystem .window("bezier", window_w, window_h) .position_centered() .opengl() .build() .unwrap(); let _gl_context = window.gl_create_context().unwrap(); gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const std::os::raw::c_void); let vertices: [f32; 18] = [ 1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0, ]; let mut vbo: GLuint = 0; let mut vao: GLuint = 0; unsafe { gl::Viewport(0, 0, window_w as i32, window_h as i32); gl::GenVertexArrays(1, &mut vao); gl::BindVertexArray(vao); gl::GenBuffers(1, &mut vbo); gl::BindBuffer(gl::ARRAY_BUFFER, vbo); gl::BufferData( gl::ARRAY_BUFFER, (vertices.len() * std::mem::size_of::()) as GLsizeiptr, vertices.as_ptr() as *const GLvoid, gl::STATIC_DRAW, ); gl::VertexAttribPointer( 0, 3, gl::FLOAT, gl::FALSE, (3 * std::mem::size_of::()) as GLint, std::ptr::null(), ); gl::EnableVertexAttribArray(0); } let mut event_pump = sdl_context.event_pump().unwrap(); 'running: loop { let x = event_pump.mouse_state().x(); let y = event_pump.mouse_state().y(); let left_click = event_pump.mouse_state().left(); for event in event_pump.poll_iter() { match event { Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => break 'running, Event::KeyDown { keycode: Some(Keycode::A), .. } => { curve.push(x, window_h as i32 - y); curve.draw(); }, Event::MouseMotion { .. } => { if left_click { curve.grab_closer(x as f32, (window_h as i32 - y) as f32); curve.draw(); } } _ => {} } } unsafe { gl::Clear(gl::COLOR_BUFFER_BIT); gl::DrawArrays(gl::TRIANGLES, 0, 6); } window.gl_swap_window(); } }