Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait before making window visible #9692

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! the mouse pointer in various ways.

use bevy::{
core::FrameCount,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::{CursorGrabMode, PresentMode, PrimaryWindow, WindowLevel, WindowTheme},
window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
};

fn main() {
Expand All @@ -25,7 +26,7 @@ fn main() {
..Default::default()
},
// This will spawn an invisible window
// The window will be made visible in the setup() function
// The window will be made visible in the make_visible() system after 3 frames.
// This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app.
visible: false,
..default()
Expand All @@ -35,7 +36,6 @@ fn main() {
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(Startup, setup)
.add_systems(
Update,
(
Expand All @@ -46,16 +46,20 @@ fn main() {
toggle_window_controls,
cycle_cursor_icon,
switch_level,
make_visible,
),
)
.run();
}

fn setup(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
// At this point the gpu is ready to show the app so we can make the window visible
// There might still be a white frame when doing it in startup.
// Alternatively, you could have a system that waits a few seconds before making the window visible.
primary_window.single_mut().visible = true;
fn make_visible(mut window: Query<&mut Window>, frames: Res<FrameCount>) {
// The delay may be different for your app or system.
if frames.0 == 3 {
// At this point the gpu is ready to show the app so we can make the window visible.
// Alternatively, you could toggle the visibility in Startup.
// It will work, but it will have one white frame before it starts rendering
window.single_mut().visible = true;
}
}

/// This system toggles the vsync mode when pressing the button V.
Expand Down