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

Implement WASM support for bevy_winit #503

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ required-features = []
name = "headless_wasm"
path = "examples/wasm/headless_wasm.rs"
required-features = []

[[example]]
name = "winit_wasm"
path = "examples/wasm/winit_wasm.rs"
required-features = []
2 changes: 1 addition & 1 deletion crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ serde = { version = "1.0", features = ["derive"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = [ "Window" ] }
wasm-timer = "0.2.5"
instant = "0.1.6"
4 changes: 2 additions & 2 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::{
};
use std::time::Duration;

#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::{thread, time::Instant};
#[cfg(target_arch = "wasm32")]
use wasm_timer::Instant;

#[cfg(target_arch = "wasm32")]
use std::{cell::RefCell, rc::Rc};
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = "0.1.6"
7 changes: 6 additions & 1 deletion crates/bevy_core/src/time/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use bevy_ecs::ResMut;
use std::time::{Duration, Instant};
use std::time::Duration;

#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

/// Tracks elapsed time since the last update and since the App has started
pub struct Time {
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ bevy_utils = { path = "../bevy_utils", version = "0.1" }
# other
uuid = { version = "0.8", features = ["v4", "serde"] }
parking_lot = "0.11.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = "0.1.6"
7 changes: 6 additions & 1 deletion crates/bevy_diagnostic/src/system_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_ecs::{Profiler, Res, ResMut};
use bevy_utils::HashMap;
use parking_lot::RwLock;
use std::{borrow::Cow, sync::Arc, time::Instant};
use std::{borrow::Cow, sync::Arc};

#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

#[derive(Debug)]
struct SystemRunInfo {
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ bevy_utils = { path = "../bevy_utils", version = "0.1" }
# other
winit = { version = "0.22.2", package = "cart-tmp-winit", default-features = false }
log = { version = "0.4", features = ["release_max_level_info"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = { version = "0.22.2", package = "cart-tmp-winit", features = ["web-sys"] }
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
target_os = "netbsd",
target_os = "openbsd"
)))]
fn run_return<F>(event_loop: &mut EventLoop<()>, event_handler: F)
fn run_return<F>(_event_loop: &mut EventLoop<()>, _event_handler: F)
where
F: FnMut(Event<'_, ()>, &EventLoopWindowTarget<()>, &mut ControlFlow),
{
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::monitor

modes.first().unwrap().clone()
}

// WARNING: this only works under the assumption that wasm runtime is single threaded
#[cfg(target_arch = "wasm32")]
unsafe impl Send for WinitWindows {}
#[cfg(target_arch = "wasm32")]
unsafe impl Sync for WinitWindows {}
35 changes: 35 additions & 0 deletions examples/wasm/winit_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extern crate console_error_panic_hook;
use bevy::prelude::*;
use std::panic;

fn main() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(log::Level::Debug).expect("cannot initialize console_log");

App::build()
.add_default_plugins()
.add_startup_system(hello_wasm_system.system())
.add_system(counter.system())
.run();
}

fn hello_wasm_system() {
log::info!("hello wasm");
}

fn counter(mut state: Local<CounterState>, time: Res<Time>) {
if state.count % 60 == 0 {
log::info!(
"tick {} @ {:?} [Δ{}]",
state.count,
time.time_since_startup(),
time.delta_seconds
);
}
state.count += 1;
}

#[derive(Default)]
struct CounterState {
count: u32,
}