main.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // load the relevant glfw things
  2. use glfw::{Action, Context, Key};
  3. // load the generated OpenGL bindings in a local module
  4. mod gl {
  5. include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
  6. }
  7. fn main() {
  8. // set up the GLFW context
  9. let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
  10. // create the GLFW window
  11. let (mut window, events) = glfw
  12. .create_window(500, 500, "GL example", glfw::WindowMode::Windowed)
  13. .expect("Failed to create GLFW window.");
  14. // let it poll for keyboard events
  15. window.set_key_polling(true);
  16. window.make_current();
  17. // initialize the OpenGL context with the window pointer
  18. gl::load_with(|s| window.get_proc_address(s) as *const _);
  19. // continuously poll for events and then redraw the window
  20. while !window.should_close() {
  21. glfw.poll_events();
  22. for (_, event) in glfw::flush_messages(&events) {
  23. handle_window_event(&mut window, event);
  24. }
  25. draw(&mut window);
  26. }
  27. }
  28. // our immediate mode OpenGL draw function
  29. fn draw(window: &mut glfw::Window) {
  30. unsafe {
  31. gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
  32. gl::LoadIdentity();
  33. gl::Begin(gl::TRIANGLES);
  34. {
  35. gl::Color3f(1.0, 0.0, 0.0);
  36. gl::Vertex2f(0.0, 1.0);
  37. gl::Color3f(0.0, 1.0, 0.0);
  38. gl::Vertex2f(0.87, -0.5);
  39. gl::Color3f(0.0, 0.0, 1.0);
  40. gl::Vertex2f(-0.87, -0.5);
  41. }
  42. gl::End();
  43. }
  44. // and swap the buffer to make it visible
  45. window.swap_buffers();
  46. }
  47. // our "event handler" just listens for the close event
  48. fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
  49. match event {
  50. glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => window.set_should_close(true),
  51. _ => {}
  52. }
  53. }