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
#![deny(missing_docs)]
use gl;
pub use glutin;
use glutin::{
event_loop::EventLoop,
window::WindowBuilder,
Api, ContextBuilder, ContextError, CreationError, GlProfile, GlRequest, NotCurrent,
PossiblyCurrent, WindowedContext,
};
use luminance::context::GraphicsContext;
use luminance::framebuffer::Framebuffer;
use luminance::state::{GraphicsState, StateQueryError};
use luminance::texture::{Dim2, Flat};
pub use luminance_windowing::{CursorMode, Surface, WindowDim, WindowOpt};
use std::cell::RefCell;
use std::fmt;
use std::os::raw::c_void;
use std::rc::Rc;
#[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 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)
}
}
pub struct GlutinSurface {
pub ctx: WindowedContext<PossiblyCurrent>,
gfx_state: Rc<RefCell<GraphicsState>>,
}
unsafe impl GraphicsContext for GlutinSurface {
fn state(&self) -> &Rc<RefCell<GraphicsState>> {
&self.gfx_state
}
}
impl GlutinSurface {
pub fn from_builders<WB, CB>(window_builder: WB, ctx_builder: CB) -> Result<(Self, EventLoop<()>), GlutinError>
where
WB: FnOnce(WindowBuilder) -> WindowBuilder,
CB: FnOnce(ContextBuilder<NotCurrent>) -> ContextBuilder<NotCurrent>,
{
let event_loop = EventLoop::new();
let window_builder = window_builder(WindowBuilder::new());
let windowed_ctx = ctx_builder(
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 gfx_state = GraphicsState::new().map_err(GlutinError::GraphicsStateError)?;
let surface = GlutinSurface {
ctx,
gfx_state: Rc::new(RefCell::new(gfx_state)),
};
Ok((surface, event_loop))
}
pub fn new(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 gfx_state = GraphicsState::new().map_err(GlutinError::GraphicsStateError)?;
let surface = GlutinSurface {
ctx,
gfx_state: Rc::new(RefCell::new(gfx_state)),
};
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<Flat, Dim2, (), ()>, GlutinError> {
Ok(Framebuffer::back_buffer(self, self.size()))
}
pub fn swap_buffers(&mut self) {
let _ = self.ctx.swap_buffers();
}
}