Skip to main content

nightshade_api/
window.rs

1//! The OS window and frame timing: title, size, cursor lock, and the per-frame
2//! clock. Reads of the clock pair with [`delta_time`](crate::prelude::delta_time)
3//! in the input module.
4
5use nightshade::prelude::*;
6
7/// Sets the OS window title.
8pub fn set_window_title(world: &mut World, title: &str) {
9    world
10        .res_mut::<nightshade::ecs::window::resources::Window>()
11        .title = title.to_string();
12}
13
14/// The window's current viewport size in physical pixels, if the window is up.
15pub fn window_size(world: &World) -> Option<(u32, u32)> {
16    world
17        .res::<nightshade::ecs::window::resources::Window>()
18        .cached_viewport_size
19}
20
21/// Locks or unlocks the cursor to the window, hiding it while locked. Locking
22/// drives mouselook in a first person camera; unlocking frees the cursor for the
23/// retained UI and pauses the look. Toggle it to switch between walking and
24/// clicking a panel.
25pub fn lock_cursor(world: &mut World, locked: bool) {
26    set_cursor_locked(world, locked);
27    set_cursor_visible(world, !locked);
28}
29
30/// Whether the cursor is currently locked to the window.
31pub fn cursor_locked(world: &World) -> bool {
32    world
33        .res::<nightshade::ecs::window::resources::Window>()
34        .cursor_locked
35}
36
37/// The current frames per second, as the engine measures it.
38pub fn frames_per_second(world: &World) -> f32 {
39    world.res::<nightshade::ecs::time::Time>().frames_per_second
40}
41
42/// Frames rendered in the current one-second sampling window. This is the live
43/// FPS accumulator: it counts up each frame and resets to zero every second.
44pub fn frame_count(world: &World) -> u32 {
45    world.res::<nightshade::ecs::time::Time>().frame_counter
46}
47
48/// Milliseconds elapsed since startup.
49pub fn uptime_milliseconds(world: &World) -> u64 {
50    world
51        .res::<nightshade::ecs::time::Time>()
52        .uptime_milliseconds
53}
54
55/// Asks the app to exit at the end of the current frame.
56pub fn request_exit(world: &mut World) {
57    world
58        .res_mut::<nightshade::ecs::window::resources::Window>()
59        .should_exit = true;
60}