1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#![deny(missing_docs)]
use gl;
use glfw::{self, Glfw, InitError, Window, WindowEvent};
use luminance::{
context::GraphicsContext,
framebuffer::{Framebuffer, FramebufferError},
texture::Dim2,
};
pub use luminance_gl::gl33::StateQueryError;
use luminance_gl::GL33;
use std::{error, fmt, os::raw::c_void, sync::mpsc::Receiver};
#[non_exhaustive]
#[derive(Debug)]
pub enum GlfwSurfaceError<E> {
InitError(InitError),
UserError(E),
GraphicsStateError(StateQueryError),
}
impl<E> fmt::Display for GlfwSurfaceError<E>
where
E: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
GlfwSurfaceError::InitError(ref e) => write!(f, "initialization error: {}", e),
GlfwSurfaceError::UserError(ref e) => write!(f, "user error: {}", e),
GlfwSurfaceError::GraphicsStateError(ref e) => {
write!(f, "failed to get graphics state: {}", e)
}
}
}
}
impl<E> From<InitError> for GlfwSurfaceError<E> {
fn from(e: InitError) -> Self {
GlfwSurfaceError::InitError(e)
}
}
impl<E> error::Error for GlfwSurfaceError<E>
where
E: 'static + error::Error,
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
GlfwSurfaceError::InitError(e) => Some(e),
GlfwSurfaceError::UserError(e) => Some(e),
GlfwSurfaceError::GraphicsStateError(e) => Some(e),
}
}
}
#[derive(Debug)]
pub struct GlfwSurface {
pub events_rx: Receiver<(f64, WindowEvent)>,
pub context: GL33Context,
}
impl GlfwSurface {
pub fn new<E>(
create_window: impl FnOnce(
&mut Glfw,
)
-> Result<(Window, Receiver<(f64, WindowEvent)>), GlfwSurfaceError<E>>,
) -> Result<Self, GlfwSurfaceError<E>> {
#[cfg(feature = "log-errors")]
let error_cbk = glfw::LOG_ERRORS;
#[cfg(not(feature = "log-errors"))]
let error_cbk = glfw::FAIL_ON_ERRORS;
let mut glfw = glfw::init(error_cbk)?;
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
glfw::OpenGlProfileHint::Core,
));
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
glfw.window_hint(glfw::WindowHint::ContextVersionMajor(3));
glfw.window_hint(glfw::WindowHint::ContextVersionMinor(3));
let (mut window, events_rx) = create_window(&mut glfw)?;
gl::load_with(|s| window.get_proc_address(s) as *const c_void);
let gl = GL33::new().map_err(GlfwSurfaceError::GraphicsStateError)?;
let context = GL33Context { window, gl };
let surface = GlfwSurface { events_rx, context };
Ok(surface)
}
}
#[derive(Debug)]
pub struct GL33Context {
pub window: Window,
gl: GL33,
}
impl GL33Context {
pub fn back_buffer(&mut self) -> Result<Framebuffer<GL33, Dim2, (), ()>, FramebufferError> {
let (w, h) = self.window.get_framebuffer_size();
Framebuffer::back_buffer(self, [w as u32, h as u32])
}
}
unsafe impl GraphicsContext for GL33Context {
type Backend = GL33;
fn backend(&mut self) -> &mut Self::Backend {
&mut self.gl
}
}