Wait before making window visible (#9692)

# Objective

- The current example still has one white frame before it starts
rendering.

## Solution

- Wait 3 frames before toggling visibility
This commit is contained in:
IceSentry 2023-09-11 15:09:55 -04:00 committed by GitHub
parent 4c6b6fc24a
commit 027ff9f378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,10 @@
//! the mouse pointer in various ways. //! the mouse pointer in various ways.
use bevy::{ use bevy::{
core::FrameCount,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*, prelude::*,
window::{CursorGrabMode, PresentMode, PrimaryWindow, WindowLevel, WindowTheme}, window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
}; };
fn main() { fn main() {
@ -25,7 +26,7 @@ fn main() {
..Default::default() ..Default::default()
}, },
// This will spawn an invisible window // 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. // 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, visible: false,
..default() ..default()
@ -35,7 +36,6 @@ fn main() {
LogDiagnosticsPlugin::default(), LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin, FrameTimeDiagnosticsPlugin,
)) ))
.add_systems(Startup, setup)
.add_systems( .add_systems(
Update, Update,
( (
@ -46,16 +46,20 @@ fn main() {
toggle_window_controls, toggle_window_controls,
cycle_cursor_icon, cycle_cursor_icon,
switch_level, switch_level,
make_visible,
), ),
) )
.run(); .run();
} }
fn setup(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) { fn make_visible(mut window: Query<&mut Window>, frames: Res<FrameCount>) {
// At this point the gpu is ready to show the app so we can make the window visible // The delay may be different for your app or system.
// There might still be a white frame when doing it in startup. if frames.0 == 3 {
// Alternatively, you could have a system that waits a few seconds before making the window visible. // At this point the gpu is ready to show the app so we can make the window visible.
primary_window.single_mut().visible = true; // 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. /// This system toggles the vsync mode when pressing the button V.