Skip to content
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
1 change: 1 addition & 0 deletions cli/assets/templates/grain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
9 changes: 9 additions & 0 deletions cli/assets/templates/grain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build:
grain compile --release --elide-type-info \
--import-memory --use-start-section --memory-base 8192 \
--initial-memory-pages 1 --maximum-memory-pages 1 \
--wasi-polyfill bindings/wasm4-wasi-polyfill.gr -I bindings \
app/main.gr -o cart.wasm

clean:
find -name '*.wasm' | xargs rm
15 changes: 15 additions & 0 deletions cli/assets/templates/grain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Grain bindings for WASM-4

Requires grain 0.6 or higher. Tested with Grain 0.6.3. For fast compiling, be sure to [build Grain from source](https://grain-lang.org/docs/getting_grain#Building-Grain-from-Source).

For original template source, see:

- https://github.com/ospencer/wasm4-gr

Tips and source here are copied from there.

## Tips to fit within 64k

This limitation is part of the fun. Grain's extensive `Number` type brings in quite a bit of support code, so avoiding it (and standard libraries that depend on it) is the best way to keep the cartridge size small. This includes the math operations provided by `Pervasives`, like `+` and `==`, but conveniently, the WASM-4 API uses the `Uint8` and `Uint16` types. The `hello-world.gr` example is only 7k, and even with a number of sprites and a good chunk of music data, the `music.gr` example is 21k.

Good luck and have fun!
49 changes: 49 additions & 0 deletions cli/assets/templates/grain/app/main.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module HelloWorld

from "wasm4" include Wasm4 as W4
from "uint8" include Uint8
use Uint8.{ (+), (-), (&), (>), (==) }

let smiley = b"\xc3\x81\x24\x24\x00\x24\x99\xc3"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested a variation of this example using png2src output instead of the manual things here, and it worked for me.

The original example here also worked for me. I used a manually built Grain 0.6.3.


let itsDangerous = "IT'S DANGEROUS\n TO GO ALONE!"

let mut x = 76us
let mut y = 76us

let mut frame = 0us

provide let update = () => {
if (frame == 108us) {
frame = 0us
}

// Load the gamepad
let gamepad = W4.gamepad1()

// Move the player location
if ((gamepad & W4._BUTTON_LEFT) > 0us) {
x -= 1us
}
if ((gamepad & W4._BUTTON_RIGHT) > 0us) {
x += 1us
}
if ((gamepad & W4._BUTTON_UP) > 0us) {
y -= 1us
}
if ((gamepad & W4._BUTTON_DOWN) > 0us) {
y += 1us
}

// Swap to color 2
W4.drawColors(2uS)
// Write the text
W4.text(itsDangerous, 24us, 50us)

// Swap to color 3
W4.drawColors(3uS)
// Draw the sprite
W4.blit(smiley, x, y, 8us, 8us, W4._BLIT_1BPP)

frame += 1us
}
Loading