use anyhow::anyhow;
use glfw::{fail_on_errors, Context, Glfw, OpenGlProfileHint, PWindow, WindowHint, WindowMode};
use std::sync::Arc;
pub struct GlfwContext {
_wnd: PWindow,
_glfw: Glfw,
pub gl: Arc<glow::Context>,
}
impl GlfwContext {
pub fn new(version: GLVersion, width: u32, height: u32) -> Result<Self, anyhow::Error> {
let mut glfw = glfw::init(fail_on_errors!())?;
let GLVersion(major, minor) = version;
glfw.window_hint(WindowHint::ContextVersion(major, minor));
glfw.window_hint(WindowHint::OpenGlProfile(OpenGlProfileHint::Core));
glfw.window_hint(WindowHint::OpenGlForwardCompat(true));
glfw.window_hint(WindowHint::Visible(false));
glfw.window_hint(WindowHint::OpenGlDebugContext(true));
let (mut wnd, _events) = glfw
.create_window(width, height, env!("CARGO_PKG_NAME"), WindowMode::Windowed)
.ok_or_else(|| anyhow!("No window"))?;
wnd.make_current();
let gl =
unsafe { glow::Context::from_loader_function(|proc| wnd.get_proc_address(proc) as _) };
Ok(Self {
_wnd: wnd,
_glfw: glfw,
gl: Arc::new(gl),
})
}
}
#[derive(Debug)]
pub struct GLVersion(pub u32, pub u32);