wasm thread

This commit is contained in:
Nixon 2022-08-26 18:38:52 +08:00
parent cc2877c9d8
commit 61cb0ce6b4
5 changed files with 131 additions and 8 deletions

99
Cargo.lock generated
View file

@ -920,6 +920,8 @@ dependencies = [
"rapier2d",
"serde",
"ticktock",
"wasm-bindgen",
"wasm_thread",
]
[[package]]
@ -1662,12 +1664,48 @@ dependencies = [
"libc",
]
[[package]]
name = "futures"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab30e97ab6aacfe635fad58f22c2bb06c8b685f7421eb1e064a729e2a5f481fa"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bfc52cbddcfd745bf1740338492bb0bd83d76c67b445f91c5fb29fae29ecaa1"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2acedae88d38235936c3922476b10fced7b2b68136f5e3c03c2d5be348a1115"
[[package]]
name = "futures-executor"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d11aa21b5b587a64682c0094c2bdd4df0076c5324961a40cc3abd7f37930528"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.23"
@ -1689,6 +1727,47 @@ dependencies = [
"waker-fn",
]
[[package]]
name = "futures-macro"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0db9cce532b0eae2ccf2766ab246f114b56b9cf6d445e00c2549fbc100ca045d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca0bae1fe9752cf7fd9b0064c674ae63f97b37bc714d745cbde0afb7ec4e6765"
[[package]]
name = "futures-task"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "842fc63b931f4056a24d59de13fb1272134ce261816e063e634ad0c15cdc5306"
[[package]]
name = "futures-util"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0828a5471e340229c11c77ca80017937ce3c58cb788a17e5f1c2d5c485a9577"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "fxhash"
version = "0.2.1"
@ -1942,7 +2021,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
[[package]]
name = "hexodsp"
version = "0.2.0"
source = "git+https://github.com/WeirdConstructor/HexoDSP#be4e9232cc05b96588b156d1c9f995f7eda41ec0"
source = "git+https://git.txmn.tk/tuxmain/HexoDSP#0ab8cf18354925a6a26ea0e885dfcd6d9f5e9d15"
dependencies = [
"hound",
"lazy_static",
@ -2883,6 +2962,12 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.25"
@ -3700,6 +3785,18 @@ version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a"
[[package]]
name = "wasm_thread"
version = "0.2.0"
source = "git+https://github.com/chemicstry/wasm_thread#19842287886ad1b03ed74bac37ca43167067d270"
dependencies = [
"async-channel",
"futures",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "web-sys"
version = "0.3.59"

View file

@ -15,14 +15,16 @@ rand = "0.8.5"
rand_distr = "0.4.3"
rapier2d = "0.14.0"
serde = { version = "1.0.144", features = ["derive"] }
ticktock = "0.8.0"
wasm-bindgen = "0.2"
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
hexodsp = { git = "https://git.txmn.tk/tuxmain/HexoDSP", default-features = false }
cpal = "0.14.0"
hexodsp = { git = "https://github.com/WeirdConstructor/HexoDSP", default-features = false }
ticktock = "0.8.0"
[target."cfg(target_arch = \"wasm32\")".dependencies]
cpal = { version = "0.14.0", features = ["wasm-bindgen"] }
wasm_thread = { git = "https://github.com/chemicstry/wasm_thread" }
[profile.dev.package."*"]
opt-level = 3

View file

@ -1,2 +1,6 @@
cargo build --release --target wasm32-unknown-unknown
wasm-bindgen --out-name bevyjam --out-dir target --target web target/wasm32-unknown-unknown/release/bevyjam.wasm
# wasm-bindgen --out-name bevyjam --out-dir target --target web target/wasm32-unknown-unknown/release/bevyjam.wasm
wasm-bindgen target/wasm32-unknown-unknown/release/bevyjam.wasm \
--out-name bevyjam \
--out-dir target \
--target no-modules

View file

@ -5,9 +5,13 @@
<title>Bevyjam</title>
</head>
<body>
<script type="module">
import init from './target/bevyjam.js'
init()
<script src="./target/bevyjam.js"></script>
<script type="text/javascript">
wasm_bindgen("./target/bevyjam_bg.wasm").then((wasm) =>{
wasm.run();
});
// import init from './target/bevyjam.js'
// init()
</script>
</body>
</html>

View file

@ -13,6 +13,11 @@ use bevy::{
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_rapier2d::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use std::thread;
#[cfg(target_arch = "wasm32")]
use wasm_thread as thread;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum AppState {
Menu,
@ -20,12 +25,23 @@ enum AppState {
Win,
}
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)] pub fn dummy_main() {}
#[wasm_bindgen]
pub fn run() {
main();
}
fn main() {
let (audio_event_sender, audio_event_receiver) =
crossbeam_channel::bounded::<game::AudioMsg>(512);
#[cfg(not(target_arch = "wasm32"))]
std::thread::spawn(move || audio::setup(audio_event_receiver));
thread::spawn(move || audio::setup(audio_event_receiver));
thread::spawn(|| {/* ... */});
#[cfg(not(target_arch = "wasm32"))]
let first_level = game::LevelId(