|
|
|
@ -6,20 +6,22 @@ wasm: wasm-life-1/game.wat
|
|
|
|
|
unlisted: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
Lately in doing research on WebAssembly I've been lookig around for examples
|
|
|
|
|
Lately in doing research on WebAssembly I've been looking around for examples
|
|
|
|
|
of things implemented in it, and I've come across several blog posts based on
|
|
|
|
|
the fantastic *Rust And WebAssembly* tutorial for
|
|
|
|
|
[Conway's Game of Life](https://rustwasm.github.io/docs/book/game-of-life/introduction.html).
|
|
|
|
|
|
|
|
|
|
And I mean no shade towards the folks who wrote those, but I feel it's mildly
|
|
|
|
|
disingenuous to say that those are "Conway's Game of Life in WebAssembly" when
|
|
|
|
|
the actual game code was written entirely in Rust. WebAssembly is in that case
|
|
|
|
|
really no more than a compilation target, not actually the language used.
|
|
|
|
|
the actual game code was written entirely in Rust. In those sorts of projects
|
|
|
|
|
WebAssembly is really no more than a compilation target, not actually the
|
|
|
|
|
language used!
|
|
|
|
|
|
|
|
|
|
So of course I knew what I had to do . . .
|
|
|
|
|
|
|
|
|
|
Welcome to Conway's Game of Life, *actually implemented* in WebAssembly:
|
|
|
|
|
|
|
|
|
|
<noscript>[If you enable Javascript, you'll see a game board here]</noscript>
|
|
|
|
|
<canvas
|
|
|
|
|
id="game"
|
|
|
|
|
width="800"
|
|
|
|
@ -34,7 +36,7 @@ Welcome to Conway's Game of Life, *actually implemented* in WebAssembly:
|
|
|
|
|
|
|
|
|
|
What you're looking at here is Conway's Game of Life where ***all of the game code***
|
|
|
|
|
is written in pure, raw, not-a-compiler-in-sight WebAssembly. Its performance is
|
|
|
|
|
pretty comparable to the Rust versions, and I'm glad to say the code for it isn't
|
|
|
|
|
pretty comparable to the Rust versions I've found, and I'm glad to say the code for it isn't
|
|
|
|
|
even that much of a mess!
|
|
|
|
|
|
|
|
|
|
Let's dive into how it works together, shall we?
|
|
|
|
@ -255,7 +257,7 @@ be a bit verbose:
|
|
|
|
|
i32.add
|
|
|
|
|
local.tee $row
|
|
|
|
|
|
|
|
|
|
;; loop back if less than heeight
|
|
|
|
|
;; loop back if less than height
|
|
|
|
|
global.get $boardHeight
|
|
|
|
|
i32.lt_s
|
|
|
|
|
br_if $rows
|
|
|
|
@ -457,3 +459,13 @@ the *entire* board each time. But on anything up to about a 400x300 grid
|
|
|
|
|
this was staying at roughly 5ms per frame on my machine, which should be
|
|
|
|
|
suitable for keeping about 60 frames per second - particularly if I can
|
|
|
|
|
optimize the board update function a bit as well.
|
|
|
|
|
|
|
|
|
|
## Final thoughts
|
|
|
|
|
|
|
|
|
|
So there it is! A Conway's Game of Life implementation actually done in
|
|
|
|
|
real WebAssembly. I hope you enjoyed this brief look into what goes into
|
|
|
|
|
writing this sort of algorithm in the language, and more than anything I
|
|
|
|
|
hope you appreciate your compilers for all the fantastic work they do for
|
|
|
|
|
you.
|
|
|
|
|
|
|
|
|
|
As promised the full source files used in this post are linked below.
|
|
|
|
|