diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..8a0f0f5 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,4 @@ +[build] +rustflags = [ +"--cfg=web_sys_unstable_apis" +] diff --git a/Cargo.lock b/Cargo.lock index 4a21a98..d27abc1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,15 +89,15 @@ checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" [[package]] name = "bumpalo" -version = "3.10.0" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" +checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" [[package]] name = "bytemuck" -version = "1.11.0" +version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5377c8865e74a160d21f29c2d40669f53286db6eab59b88540cbb12ffc8b835" +checksum = "2f5715e491b5a1598fc2bef5a606847b5dc1d48ea625bd3c02c00de8285591da" [[package]] name = "byteorder" @@ -112,7 +112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf2eec61efe56aa1e813f5126959296933cf0700030e4314786c48779a66ab82" dependencies = [ "log", - "nix", + "nix 0.22.3", ] [[package]] @@ -139,19 +139,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time", - "winapi", -] - [[package]] name = "cocoa" version = "0.24.0" @@ -193,6 +180,27 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen", +] + +[[package]] +name = "console_log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501a375961cef1a0d44767200e66e4a559283097e91d0730b1d75dfb2f8a1494" +dependencies = [ + "log", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "copyless" version = "0.1.5" @@ -404,8 +412,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if 1.0.0", + "js-sys", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", + "wasm-bindgen", ] [[package]] @@ -441,13 +451,13 @@ dependencies = [ [[package]] name = "gpu-descriptor" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a538f217be4d405ff4719a283ca68323cc2384003eca5baaa87501e821c81dda" +checksum = "0b0c02e1ba0bdb14e965058ca34e09c020f8e507a760df1121728e0aef68d57a" dependencies = [ "bitflags", "gpu-descriptor-types", - "hashbrown 0.11.2", + "hashbrown", ] [[package]] @@ -461,19 +471,13 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ "ahash", ] -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - [[package]] name = "hermit-abi" version = "0.1.19" @@ -508,7 +512,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", - "hashbrown 0.12.3", + "hashbrown", ] [[package]] @@ -537,9 +541,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" [[package]] name = "js-sys" -version = "0.3.58" +version = "0.3.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27" +checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2" dependencies = [ "wasm-bindgen", ] @@ -562,9 +566,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.126" +version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "libloading" @@ -578,15 +582,15 @@ dependencies = [ [[package]] name = "libm" -version = "0.2.2" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db" +checksum = "292a948cd991e376cf75541fe5b97a1081d713c618b4f1b9500f8844e49eb565" [[package]] name = "lock_api" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" +checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" dependencies = [ "autocfg", "scopeguard", @@ -662,7 +666,7 @@ checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" dependencies = [ "libc", "log", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", "windows-sys", ] @@ -750,6 +754,18 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "195cdbc1741b8134346d515b3a56a1c94b0912758009cfd53f99ea0f57b065fc" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "7.1.1" @@ -761,23 +777,23 @@ dependencies = [ ] [[package]] -name = "num-integer" -version = "0.1.45" +name = "num-traits" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", - "num-traits", + "libm", ] [[package]] -name = "num-traits" -version = "0.2.15" +name = "num_cpus" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" dependencies = [ - "autocfg", - "libm", + "hermit-abi", + "libc", ] [[package]] @@ -822,9 +838,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" [[package]] name = "parking_lot" @@ -857,6 +873,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + [[package]] name = "pixels" version = "0.9.0" @@ -891,19 +913,20 @@ checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] name = "proc-macro-crate" -version = "1.1.3" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a" +checksum = "eda0fc3b0fb7c975631757e14d9049da17374063edb6ebbcbc54d880d4fe94e9" dependencies = [ + "once_cell", "thiserror", "toml", ] [[package]] name = "proc-macro2" -version = "1.0.41" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdcc2916cde080c1876ff40292a396541241fe0072ef928cd76582e9ea5d60d2" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ "unicode-ident", ] @@ -919,22 +942,28 @@ name = "purple_rain" version = "0.1.0" dependencies = [ "bencher", - "chrono", + "console_error_panic_hook", + "console_log", "env_logger", + "getrandom", + "instant", "log", "pixels", "rand", "rand_distr", - "timer", + "tokio", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", "winit", "winit_input_helper", ] [[package]] name = "quote" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" dependencies = [ "proc-macro2", ] @@ -996,9 +1025,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534cfe58d6a18cc17120fbf4635d53d14691c1fe4d951064df9bd326178d7d5a" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ "bitflags", ] @@ -1055,9 +1084,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.140" +version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc855a42c7967b7c369eb5860f7164ef1f6f81c20c7cc1141f2a604e18723b03" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" [[package]] name = "slotmap" @@ -1086,13 +1115,23 @@ dependencies = [ "lazy_static", "log", "memmap2", - "nix", + "nix 0.22.3", "pkg-config", "wayland-client", "wayland-cursor", "wayland-protocols", ] +[[package]] +name = "socket2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "spirv" version = "0.2.0+1.5.4" @@ -1111,9 +1150,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.98" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" dependencies = [ "proc-macro2", "quote", @@ -1131,18 +1170,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.31" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a" +checksum = "3d0a539a918745651435ac7db7a18761589a94cd7e94cd56999f828bf73c8a57" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.31" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a" +checksum = "c251e90f708e16c49a16f4917dc2131e75222b72edfa9cb7f7c58ae56aae0c09" dependencies = [ "proc-macro2", "quote", @@ -1150,23 +1189,16 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", -] - -[[package]] -name = "timer" -version = "0.2.0" +name = "tokio" +version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31d42176308937165701f50638db1c31586f183f1aab416268216577aec7306b" +checksum = "89797afd69d206ccd11fb0ea560a44bbb87731d020670e79416d442919257d42" dependencies = [ - "chrono", + "autocfg", + "num_cpus", + "once_cell", + "pin-project-lite", + "socket2", ] [[package]] @@ -1189,9 +1221,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" [[package]] name = "unicode-width" @@ -1205,12 +1237,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1219,9 +1245,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994" +checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -1229,13 +1255,13 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a" +checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f" dependencies = [ "bumpalo", - "lazy_static", "log", + "once_cell", "proc-macro2", "quote", "syn", @@ -1244,9 +1270,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.31" +version = "0.4.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de9a9cec1733468a8c657e57fa2413d2ae2c0129b95e87c5b72b8ace4d13f31f" +checksum = "fa76fb221a1f8acddf5b54ace85912606980ad661ac7a503b4570ffd3a624dad" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -1256,9 +1282,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa" +checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1266,9 +1292,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048" +checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da" dependencies = [ "proc-macro2", "quote", @@ -1279,20 +1305,20 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.81" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be" +checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a" [[package]] name = "wayland-client" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91223460e73257f697d9e23d401279123d36039a3f7a449e983f123292d4458f" +checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" dependencies = [ "bitflags", "downcast-rs", "libc", - "nix", + "nix 0.24.2", "scoped-tls", "wayland-commons", "wayland-scanner", @@ -1301,11 +1327,11 @@ dependencies = [ [[package]] name = "wayland-commons" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94f6e5e340d7c13490eca867898c4cec5af56c27a5ffe5c80c6fc4708e22d33e" +checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" dependencies = [ - "nix", + "nix 0.24.2", "once_cell", "smallvec", "wayland-sys", @@ -1313,20 +1339,20 @@ dependencies = [ [[package]] name = "wayland-cursor" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c52758f13d5e7861fc83d942d3d99bf270c83269575e52ac29e5b73cb956a6bd" +checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" dependencies = [ - "nix", + "nix 0.24.2", "wayland-client", "xcursor", ] [[package]] name = "wayland-protocols" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60147ae23303402e41fe034f74fb2c35ad0780ee88a1c40ac09a3be1e7465741" +checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" dependencies = [ "bitflags", "wayland-client", @@ -1336,9 +1362,9 @@ dependencies = [ [[package]] name = "wayland-scanner" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39a1ed3143f7a143187156a2ab52742e89dac33245ba505c17224df48939f9e0" +checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" dependencies = [ "proc-macro2", "quote", @@ -1347,9 +1373,9 @@ dependencies = [ [[package]] name = "wayland-sys" -version = "0.29.4" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9341df79a8975679188e37dab3889bfa57c44ac2cb6da166f519a81cbe452d4" +checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" dependencies = [ "dlib", "lazy_static", @@ -1585,9 +1611,9 @@ dependencies = [ [[package]] name = "x11-dl" -version = "2.19.1" +version = "2.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea26926b4ce81a6f5d9d0f3a0bc401e5a37c6ae14a1bfaa8ff6099ca80038c59" +checksum = "0c83627bc137605acc00bb399c7b908ef460b621fc37c953db2b09f88c449ea6" dependencies = [ "lazy_static", "libc", diff --git a/Cargo.toml b/Cargo.toml index 11fa47f..34ec9f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,16 +6,29 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -chrono = "0.4.19" -env_logger = "0.9.0" +instant = "0.1.12" log = "0.4.17" pixels = "0.9.0" rand = "0.8.5" rand_distr = "0.4.3" -timer = "0.2.0" winit = "0.26.1" + +[target.'cfg(target_family = "wasm")'.dependencies] +console_error_panic_hook = "0.1.7" +getrandom = { version = "0.2", features = ["js"] } +console_log = { version = "0.2.0", features = ["wasm-bindgen"] } +wasm-bindgen = "0.2.82" +wasm-bindgen-futures = "0.4.32" +web-sys = { version = "0.3.57", features = ["Element", "HtmlCollection"] } + +[target.'cfg(not(target_family = "wasm"))'.dependencies] +tokio = { version = "1.21.0", features = ["rt-multi-thread", "sync", "time"] } +env_logger = "0.9.0" winit_input_helper = "0.12.0" +[target.wasm32-unknown-unknown.dependencies] +web-sys = "*" + [dev-dependencies] bencher = "0.1.5" diff --git a/index.html b/index.html new file mode 100644 index 0000000..698d115 --- /dev/null +++ b/index.html @@ -0,0 +1,9 @@ + + + + +blah + + + + diff --git a/src/lib.rs b/src/lib.rs index f16de87..dbe954a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,33 +9,6 @@ pub const PURPLE_RAIN: &[u8] = &[207, 100, 219, 0xff]; pub const BLUE_BACKGROUND: &[u8] = &[0x8f, 0xbe, 0xeb, 0xff]; pub const BLUE_RAIN: &[u8] = &[0x3e, 0x9f, 0xfa, 0xff]; -pub fn draw(drops: &mut Vec, frame: &mut [u8]) { - let pixels = frame.chunks_exact_mut(4); - for pixel in pixels { - pixel.copy_from_slice(BLUE_BACKGROUND); - } - - drops.iter_mut().for_each(|d| d.update()); - - for drop in drops { - let mut drawn = false; - for y in drop.y..(drop.y + drop.z) { - for x in drop.x..(drop.x + drop.z/4) { - if let Ok(i) = (y * (WIDTH as i32) * 4 + x * 4).try_into() { - if i < frame.len() { - drawn = true; - frame[i..i + 4].copy_from_slice(BLUE_RAIN); - } - } - } - } - if !drawn { - drop.y = -drop.z; - drop.x = rand::thread_rng().gen_range(0..WIDTH as i32); - } - } -} - pub struct Drop { pub x: i32, pub y: i32, @@ -44,6 +17,33 @@ pub struct Drop { impl Drop { fn update(&mut self) { - self.y += std::cmp::max(self.z/4, 1); + self.y += std::cmp::max(self.z / 4, 1); + } + + pub fn draw(drops: &mut [Drop], frame: &mut [u8]) { + let pixels = frame.chunks_exact_mut(4); + for pixel in pixels { + pixel.copy_from_slice(BLUE_BACKGROUND); + } + + drops.iter_mut().for_each(Drop::update); + + for drop in drops { + let mut drawn = false; + for y in drop.y..(drop.y + drop.z) { + for x in drop.x..(drop.x + drop.z / 4) { + if let Ok(i) = (y * (WIDTH as i32) * 4 + x * 4).try_into() { + if i < frame.len() { + drawn = true; + frame[i..i + 4].copy_from_slice(BLUE_RAIN); + } + } + } + } + if !drawn { + drop.y = -drop.z; + drop.x = rand::thread_rng().gen_range(0..WIDTH as i32); + } + } } } diff --git a/src/main.rs b/src/main.rs index c24d2c5..3612adb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,42 +1,68 @@ #![windows_subsystem = "windows"] -use chrono::Duration; use log::error; use pixels::{Pixels, SurfaceTexture}; use rand::Rng; -use timer::Timer; + +#[cfg(not(target_arch = "wasm32"))] +use winit::event::VirtualKeyCode; use winit::{ dpi::LogicalSize, - event::{Event, VirtualKeyCode}, + event::{ + Event, + StartCause::{self, ResumeTimeReached}, + }, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; +#[cfg(not(target_arch = "wasm32"))] use winit_input_helper::WinitInputHelper; -use purple_rain::*; +use purple_rain::{Drop, HEIGHT, WIDTH}; fn main() { - env_logger::init(); + #[cfg(target_arch = "wasm32")] + { + wasm_bindgen_futures::spawn_local(run()); + } + #[cfg(not(target_arch = "wasm32"))] + { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .enable_time() + .build() + .unwrap() + .block_on(run()); + } +} + +async fn run() { let event_loop = EventLoop::new(); + #[cfg(not(target_arch = "wasm32"))] let mut input = WinitInputHelper::new(); let window = { - let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64); - let scaled_size = LogicalSize::new(WIDTH as f64 * 3.0, HEIGHT as f64 * 3.0); + let size = LogicalSize::new(f64::from(WIDTH), f64::from(HEIGHT)); + let scaled_size = LogicalSize::new(f64::from(WIDTH) * 4.0, f64::from(HEIGHT) * 4.0); WindowBuilder::new() .with_title("Purple Rain") .with_inner_size(scaled_size) .with_min_inner_size(size) .build(&event_loop) - .unwrap() + .expect("Could not initialize the window") }; + #[cfg(all(target_arch = "wasm32", not(debug_assertions)))] + wasm::create_canvas(&window); + #[cfg(all(target_arch = "wasm32", debug_assertions))] + let log_list = wasm::create_log_list(&window); + let mut pixels = { let window_size = window.inner_size(); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); - Pixels::new(WIDTH, HEIGHT, surface_texture) + Pixels::new_async(WIDTH, HEIGHT, surface_texture).await } - .unwrap(); + .expect("Could not initialize the pixels object."); let mut drops = Vec::new(); let mut rng = rand::thread_rng(); @@ -45,33 +71,43 @@ fn main() { drops.push(Drop { x: (rng.gen_range(0..WIDTH as i32)), y: (rng.gen_range(0..HEIGHT as i32)), - z: ((rng.sample(rand_distr::Geometric::new(0.2).unwrap())+1) as i32), + z: ((rng.sample(rand_distr::Geometric::new(0.2).unwrap()) + 1) as i32), }); } - let timer = Timer::new(); - let guard = timer.schedule_repeating(Duration::milliseconds(10), move || { - window.request_redraw(); - }); - guard.ignore(); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Wait; - - if let Event::RedrawRequested(_) = event { - draw(&mut drops, pixels.get_frame()); - if pixels - .render() - .map_err(|e| error!("pixels.render() failed: {}", e)) - .is_err() - { - *control_flow = ControlFlow::Exit; - return; + #[cfg(all(target_arch = "wasm32", debug_assertions))] + wasm::log_event(&log_list, &event); + + match event { + Event::RedrawRequested(_) => { + Drop::draw(&mut drops, pixels.get_frame()); + if pixels + .render() + .map_err(|e| error!("pixels.render() failed: {}", e)) + .is_err() + { + *control_flow = ControlFlow::Exit; + return; + } + } + Event::NewEvents(StartCause::Init) | Event::NewEvents(ResumeTimeReached { .. }) => { + *control_flow = ControlFlow::WaitUntil( + instant::Instant::now() + instant::Duration::from_millis(1000 / 60), + ); + window.request_redraw(); } + #[cfg(not(target_arch = "wasm32"))] + Event::MainEventsCleared => window.request_redraw(), + _ => (), } + #[cfg(not(target_arch = "wasm32"))] if input.update(&event) { - if input.key_pressed(VirtualKeyCode::Escape) || input.key_pressed(VirtualKeyCode::Q) || input.quit() { + if input.key_pressed(VirtualKeyCode::Escape) + || input.key_pressed(VirtualKeyCode::Q) + || input.quit() + { *control_flow = ControlFlow::Exit; return; } else if input.key_pressed(VirtualKeyCode::Space) { @@ -86,3 +122,74 @@ fn main() { }); } +#[cfg(target_arch = "wasm32")] +mod wasm { + use wasm_bindgen::prelude::*; + #[cfg(debug_assertions)] + use winit::event::Event; + use winit::window::Window; + + #[wasm_bindgen(start)] + pub fn run() { + console_log::init_with_level(log::Level::Debug).expect("error initializing logger"); + + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + + #[allow(clippy::main_recursion)] + super::main(); + } + + pub fn create_canvas(window: &Window) { + use winit::platform::web::WindowExtWebSys; + let canvas = window.canvas(); + + canvas.style().set_css_text( + "background-color: crimson; margin: auto; width: 100%; aspect-ratio: 4 / 3;", + ); + // Set id so that we can move it later + canvas.set_id("purple_rain"); + + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + let canvas_div = document + .get_element_by_id("purple_rain_canvas") + .unwrap_or(From::from(body)); //Get the div + canvas_div.append_child(&canvas).unwrap(); + } + + #[cfg(debug_assertions)] + pub fn create_log_list(window: &Window) -> web_sys::Element { + create_canvas(window); + + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + + let log_header = document.create_element("h2").unwrap(); + log_header.set_text_content(Some("Event Log")); + body.append_child(&log_header).unwrap(); + + let log_list = document.create_element("ul").unwrap(); + body.append_child(&log_list).unwrap(); + log_list + } + + #[cfg(debug_assertions)] + pub fn log_event(log_list: &web_sys::Element, event: &Event<()>) { + log::debug!("{:?}", event); + + // Getting access to browser logs requires a lot of setup on mobile devices. + // So we implement this basic logging system into the page to give developers an easy alternative. + // As a bonus its also kind of handy on desktop. + if let Event::WindowEvent { event, .. } = &event { + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let log = document.create_element("li").unwrap(); + log.set_text_content(Some(&format!("{:?}", event))); + log_list + .insert_before(&log, log_list.first_child().as_ref()) + .unwrap(); + } + } +}