Skip to content

Instantly share code, notes, and snippets.

@csauve
Created January 20, 2019 09:12
Show Gist options
  • Save csauve/33e7e541fdf39d9a99fb3b0a36d809c8 to your computer and use it in GitHub Desktop.
Save csauve/33e7e541fdf39d9a99fb3b0a36d809c8 to your computer and use it in GitHub Desktop.
// error[E0502]: cannot borrow `window` as mutable because it is also borrowed as immutable
// --> src/main.rs:14:9
// |
// 14 | window.poll(|event| {
// | ^ ---- ------- immutable borrow occurs here
// | | |
// | _________| immutable borrow used by call, in later iteration of loop
// | |
// 15 | | match event {
// 16 | | WindowEvent::Close => {
// 17 | | running = false;
// ... |
// 20 | | window.resize(dim);
// | | ------ first borrow occurs due to use of `window` in closure
// ... |
// 23 | | }
// 24 | | });
// | |__________^ mutable borrow occurs here
mod window;
use window::{Window, WindowEvent, Dimensions};
fn main() {
let mut window = Window::new(
"redrock",
Dimensions {width: 1024.0, height: 480.0}
);
window.make_current();
let mut running = true;
while running {
window.poll(|event| {
match event {
WindowEvent::Close => {
running = false;
},
WindowEvent::Resize(dim) => {
window.resize(dim);
},
_ => ()
}
});
window.clear();
//draw
window.swap();
}
}
//--------------------------------------------
// window.rs:
//--------------------------------------------
use gl;
use glutin::dpi::*;
use glutin::GlContext;
pub struct Window {
gl_window: glutin::GlWindow,
events_loop: glutin::EventsLoop,
}
pub struct Dimensions {
pub width: f64,
pub height: f64,
}
pub enum WindowEvent {
Close,
Resize(Dimensions),
Key(bool, u32),
}
impl Window {
pub fn new(title: &str, size: Dimensions) -> Window {
let events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new()
.with_title(title)
.with_dimensions(LogicalSize::new(size.width, size.height));
let context = glutin::ContextBuilder::new()
.with_vsync(true);
let gl_window = glutin::GlWindow::new(window, context, &events_loop)
.expect("Failed to create GL window");
Window {
gl_window,
events_loop
}
}
pub fn poll<F>(&mut self, mut handler: F) where F: FnMut(WindowEvent) {
self.events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent{event, ..} => match event {
glutin::WindowEvent::CloseRequested => {
handler(WindowEvent::Close);
},
glutin::WindowEvent::Resized(logical_size) => {
handler(WindowEvent::Resize(Dimensions {
width: logical_size.width,
height: logical_size.height,
}));
},
_ => ()
},
_ => ()
}
});
}
pub fn resize(&self, size: Dimensions) {
let dpi = self.gl_window.get_hidpi_factor();
let logical_size = LogicalSize::new(size.width, size.height);
self.gl_window.resize(logical_size.to_physical(dpi));
}
pub fn make_current(&self) {
unsafe {
self.gl_window.make_current().unwrap();
gl::load_with(|symbol| self.gl_window.get_proc_address(symbol) as *const _);
}
}
pub fn clear(&self) {
unsafe {
gl::ClearColor(0.0, 1.0, 0.0, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
}
pub fn swap(&self) {
self.gl_window.swap_buffers().unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment