/
frame.rs
70 lines (60 loc) · 1.74 KB
/
frame.rs
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
use super::Window;
use crate::graphics::{Color, Gpu, Target};
/// The next frame of your game.
///
/// You can only get a [`Frame`] by using [`Window::frame`].
///
/// This type is useful to define explicit rendering function signatures. If
/// a function should never render off-screen, consider taking a `Frame` as an
/// argument instead of a generic [`Target`].
///
/// [`Frame`]: struct.Frame.html
/// [`Window::frame`]: struct.Window.html#method.frame
/// [`Target`]: struct.Target.html
#[derive(Debug)]
pub struct Frame<'a> {
window: &'a mut Window,
}
impl<'a> Frame<'a> {
pub(crate) fn new(window: &mut Window) -> Frame<'_> {
Frame { window }
}
/// Get the [`Gpu`] linked to the [`Window`] of this [`Frame`].
///
/// [`Gpu`]: struct.Gpu.html
/// [`Window`]: struct.Window.html
/// [`Frame`]: struct.Frame.html
pub fn gpu(&mut self) -> &mut Gpu {
self.window.gpu()
}
/// Get the width of the frame.
pub fn width(&self) -> f32 {
self.window.width
}
/// Get the height of the frame.
pub fn height(&self) -> f32 {
self.window.height
}
/// See the frame as a [`Target`].
///
/// You will need to use this in order to render some resources to it.
///
/// [`Target`]: struct.Target.html
pub fn as_target(&mut self) -> Target<'_> {
let Window {
surface,
gpu,
width,
height,
..
} = &mut self.window;
let view = surface.target();
Target::new(gpu, view, *width, *height)
}
/// Clear the frame with the given [`Color`].
///
/// [`Color`]: struct.Color.html
pub fn clear(&mut self, color: Color) {
self.as_target().clear(color);
}
}