Skip to content

Commit

Permalink
Switch to trunk for web builds
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Mar 24, 2022
1 parent 7bb3bff commit e48e9c2
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 150 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

18 changes: 9 additions & 9 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
overwrite: true

build-linux:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest

steps:
- name: Get tag
Expand Down Expand Up @@ -137,20 +137,20 @@ jobs:
override: true
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install pkg-config libx11-dev libasound2-dev libudev-dev
- name: Install cargo-make
- name: Install trunk
run: |
cargo install cargo-make
cargo install --locked trunk
- name: Add wasm target
run: |
rustup target add wasm32-unknown-unknown
- name: Build Release
run: |
cargo make release
- name: optimize Wasm
uses: NiklasEi/wasm-opt@v1
with:
file: build/web/target/wasm_bg.wasm
output: build/web/target/wasm_bg.wasm
trunk build --release
# - name: optimize Wasm
# uses: NiklasEi/wasm-opt@v1
# with:
# file: build/web/target/wasm_bg.wasm
# output: build/web/target/wasm_bg.wasm
- name: Zip release
uses: vimtor/action-zip@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ target/
.idea/
.DS_Store

build/web/
dist/
40 changes: 0 additions & 40 deletions Makefile.toml

This file was deleted.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Template for a Game using the awesome [Bevy engine][bevy] featuring out of the b
_Since Bevy is in heavy development, there regularly are unpublished new features or bug fixes. If you like living on the edge, you can use the branch `bevy_main` of this template to be close to the current state of Bevy's main branch_

# What does this template give you?
* basic setup with a slim main function and your game as a Bevy plugin in a library
* small example game (*warning: biased; e.g., split into a lot of plugins and using `bevy_kira_audio` for sound*)
* easy setup for running the web (`cargo run --target wasm32-unknown-unknown`) and the native version (`cargo run`)
* easy setup for running the web build using [trunk] (`trunk serve`)
* run the native version with `cargo run`
* workflow for GitHub actions creating releases for Windows, Linux, macOS, and Web (Wasm) ready for distribution
* push a tag in the form of `v[0-9]+.[0-9]+.[0-9]+*` (e.g. `v1.1.42`) to trigger the flow

Expand All @@ -17,10 +17,10 @@ _Since Bevy is in heavy development, there regularly are unpublished new feature
3. [Update the icons as described below](#updating-the-icons)
4. Start coding :tada:
* Start the native app: `cargo run`
* Start the web build: `cargo run --target wasm32-unknown-unknown`
* requires [`wasm-server-runner`]: `cargo install wasm-server-runner`
* Start the web build: `trunk serve`
* requires [trunk]: `cargo install --locked trunk`
* requires `wasm32-unknown-unknown` target: `rustup target add wasm32-unknown-unknown`
* this will serve your app on a free port
* this will serve your app on `8080` and automatically rebuild + reload it after code changes

You should keep the `credits` directory up to date. The release workflow automatically includes the directory in every build.

Expand All @@ -47,3 +47,4 @@ This project is licensed under [CC0 1.0 Universal](LICENSE) except some content
[firefox-sound-issue]: https://github.com/NiklasEi/bevy_kira_audio/issues/9
[Bevy Cheat Book]: https://bevy-cheatbook.github.io/introduction.html
[`wasm-server-runner`]: https://github.com/jakobhellermann/wasm-server-runner
[trunk]: https://trunkrs.dev/
62 changes: 62 additions & 0 deletions build/web/sound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Insert hack to make sound autoplay on Chrome as soon as the user interacts with the tab:
// https://developers.google.com/web/updates/2018/11/web-audio-autoplay#moving-forward

// the following function keeps track of all AudioContexts and resumes them on the first user
// interaction with the page. If the function is called and all contexts are already running,
// it will remove itself from all event listeners.
(function () {
// An array of all contexts to resume on the page
const audioContextList = [];

// An array of various user interaction events we should listen for
const userInputEventNames = [
"click",
"contextmenu",
"auxclick",
"dblclick",
"mousedown",
"mouseup",
"pointerup",
"touchend",
"keydown",
"keyup",
];

// A proxy object to intercept AudioContexts and
// add them to the array for tracking and resuming later
self.AudioContext = new Proxy(self.AudioContext, {
construct(target, args) {
const result = new target(...args);
audioContextList.push(result);
return result;
},
});

// To resume all AudioContexts being tracked
function resumeAllContexts(_event) {
let count = 0;

audioContextList.forEach((context) => {
if (context.state !== "running") {
context.resume();
} else {
count++;
}
});

// If all the AudioContexts have now resumed then we unbind all
// the event listeners from the page to prevent unnecessary resume attempts
// Checking count > 0 ensures that the user interaction happens AFTER the game started up
if (count > 0 && count === audioContextList.length) {
userInputEventNames.forEach((eventName) => {
document.removeEventListener(eventName, resumeAllContexts);
});
}
}

// We bind the resume function for each user interaction
// event on the page
userInputEventNames.forEach((eventName) => {
document.addEventListener(eventName, resumeAllContexts);
});
})();
16 changes: 16 additions & 0 deletions build/web/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
body {
background: linear-gradient(
135deg,
white 0%,
white 49%,
black 49%,
black 51%,
white 51%,
white 100%
) repeat;
background-size: 20px 20px;
margin: 0;
}
canvas {
background-color: white;
}
105 changes: 12 additions & 93 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,96 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<style>
body {
background: linear-gradient(
135deg,
white 0%,
white 49%,
black 49%,
black 51%,
white 51%,
white 100%
) repeat;
background-size: 20px 20px;
margin: 0;
}
canvas {
background-color: white;
}
</style>
<title>Bevy game</title> <!-- ToDo -->
</head>
<body>
<script>
// Insert hack to make sound autoplay on Chrome as soon as the user interacts with the tab:
// https://developers.google.com/web/updates/2018/11/web-audio-autoplay#moving-forward

// the following function keeps track of all AudioContexts and resumes them on the first user
// interaction with the page. If the function is called and all contexts are already running,
// it will remove itself from all event listeners.
(function () {
// An array of all contexts to resume on the page
const audioContextList = [];

// An array of various user interaction events we should listen for
const userInputEventNames = [
"click",
"contextmenu",
"auxclick",
"dblclick",
"mousedown",
"mouseup",
"pointerup",
"touchend",
"keydown",
"keyup",
];

// A proxy object to intercept AudioContexts and
// add them to the array for tracking and resuming later
self.AudioContext = new Proxy(self.AudioContext, {
construct(target, args) {
const result = new target(...args);
audioContextList.push(result);
return result;
},
});

// To resume all AudioContexts being tracked
function resumeAllContexts(_event) {
let count = 0;

audioContextList.forEach((context) => {
if (context.state !== "running") {
context.resume();
} else {
count++;
}
});

// If all the AudioContexts have now resumed then we unbind all
// the event listeners from the page to prevent unnecessary resume attempts
// Checking count > 0 ensures that the user interaction happens AFTER the game started up
if (count > 0 && count === audioContextList.length) {
userInputEventNames.forEach((eventName) => {
document.removeEventListener(eventName, resumeAllContexts);
});
}
}

// We bind the resume function for each user interaction
// event on the page
userInputEventNames.forEach((eventName) => {
document.addEventListener(eventName, resumeAllContexts);
});
})();
</script>
<script type="module">
// load the actual game
import init from './target/wasm.js'
init();
</script>
</body>
<head>
<meta charset="utf-8"/>
<title>Bevy game</title> <!-- ToDo -->
<link data-trunk rel="copy-dir" href="assets"/>
<link data-trunk rel="copy-dir" href="credits"/>
<link data-trunk rel="copy-file" href="build/windows/icon.ico"/>
<link rel="icon" href="icon.ico">
<link data-trunk rel="inline" href="build/web/styles.css"/>
</head>
<body>
<link data-trunk rel="inline" href="build/web/sound.js"/>
</body>
</html>

0 comments on commit e48e9c2

Please sign in to comment.