-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #712 from tjpalmer/grain
Add Grain language support
- Loading branch information
Showing
14 changed files
with
913 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.