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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use draw::properties::color::IntoRgba;
use glium::{self, Surface};
use glium::framebuffer::{MultiOutputFrameBuffer, SimpleFrameBuffer};
use glium::uniforms::{MagnifySamplerFilter, Uniforms};
use glium::vertex::MultiVerticesSource;
use glium::index::IndicesSource;
use std::collections::{hash_map, HashMap};
use std::cell::{RefMut, RefCell};
use std::ops::{Deref, DerefMut};
use window;
pub struct Frame {
gl_frames: HashMap<window::Id, RefCell<GlFrame>>,
main_window: Option<window::Id>,
}
pub struct WindowFrames<'a> {
iter: hash_map::Iter<'a, window::Id, RefCell<GlFrame>>,
}
impl Frame {
pub fn window(&self, id: window::Id) -> Option<WindowFrame> {
self.gl_frames
.get(&id)
.map(|wf| WindowFrame { frame: wf.borrow_mut() })
}
pub fn main_window(&self) -> WindowFrame {
self.main_window
.and_then(|id| self.window(id))
.or_else(|| {
self.gl_frames
.values()
.next()
.map(|wf| WindowFrame { frame: wf.borrow_mut() })
})
.expect("no `main_window` in `Frame`")
}
pub fn windows(&self) -> WindowFrames {
let iter = self.gl_frames.iter();
WindowFrames { iter }
}
pub fn clear_all<C>(&self, color: C)
where
C: IntoRgba<f32>,
{
let rgba = color.into_rgba();
for (_, mut frame) in self.windows() {
frame.clear(rgba);
}
}
}
impl<'a> Iterator for WindowFrames<'a> {
type Item = (window::Id, WindowFrame<'a>);
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|(&id, wf)| (id, WindowFrame { frame: wf.borrow_mut() }))
}
}
pub fn new(gl_frames: HashMap<window::Id, RefCell<GlFrame>>, main_window: Option<window::Id>) -> Frame {
Frame { gl_frames, main_window }
}
pub fn finish(Frame { gl_frames, .. }: Frame) -> Result<(), glium::SwapBuffersError> {
for (_, gl_frame) in gl_frames {
gl_frame.into_inner().frame.finish()?;
}
Ok(())
}
pub struct WindowFrame<'a> {
pub(crate) frame: RefMut<'a, GlFrame>,
}
impl<'a> Deref for WindowFrame<'a> {
type Target = RefMut<'a, GlFrame>;
fn deref(&self) -> &Self::Target {
&self.frame
}
}
impl<'a> DerefMut for WindowFrame<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.frame
}
}
pub struct GlFrame {
pub(crate) frame: glium::Frame,
}
impl GlFrame {
pub fn new(frame: glium::Frame) -> Self {
GlFrame { frame }
}
pub fn clear_raw(
&mut self,
rect: Option<&glium::Rect>,
color: Option<(f32, f32, f32, f32)>,
color_srgb: bool,
depth: Option<f32>,
stencil: Option<i32>,
) {
self.frame.clear(rect, color, color_srgb, depth, stencil);
}
pub fn dimensions_pixels(&self) -> (u32, u32) {
self.frame.get_dimensions()
}
pub fn depth_buffer_bits(&self) -> Option<u16> {
self.frame.get_depth_buffer_bits()
}
pub fn stencil_buffer_bits(&self) -> Option<u16> {
self.frame.get_stencil_buffer_bits()
}
pub fn draw<'a, 'b, V, I, U>(
&mut self,
vertex_buffer: V,
index_buffer: I,
program: &glium::Program,
uniforms: &U,
draw_parameters: &glium::DrawParameters,
) -> Result<(), glium::DrawError>
where
I: Into<IndicesSource<'a>>,
U: Uniforms,
V: MultiVerticesSource<'b>,
{
self.frame.draw(vertex_buffer, index_buffer, program, uniforms, draw_parameters)
}
pub fn blit_color<S>(
&self,
source_rect: &glium::Rect,
target: &S,
target_rect: &glium::BlitTarget,
filter: MagnifySamplerFilter,
)
where
S: Surface,
{
self.frame.blit_color(source_rect, target, target_rect, filter)
}
pub fn blit_from_frame(
&self,
source_rect: &glium::Rect,
target_rect: &glium::BlitTarget,
filter: MagnifySamplerFilter,
) {
self.frame.blit_from_frame(source_rect, target_rect, filter)
}
pub fn blit_from_simple_framebuffer(
&self,
source: &SimpleFrameBuffer,
source_rect: &glium::Rect,
target_rect: &glium::BlitTarget,
filter: MagnifySamplerFilter,
) {
self.frame.blit_from_simple_framebuffer(source, source_rect, target_rect, filter)
}
pub fn blit_from_multioutput_framebuffer(
&self,
source: &MultiOutputFrameBuffer,
source_rect: &glium::Rect,
target_rect: &glium::BlitTarget,
filter: MagnifySamplerFilter,
) {
self.frame.blit_from_multioutput_framebuffer(source, source_rect, target_rect, filter)
}
pub fn blit_whole_color_to<S>(
&self,
target: &S,
target_rect: &glium::BlitTarget,
filter: MagnifySamplerFilter,
)
where
S: Surface,
{
self.frame.blit_whole_color_to(target, target_rect, filter)
}
pub fn fill<S>(&self, target: &S, filter: MagnifySamplerFilter)
where S: Surface,
{
self.frame.fill(target, filter)
}
pub fn clear<C>(&mut self, color: C)
where
C: IntoRgba<f32>,
{
let color = color.into_rgba();
self.clear_color(color.red, color.green, color.blue, color.alpha);
}
pub fn clear_color(&mut self, red: f32, green: f32, blue: f32, alpha: f32) {
self.frame.clear_color(red, green, blue, alpha)
}
pub fn clear_color_srgb(&mut self, red: f32, green: f32, blue: f32, alpha: f32) {
self.frame.clear_color_srgb(red, green, blue, alpha)
}
pub fn clear_depth(&mut self, value: f32) {
self.frame.clear_depth(value)
}
pub fn clear_stencil(&mut self, value: i32) {
self.frame.clear_stencil(value)
}
pub fn clear_color_and_depth(&mut self, color: (f32, f32, f32, f32), depth: f32) {
self.frame.clear_color_and_depth(color, depth)
}
pub fn clear_color_srgb_and_depth(&mut self, color: (f32, f32, f32, f32), depth: f32) {
self.frame.clear_color_srgb_and_depth(color, depth)
}
pub fn clear_color_and_stencil(&mut self, color: (f32, f32, f32, f32), stencil: i32) {
self.frame.clear_color_and_stencil(color, stencil)
}
pub fn clear_color_srgb_and_stencil(&mut self, color: (f32, f32, f32, f32), stencil: i32) {
self.frame.clear_color_srgb_and_stencil(color, stencil)
}
pub fn clear_depth_and_stencil(&mut self, depth: f32, stencil: i32) {
self.frame.clear_depth_and_stencil(depth, stencil)
}
pub fn clear_all(&mut self, color: (f32, f32, f32, f32), depth: f32, stencil: i32) {
self.frame.clear_all(color, depth, stencil)
}
pub fn clear_all_srgb(&mut self, color: (f32, f32, f32, f32), depth: f32, stencil: i32) {
self.frame.clear_all_srgb(color, depth, stencil)
}
pub fn has_depth_buffer(&self) -> bool {
self.frame.has_depth_buffer()
}
pub fn has_stencil_buffer(&self) -> bool {
self.frame.has_stencil_buffer()
}
}