Skip to content
Open
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
11 changes: 3 additions & 8 deletions cli/assets/templates/d/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
DUB_FLAGS = --quiet --arch wasm32-unknown-unknown-wasm --build release
ifneq ($(origin WASI_SDK_PATH), undefined)
override DUB_FLAGS += --config wasi
endif
.PHONY: build clean

# Just rebuild every time because it's fast.
# cart.wasm: Makefile dub.json $(wildcard source/*.d)
build:
dub build ${DUB_FLAGS}
./build

clean:
rm -rf cart.wasm .dub
rm -rf cart.wasm cart.o
31 changes: 31 additions & 0 deletions cli/assets/templates/d/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/env sh
set -e

## A WASM-4 project should look like this:
## +- Folder -------+
## | source/app.d |
## | source/wasm4.d |
## | build |
## +----------------+

output="cart.wasm"
source="source"
files="source/app.d"
flags=""

cd "$(dirname "$0")"
ldc2 -of=$output \
$files $flags -I=$source \
-i -betterC \
--mtriple=wasm32 \
--checkaction=halt \
--d-version=JokaSmallFootprint \
--d-version=JokaMathStubs \
-L--export=update \
-L--strip-all \
-L--no-entry \
-L--stack-first \
-L--import-memory \
-L--initial-memory=65536 \
-L--max-memory=65536 \
-L-zstack-size=14752
33 changes: 33 additions & 0 deletions cli/assets/templates/d/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@echo off
setlocal enabledelayedexpansion

:: A WASM-4 project should look like this:
:: +- Folder -------+
:: | source\app.d |
:: | source\wasm4.d |
:: | build.bat |
:: +----------------+

set "output=cart.wasm"
set "source=source"
set "files=source\app.d"
set "flags="

:: Change directory to where the script is located
cd /d "%~dp0"

ldc2 -of=%output% ^
%files% %flags% -I=%source% ^
-i -betterC ^
--mtriple=wasm32 ^
--checkaction=halt ^
--d-version=JokaSmallFootprint ^
--d-version=JokaMathStubs ^
-L--export=update ^
-L--strip-all ^
-L--no-entry ^
-L--stack-first ^
-L--import-memory ^
-L--initial-memory=65536 ^
-L--max-memory=65536 ^
-L-zstack-size=14752
41 changes: 0 additions & 41 deletions cli/assets/templates/d/dub.json

This file was deleted.

91 changes: 61 additions & 30 deletions cli/assets/templates/d/source/wasm4.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
module wasm4;

extern(C):
private {
version (LDC) {
import ldc = ldc.attributes;
private alias llvmAttr = ldc.llvmAttr;
} else {
private struct llvmAttr { immutable(char)[] a, b; }
}

enum wasm4 = llvmAttr("wasm-import-module", "env");
auto importName(immutable(char)[] name) => llvmAttr("wasm-import-name", name);
}

// ┌───────────────────────────────────────────────────────────────────────────┐
// │ │
Expand Down Expand Up @@ -50,47 +60,62 @@ enum systemHideGamepadOverlay = 2;
// │ │
// └───────────────────────────────────────────────────────────────────────────┘

/** Copies pixels to the framebuffer. */
void blit(const ubyte* data, int x, int y, uint width, uint height, uint flags);
extern(C) nothrow @nogc @wasm4 {
/** Copies pixels to the framebuffer. */
@importName("blit")
void blit(const ubyte* data, int x, int y, uint width, uint height, uint flags);

/** Copies a subregion within a larger sprite atlas to the framebuffer. */
void blitSub(
const ubyte* data, int x, int y, uint width, uint height,
uint srcX, uint srcY, uint stride, uint flags,
);
/** Copies a subregion within a larger sprite atlas to the framebuffer. */
@importName("blitSub")
void blitSub(
const ubyte* data, int x, int y, uint width, uint height,
uint srcX, uint srcY, uint stride, uint flags,
);
}

enum blit2Bpp = 1;
enum blit1Bpp = 0;
enum blitFlipX = 2;
enum blitFlipY = 4;
enum blitRotate = 8;

/** Draws a line between two points. */
void line(int x1, int y1, int x2, int y2);
extern(C) nothrow @nogc @wasm4 {
/** Draws a line between two points. */
@importName("line")
void line(int x1, int y1, int x2, int y2);

/** Draws a horizontal line. */
void hline(int x, int y, uint len);
/** Draws a horizontal line. */
@importName("hline")
void hline(int x, int y, uint len);

/** Draws a vertical line. */
void vline(int x, int y, uint len);
/** Draws a vertical line. */
@importName("vline")
void vline(int x, int y, uint len);

/** Draws an oval (or circle). */
void oval(int x, int y, uint width, uint height);
/** Draws an oval (or circle). */
@importName("oval")
void oval(int x, int y, uint width, uint height);

/** Draws a rectangle. */
void rect(int x, int y, uint width, uint height);
/** Draws a rectangle. */
@importName("rect")
void rect(int x, int y, uint width, uint height);

// /** Draws text using the built-in system font. */
void text(const char* text, int x, int y);
// /** Draws text using the built-in system font. */
@importName("text")
void text(const char* text, int x, int y);
}

// ┌───────────────────────────────────────────────────────────────────────────┐
// │ │
// │ Sound Functions │
// │ │
// └───────────────────────────────────────────────────────────────────────────┘

/** Plays a sound tone. */
void tone(uint frequency, uint duration, uint volume, uint flags);
extern(C) nothrow @nogc @wasm4 {
/** Plays a sound tone. */
@importName("tone")
void tone(uint frequency, uint duration, uint volume, uint flags);
}

enum tonePulse1 = 0;
enum tonePulse2 = 1;
Expand All @@ -110,14 +135,20 @@ enum toneNoteMode = 64;
// │ │
// └───────────────────────────────────────────────────────────────────────────┘

/** Reads up to `size` bytes from persistent storage into the pointer `destPtr`. */
uint diskr(void* dest, uint size);
extern(C) nothrow @nogc @wasm4 {
/** Reads up to `size` bytes from persistent storage into the pointer `destPtr`. */
@importName("diskr")
uint diskr(void* dest, uint size);

/** Writes up to `size` bytes from the pointer `srcPtr` into persistent storage. */
uint diskw(const void* src, uint size);
/** Writes up to `size` bytes from the pointer `srcPtr` into persistent storage. */
@importName("diskw")
uint diskw(const void* src, uint size);

/** Prints a message to the debug console. */
void trace(const char* str);
/** Prints a message to the debug console. */
@importName("trace")
void trace(const char* str);

/** Prints a message to the debug console. */
void tracef(const char* fmt, ...);
/** Prints a message to the debug console. */
@importName("tracef")
void tracef(const char* fmt, ...);
}