three_d/window/winit_window/
frame_io.rs

1use crate::control::Event;
2use crate::core::{Context, RenderTarget, Viewport};
3
4///
5/// Input for rendering (and whatever else needs it) each frame.
6/// It includes events that can be used as input to [controls](crate::renderer::control).
7/// The data should only be used for one frame.
8///
9/// Note:
10/// [FrameInput] is automatically generated if using the default [Window](crate::window::Window).
11/// Use [FrameInputGenerator](crate::window::FrameInputGenerator) to generate it with a custom [winit](https://crates.io/crates/winit) window.
12///
13#[derive(Clone, Debug)]
14pub struct FrameInput {
15    /// A list of [events](crate::Event) which has occurred since last frame.
16    pub events: Vec<Event>,
17
18    /// Milliseconds since last frame.
19    pub elapsed_time: f64,
20
21    /// Milliseconds accumulated time since start.
22    pub accumulated_time: f64,
23
24    /// Viewport of the window in physical pixels (the size of the screen [RenderTarget] which is returned from [FrameInput::screen]).
25    pub viewport: Viewport,
26
27    /// Width of the window in logical pixels.
28    pub window_width: u32,
29
30    /// Height of the window in logical pixels.
31    pub window_height: u32,
32
33    /// Number of physical pixels for each logical pixel.
34    pub device_pixel_ratio: f32,
35
36    /// Whether or not this is the first frame. Note: also set after the window becomes (partially) visible.
37    pub first_frame: bool,
38
39    /// The graphics context for the window.
40    pub context: Context,
41}
42
43impl FrameInput {
44    ///
45    /// Returns the screen render target, which is used for drawing to the screen, for this window.
46    /// Same as
47    ///
48    /// ```notrust
49    /// RenderTarget::screen(&frame_input.context, frame_input.viewport.width, frame_input.viewport.height)
50    /// ```
51    ///
52    pub fn screen(&self) -> RenderTarget {
53        RenderTarget::screen(&self.context, self.viewport.width, self.viewport.height)
54    }
55}
56
57///
58/// Output from the rendering to the default [Window](crate::window::Window) each frame.
59///
60#[derive(Clone, Debug)]
61pub struct FrameOutput {
62    ///
63    /// If this is true:
64    /// - On desktop, the window is closed and the renderloop is stopped.
65    /// - On web, the render loop is stopped, the event handlers are removed and the `Window` dropped. Note that the canvas is not removed.
66    ///
67    pub exit: bool,
68
69    ///
70    /// Swaps the back and front buffer if this is true.
71    /// Set this to true if something have been rendered this frame and you want to display it.
72    /// Set it to false if nothing have been rendered this frame, for example if nothing has changed,
73    /// and you want to reuse the image from an old frame.
74    /// Currently ignored on web, since it does not use double buffering.
75    ///
76    pub swap_buffers: bool,
77
78    ///
79    /// Whether to stop the render loop until next event.
80    ///
81    pub wait_next_event: bool,
82}
83
84impl Default for FrameOutput {
85    fn default() -> Self {
86        Self {
87            exit: false,
88            swap_buffers: true,
89            wait_next_event: false,
90        }
91    }
92}