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

Allow Bevy to start from non-main threads on supported platforms #10020

Merged
merged 7 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl PluginGroup for DefaultPlugins {

#[cfg(feature = "bevy_winit")]
{
group = group.add(bevy_winit::WinitPlugin);
group = group.add(bevy_winit::WinitPlugin::default());
}

#[cfg(feature = "bevy_render")]
Expand Down
44 changes: 43 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,53 @@ pub static ANDROID_APP: std::sync::OnceLock<AndroidApp> = std::sync::OnceLock::n
/// replace the existing [`App`] runner with one that constructs an [event loop](EventLoop) to
/// receive window and input events from the OS.
#[derive(Default)]
pub struct WinitPlugin;
pub struct WinitPlugin {
/// Allows the window (and the event loop) to be created on any thread
/// instead of only the main thread.
///
/// See [`winit with_any_thread`].
///
/// # Supported platforms
///
/// Only works on Linux (X11/Wayland) and Windows.
/// This field is ignored on other platforms.
///
/// [`winit with_any_thread`]: https://docs.rs/winit/latest/winit/event_loop/struct.EventLoopBuilder.html#method.with_any_thread
james7132 marked this conversation as resolved.
Show resolved Hide resolved
pub run_on_any_thread: bool,
}

impl Plugin for WinitPlugin {
fn build(&self, app: &mut App) {
let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event();

// This is needed because the features checked in the inner
// block might be enabled on other platforms than linux.
#[cfg(target_os = "linux")]
{
#[cfg(feature = "x11")]
{
use winit::platform::x11::EventLoopBuilderExtX11;

// This allows a Bevy app to be started and ran outside of the main thread.
// A use case for this is to allow external applications to spawn a thread
// which runs a Bevy app without requiring the Bevy app to need to reside on
// the main thread, which can be problematic.
event_loop_builder.with_any_thread(self.run_on_any_thread);
}

#[cfg(feature = "wayland")]
{
use winit::platform::wayland::EventLoopBuilderExtWayland;
event_loop_builder.with_any_thread(self.run_on_any_thread);
}
}

#[cfg(target_os = "windows")]
{
use winit::platform::windows::EventLoopBuilderExtWindows;
event_loop_builder.with_any_thread(self.run_on_any_thread);
}

#[cfg(target_os = "android")]
{
use winit::platform::android::EventLoopBuilderExtAndroid;
Expand Down