Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<del> #2644

Closed
wants to merge 3 commits into from
Closed

<del> #2644

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
439 changes: 3 additions & 436 deletions .github/workflows/build.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This directory contains starter projects (and libs) for the compiled languages we support via `script: wasm`. Currently this list includes:

- Nim
- Zig (low-level)
- D (low-level)

Expand All @@ -11,7 +12,6 @@ We could easily support these as well, with contributor help:
- C++
- Go
- Nelua
- Nim
- Odin
- Rust
- Wat
Expand Down
10 changes: 10 additions & 0 deletions templates/nim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Nim Starter Project Template

This is a Nim / TIC-80 starter template. To build wasm binary and import it into
the 'cart.tic' file, ensure you have a Nim compiler, Wasi-SDK and Tic-80 on your system and run:

```
nimble buildcart
```

Run `nimble tasks` command to see more build options.
38 changes: 38 additions & 0 deletions templates/nim/cart.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Package

version = "0.1.0"
author = "archargelod"
description = "tic80 wasm template for Nim language"
license = "MIT"
srcDir = "src"


# Dependencies

requires "nim >= 2.0.0"

# Tasks
import std/strformat

let pwd = getCurrentDir()

task wasmbuild, "Build wasm binary (debug)":
exec("nim c -o:cart.wasm src/cart")

task wasmrelease, "Build wasm binary (release)":
exec("nim c -d:release -o:cart.wasm src/cart")

task buildcart, "Build optimized wasm binary and import it to tic80 cart":
rmFile("cart.tic")
exec("nim c -d:release -o:cart.wasm src/cart")
exec(&"tic80 --cli --fs=\"{pwd}\" --cmd=\"load src/cart.tic & import binary cart.wasm & save cart.tic & exit\"")

task runcart, "Build optimized wasm binary and run it with tic80":
rmFile("cart.tic")
exec("nim c -d:release -o:cart.wasm src/cart")
exec(&"tic80 --skip --fs=\"{pwd}\" --cmd=\"load src/cart.tic & import binary cart.wasm & save cart.tic & run\"")

task debugcart, "Build wasm binary in debug mode and run it with tic80":
rmFile("cart.tic")
exec("nim c -o:cart.wasm src/cart")
exec(&"tic80 --skip --fs=\"{pwd}\" --cmd=\"load src/cart.tic & import binary cart.wasm & save cart.tic & run\"")
52 changes: 52 additions & 0 deletions templates/nim/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import std/os

let wasi = getEnv("WASI_SDK_PATH")
if wasi == "" and not defined(nimsuggest):
echo ""
echo "Error:"
echo "Download the WASI SDK (https://github.com/WebAssembly/wasi-sdk) and set the $WASI_SDK_PATH environment variable!"
echo ""
quit(-1)

switch("cpu", "wasm32")
switch("cc", "clang")
switch("threads", "off")

# ARC is much more embedded-friendly
switch("gc", "arc")
# Treats defects as errors, results in smaller binary size
switch("panics", "on")

# Use the common ANSI C target
switch("os", "any")
# Needed for os:any
switch("define", "posix")

# WASM has no signal handlers
switch("define", "noSignalHandler")
# The only entrypoints are start and update
switch("noMain")
# Use malloc instead of Nim's memory allocator, makes binary size much smaller
switch("define", "useMalloc")

switch("clang.exe", wasi / "bin" / "clang")
switch("clang.linkerexe", wasi / "bin" / "clang")

switch("passC", "--sysroot=" & (wasi / "share" / "wasi-sysroot"))

switch("passL", "-Wl,-zstack-size=8192,--no-entry,--import-memory -mexec-model=reactor -Wl,--initial-memory=262144,--max-memory=262144,--global-base=98304")

when not defined(release):
switch("assertions", "off")
switch("checks", "off")
switch("stackTrace", "off")
switch("lineTrace", "off")
switch("lineDir", "off")
switch("debugger", "native")
# This is needed to remove all unused Nim functions that will in turn import
# WASI stuff that is not available in WASM-4
switch("passL", "-Wl,--gc-sections")
else:
switch("opt", "size")
switch("passC", "-flto")
switch("passL", "-Wl,--strip-all,--gc-sections,--lto-O3")
Binary file added templates/nim/demo/bunny.tic
Binary file not shown.
84 changes: 84 additions & 0 deletions templates/nim/demo/bunnymark.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import std/[random]
import ../cart/tic80

let ToolbarHeight = 6

type
Bunny = object
x, y: float32
width: int32 = 26
height: int32 = 32
speedX, speedY: float32
sprite: int32
FPS = object
value, frames: int32
lastTime: float32

proc getValue(f: var FPS): int32 =
if time() - f.lastTime <= 1000:
inc f.frames
else:
f.value = f.frames
f.frames = 0
f.lastTime = time()
f.value

proc newBunny: Bunny =
Bunny(
x: rand(0 .. Width).float32,
y: rand(0 .. Height).float32,
width: 26,
height: 32,
speedX: rand(-100.0 .. 100.0) / 60,
speedY: rand(-100.0 .. 100.0) / 60,
sprite: 1,
)

proc draw(b: Bunny) =
spr(b.sprite, b.x.int32, b.y.int32, transColor=1, w=4, h=4)

proc update(b: var Bunny) =
b.x += b.speedX
b.y += b.speedY

if b.x.int32 + b.width > Width:
b.x = float32(Width - b.width)
b.speedX *= -1
if b.x < 0:
b.x = 0
b.speedX *= -1

if b.y.int32 + b.height > Height:
b.y = float32(Height - b.height)
b.speedY *= -1
if b.y < 0:
b.y = 0
b.speedY *= -1

randomize(tstamp().int64)

var fps: FPS
var bunnies: seq[Bunny]

proc TIC {.exportWasm.} =
cls(13)

if btn(P1_Up):
for _ in 1..5:
bunnies.add newBunny()

if btn(P1_Down):
for _ in 1..5:
if bunnies.len > 0:
bunnies.del bunnies.high

for bunny in bunnies.mitems:
bunny.update()

for bunny in bunnies.mitems:
bunny.draw()

rect(0, 0, Width, ToolbarHeight, 0)
print("Bunnies: " & $bunnies.len, 1, 0, 11, false, 1, false)
print("FPS: " & $fps.getValue(), Width div 2, 0, 11, false, 1, false)

17 changes: 17 additions & 0 deletions templates/nim/src/cart.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cart/tic80

var
t = 1
x = 96
y = 24

proc TIC {.exportWasm.} =
cls(Color13)

if btn(P1_Up): dec y
if btn(P1_Down): inc y
if btn(P1_Left): dec x
if btn(P1_Right): inc x

spr(1 + t mod 60 div 30 * 2, x, y, transColor=Color14, scale=3, w=2, h=2)
print("Hello from Nim!", 84, 84)
Binary file added templates/nim/src/cart.tic
Binary file not shown.
Loading
Loading