Skip to content

Instantly share code, notes, and snippets.

@vojd
Last active October 3, 2023 18:47
Show Gist options
  • Save vojd/d7c4b2929c8b985826f471b965eb15c2 to your computer and use it in GitHub Desktop.
Save vojd/d7c4b2929c8b985826f471b965eb15c2 to your computer and use it in GitHub Desktop.
Simple OpenGL window in Rust
[dependencies]
gl = "*"
glutin = "0.26"
winit = "0.24"
extern crate gl;
extern crate glutin;
extern crate winit;
use glutin::ContextBuilder;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new();
let window_context = ContextBuilder::new()
.build_windowed(window, &event_loop)
.unwrap();
let context = unsafe { window_context.make_current().unwrap() };
gl::load_with(|s| context.get_proc_address(s) as *const _);
event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
println!("Bye now...");
*control_flow = ControlFlow::Exit
}
Event::RedrawRequested(_) => {
unsafe {
gl::ClearColor(0.2, 0.5, 0.2, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
context.swap_buffers().unwrap();
}
_ => (),
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment