Skip to content

Commit

Permalink
Merge pull request #712 from tjpalmer/grain
Browse files Browse the repository at this point in the history
Add Grain language support
  • Loading branch information
aduros authored Jun 2, 2024
2 parents f40ec0b + d58e9d9 commit 5c1f280
Show file tree
Hide file tree
Showing 14 changed files with 913 additions and 2 deletions.
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"

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

0 comments on commit 5c1f280

Please sign in to comment.