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
use {
crate::{
driver::device::Device,
graph::{node::SwapchainImageNode, RenderGraph},
},
std::sync::Arc,
winit::{dpi::PhysicalPosition, event::Event, window::Window},
};
/// Centers the mouse cursor within the window.
pub fn center_cursor(window: &Window) {
let window_size = window.inner_size();
let x = window_size.width / 2;
let y = window_size.height / 2;
set_cursor_position(window, x, y);
}
/// Sets the mouse cursor at the specified position within the window.
pub fn set_cursor_position(window: &Window, x: u32, y: u32) {
let position = PhysicalPosition::new(x as i32, y as i32);
window.set_cursor_position(position).unwrap_or_default();
}
/// A request to render a single frame to the provided render graph.
pub struct FrameContext<'a> {
/// The device this frame belongs to.
pub device: &'a Arc<Device>,
/// The elapsed seconds since the previous frame.
pub dt: f32,
/// A slice of events that have occurred since the previous frame.
pub events: &'a [Event<()>],
/// The height, in pixels, of the current frame.
pub height: u32,
/// A render graph which rendering commands should be recorded into.
///
/// Make sure to write to `swapchain_image` as part of this graph.
pub render_graph: &'a mut RenderGraph,
/// A pre-bound image node for the swapchain image to be drawn.
pub swapchain_image: SwapchainImageNode,
/// A mutable `bool` which indicates if this frame should cause the program to exit.
pub will_exit: &'a mut bool,
/// The width, in pixels, of the current frame.
pub width: u32,
/// A borrow of the operating system window relating to this frame.
pub window: &'a Window,
}
impl FrameContext<'_> {
/// Causes the program to exit after rendering this frame.
pub fn exit(&mut self) {
*self.will_exit = true;
}
/// Returns the frame width divided by the frame height.
pub fn render_aspect_ratio(&self) -> f32 {
self.width as f32 / self.height as f32
}
/// Centers the mouse cursor within the window.
pub fn center_cursor(&self) {
center_cursor(self.window);
}
/// Sets the mouse cursor at the specified position within the window.
pub fn set_cursor_position(&self, x: u32, y: u32) {
set_cursor_position(self.window, x, y);
}
}