123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- // load the relevant glfw things
- use glfw::{Action, Context, Key};
- // load the generated OpenGL bindings in a local module
- mod gl {
- include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
- }
- fn main() {
- // set up the GLFW context
- let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
- // create the GLFW window
- let (mut window, events) = glfw
- .create_window(500, 500, "GL example", glfw::WindowMode::Windowed)
- .expect("Failed to create GLFW window.");
- // let it poll for keyboard events
- window.set_key_polling(true);
- window.make_current();
- // initialize the OpenGL context with the window pointer
- gl::load_with(|s| window.get_proc_address(s) as *const _);
- // continuously poll for events and then redraw the window
- while !window.should_close() {
- glfw.poll_events();
- for (_, event) in glfw::flush_messages(&events) {
- handle_window_event(&mut window, event);
- }
- draw(&mut window);
- }
- }
- // our immediate mode OpenGL draw function
- fn draw(window: &mut glfw::Window) {
- unsafe {
- gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
- gl::LoadIdentity();
- gl::Begin(gl::TRIANGLES);
- {
- gl::Color3f(1.0, 0.0, 0.0);
- gl::Vertex2f(0.0, 1.0);
- gl::Color3f(0.0, 1.0, 0.0);
- gl::Vertex2f(0.87, -0.5);
- gl::Color3f(0.0, 0.0, 1.0);
- gl::Vertex2f(-0.87, -0.5);
- }
- gl::End();
- }
- // and swap the buffer to make it visible
- window.swap_buffers();
- }
- // our "event handler" just listens for the close event
- fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
- match event {
- glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => window.set_should_close(true),
- _ => {}
- }
- }
|