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

Fix white frames at startup #323

Merged
merged 1 commit into from
Jul 2, 2024
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
47 changes: 25 additions & 22 deletions src/bevy_config.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
use crate::util::error;
use bevy::render::settings::{WgpuFeatures, WgpuSettings};
use bevy::render::RenderPlugin;
use bevy::core::FrameCount;
use bevy::{prelude::*, window::PrimaryWindow, winit::WinitWindows};
use std::io::Cursor;
use winit::window::Icon;

/// Overrides the default Bevy plugins and configures things like the screen settings.
pub(super) fn plugin(app: &mut App) {
app.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: "Foxtrot".to_string(),
..default()
}),
DefaultPlugins.set(WindowPlugin {
primary_window: Window {
title: "Foxtrot".to_string(),
// This will spawn an invisible window
// 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()
})
.set(RenderPlugin {
render_creation: create_wgpu_settings().into(),
synchronous_pipeline_compilation: false,
}),
}
.into(),
..default()
}),
)
.insert_resource(Msaa::Sample4)
.insert_resource(ClearColor(Color::rgb(0.4, 0.4, 0.4)))
.add_systems(Startup, set_window_icon.pipe(error));
}

fn create_wgpu_settings() -> WgpuSettings {
let mut wgpu_settings = WgpuSettings::default();
wgpu_settings
.features
.set(WgpuFeatures::VERTEX_WRITABLE_STORAGE, true);
wgpu_settings
.add_systems(Startup, set_window_icon.pipe(error))
.add_systems(Update, make_visible);
}

// Sets the icon on Windows and X11
Expand All @@ -55,3 +47,14 @@ fn set_window_icon(
};
Ok(())
}

/// Source: <https://github.com/bevyengine/bevy/blob/v0.14.0-rc.4/examples/window/window_settings.rs#L56>
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;
}
}