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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![deny(missing_docs)]
use gl;
use glutin::{
event_loop::EventLoop, window::WindowBuilder, Api, ContextBuilder, ContextError, CreationError,
GlProfile, GlRequest, NotCurrent, PossiblyCurrent, WindowedContext,
};
use luminance::context::GraphicsContext;
use luminance::framebuffer::{Framebuffer, FramebufferError};
use luminance::texture::Dim2;
pub use luminance_gl::gl33::StateQueryError;
use luminance_gl::GL33;
use std::error;
use std::fmt;
use std::os::raw::c_void;
#[derive(Debug)]
pub enum GlutinError {
CreationError(CreationError),
ContextError(ContextError),
GraphicsStateError(StateQueryError),
}
impl fmt::Display for GlutinError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
GlutinError::CreationError(ref e) => write!(f, "Glutin surface creation error: {}", e),
GlutinError::ContextError(ref e) => write!(f, "Glutin OpenGL context creation error: {}", e),
GlutinError::GraphicsStateError(ref e) => {
write!(f, "OpenGL graphics state initialization error: {}", e)
}
}
}
}
impl error::Error for GlutinError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
GlutinError::CreationError(e) => Some(e),
GlutinError::ContextError(e) => Some(e),
GlutinError::GraphicsStateError(e) => Some(e),
}
}
}
impl From<CreationError> for GlutinError {
fn from(e: CreationError) -> Self {
GlutinError::CreationError(e)
}
}
impl From<ContextError> for GlutinError {
fn from(e: ContextError) -> Self {
GlutinError::ContextError(e)
}
}
impl From<StateQueryError> for GlutinError {
fn from(e: StateQueryError) -> Self {
GlutinError::GraphicsStateError(e)
}
}
pub struct GlutinSurface {
pub ctx: WindowedContext<PossiblyCurrent>,
gl: GL33,
}
unsafe impl GraphicsContext for GlutinSurface {
type Backend = GL33;
fn backend(&mut self) -> &mut Self::Backend {
&mut self.gl
}
}
impl GlutinSurface {
pub fn new_gl33_from_builders<'a, WB, CB>(
window_builder: WB,
ctx_builder: CB,
) -> Result<(Self, EventLoop<()>), GlutinError>
where
WB: FnOnce(&mut EventLoop<()>, WindowBuilder) -> WindowBuilder,
CB:
FnOnce(&mut EventLoop<()>, ContextBuilder<'a, NotCurrent>) -> ContextBuilder<'a, NotCurrent>,
{
let mut event_loop = EventLoop::new();
let window_builder = window_builder(&mut event_loop, WindowBuilder::new());
let windowed_ctx = ctx_builder(
&mut event_loop,
ContextBuilder::new()
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
.with_gl_profile(GlProfile::Core),
)
.build_windowed(window_builder, &event_loop)?;
let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };
gl::load_with(|s| ctx.get_proc_address(s) as *const c_void);
ctx.window().set_visible(true);
let gl = GL33::new().map_err(GlutinError::GraphicsStateError)?;
let surface = GlutinSurface { ctx, gl };
Ok((surface, event_loop))
}
pub fn new_gl33(
window_builder: WindowBuilder,
samples: u16,
) -> Result<(Self, EventLoop<()>), GlutinError> {
let event_loop = EventLoop::new();
let windowed_ctx = ContextBuilder::new()
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
.with_gl_profile(GlProfile::Core)
.with_multisampling(samples)
.with_double_buffer(Some(true))
.build_windowed(window_builder, &event_loop)?;
let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };
gl::load_with(|s| ctx.get_proc_address(s) as *const c_void);
ctx.window().set_visible(true);
let gl = GL33::new().map_err(GlutinError::GraphicsStateError)?;
let surface = GlutinSurface { ctx, gl };
Ok((surface, event_loop))
}
pub fn size(&self) -> [u32; 2] {
let size = self.ctx.window().inner_size();
[size.width, size.height]
}
pub fn back_buffer(&mut self) -> Result<Framebuffer<GL33, Dim2, (), ()>, FramebufferError> {
Framebuffer::back_buffer(self, self.size())
}
pub fn swap_buffers(&mut self) {
let _ = self.ctx.swap_buffers();
}
}