this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1891 from PierceNg/wasm-funcsig

Improved D template

authored by

Vadim Grigoruk and committed by
GitHub
f877c5de a4f318fa

+186 -26
+4 -4
src/api/wasm.c
··· 966 966 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "font", "i(*iiiiiii)", &wasmtic_font))); 967 967 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "key", "i(i)", &wasmtic_key))); 968 968 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "keyp", "i(iii)", &wasmtic_keyp))); 969 - _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "line", "v(iiiii)", &wasmtic_line))); 969 + _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "line", "v(ffffi)", &wasmtic_line))); 970 970 // TODO: needs a lot of help for all the optional arguments 971 971 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "map", "v(iiiiiiiiii)", &wasmtic_map))); 972 972 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "memcpy", "v(iii)", &wasmtic_memcpy))); ··· 994 994 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "time", "f()", &wasmtic_time))); 995 995 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tstamp", "i()", &wasmtic_tstamp))); 996 996 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trace", "v(*i)", &wasmtic_trace))); 997 - _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tri", "v(iiiiiii)", &wasmtic_tri))); 998 - _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trib", "v(iiiiiii)", &wasmtic_trib))); 999 - _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "ttri", "v(iiiiiiiiiiiiiiiiiii)", &wasmtic_ttri))); 997 + _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "tri", "v(ffffffi)", &wasmtic_tri))); 998 + _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "trib", "v(ffffffi)", &wasmtic_trib))); 999 + _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "ttri", "v(ffffffffffffiiifffi)", &wasmtic_ttri))); 1000 1000 _ (SuppressLookupFailure (m3_LinkRawFunction (module, "env", "vbank", "i(i)", &wasmtic_vbank))); 1001 1001 1002 1002 _catch:
+10
templates/d/Makefile
··· 1 + DUB_FLAGS = --quiet --build release --compiler ldc2 --arch wasm32-unknown-unknown-wasm 2 + ifneq ($(origin WASI_SDK_PATH), undefined) 3 + override DUB_FLAGS += --config wasi 4 + endif 5 + 6 + build: 7 + dub build ${DUB_FLAGS} 8 + 9 + clean: 10 + rm -rf cart.wasm .dub
+49 -10
templates/d/README.md
··· 1 1 # D Starter Project Template 2 2 3 - This is a D TIC-80 starter template. To build the D source files: 3 + ## Pre-requisites 4 + 5 + - [LDC](https://wiki.dlang.org/LDC) 6 + - [WASI libc](https://github.com/WebAssembly/wasi-libc) 7 + 8 + ### WASI libc 9 + 10 + WASI libc's [README](https://github.com/WebAssembly/wasi-libc#usage) states 11 + that "the easiest way to get started with this is to use wask-sdk, which 12 + includes a build of WASI libc in its sysroot." 13 + 14 + Just [building WASI libc from source](https://github.com/WebAssembly/wasi-libc#building-from-source) 15 + works too, for the purpose of programming TIC-80 games in D. Let's say you 16 + install into ```$HOME/wasi-sdk/share/wasi-sysroot```, which then look like this: 4 17 5 18 ``` 6 - % ./build.sh 19 + % ls -l 20 + total 12 21 + drwxr-xr-x 10 pierce pierce 4096 Apr 24 16:19 include/ 22 + drwxr-xr-x 3 pierce pierce 4096 Apr 24 16:19 lib/ 23 + drwxr-xr-x 3 pierce pierce 4096 Apr 24 16:19 share/ 7 24 ``` 8 25 9 - To import the resulting WASM file into a cartridge: 26 + ## Files in this template 27 + 28 + - ```buildcart.sh``` - convenience script to build and run the game cartridge 29 + - ```buildwasm.sh``` - convenience script to build and run the Wasm program 30 + - ```dub.json``` - D's dub package description file 31 + - ```Makefile``` - convenience Makefile that invokes ```dub``` 32 + - ```wasmdemo.wasmp``` - TIC-80 Wasm 'script' file. Note the embedded game assets data at the end of the file. 33 + 34 + ## Building your game 35 + 36 + Define the environment variable WASI_SDK_PATH; e.g., if you installed WASI 37 + libc into ```$HOME/wasi-sdk/share/wasi-sysroot```, then ```export WASI_SDK_PATH=$HOME/wasi-sdk```. 38 + 39 + Edit ```src/main.d``` to implement your game. You are of course free to 40 + organize your code in more than one D source file. 41 + 42 + If you create sprites, map, music, etc., for your game, remember to 43 + replace the game asset data at the end of ```wasmdemo.wasmp``` with 44 + your creations. 45 + 46 + To build the Wasm file, execute ```make```. This generates ```cart.wasm``` 47 + in the current directory. To run: 10 48 11 49 ``` 12 - % tic80 --fs . 13 - tic80 prompt> load wasmdemo.wasmp 14 - tic80 prompt> import binary cart.wasm 15 - tic80 prompt> save game.tic 16 - tic80 prompt> exit 50 + % tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & run & exit' 17 51 ``` 18 52 19 - Or, on the command line: 53 + The script ```buildwasm.sh``` contains above steps as a convenience. 54 + 55 + To build a TIC-80 cartridge, first build the Wasm file, then build the 56 + cartridge file: 20 57 21 58 ``` 22 59 % tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & save game.tic & exit' 23 60 ``` 24 61 25 - Now, run ```game.tic```: 62 + You can then run your cartridge as follows: 26 63 27 64 ``` 28 65 % tic80 --fs . --cmd 'load game.tic & run & exit' 29 66 ``` 30 67 68 + The script ```buildcart.sh``` does the above steps as a convenience. 69 +
-2
templates/d/build.sh
··· 1 - #!/bin/sh 2 - (cd src; ldc2 -mtriple=wasm32-unknown-unknown-wasm -L-allow-undefined -L-no-entry -of=../cart.wasm main.d)
+6
templates/d/buildcart.sh
··· 1 + #!/bin/sh 2 + rm -f game.tic 3 + make clean 4 + make 5 + tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & save game.tic & exit' 6 + tic80 --fs . --cmd 'load game.tic & run & exit'
+4
templates/d/buildwasm.sh
··· 1 + #!/bin/sh 2 + make clean 3 + make 4 + tic80 --fs . --cmd 'load wasmdemo.wasmp & import binary cart.wasm & run & exit'
+38
templates/d/dub.json
··· 1 + { 2 + "name": "cart", 3 + "targetType": "executable", 4 + "configurations": [ 5 + { 6 + "name": "raw", 7 + "dflags": [ 8 + "-betterC" 9 + ], 10 + "lflags": [ 11 + "--strip-all", 12 + "--allow-undefined", 13 + "--stack-first", 14 + "--no-entry" 15 + ] 16 + }, 17 + { 18 + "name": "wasi", 19 + "dflags": [ 20 + "-betterC", 21 + "-Xcc", 22 + "$WASI_SDK_PATH/share/wasi-sysroot" 23 + ], 24 + "lflags": [ 25 + "--strip-all", 26 + "--allow-undefined", 27 + "--stack-first", 28 + "--no-entry", 29 + "$WASI_SDK_PATH/share/wasi-sysroot/lib/wasm32-wasi/libc.a" 30 + ] 31 + } 32 + ], 33 + "toolchainRequirements": { 34 + "dmd": "no", 35 + "gdc": "no", 36 + "ldc": ">=1.11.0" 37 + } 38 + }
+24 -1
templates/d/src/main.d
··· 1 + import std.algorithm: min, max; 1 2 import tic80; 2 3 3 4 extern(C): 5 + 6 + // From WASI libc: 7 + int snprintf(scope char* s, size_t n, scope const char* format, scope const ...); 4 8 5 9 int t, x, y; 6 10 const char* m = "HELLO WORLD FROM D!"; 11 + int r = 0; 12 + MouseData md; 7 13 8 14 void BOOT() { 9 15 t = 1; ··· 12 18 } 13 19 14 20 void TIC() { 21 + cls(13); 22 + 23 + // The standard demo. 15 24 if (btn(0) > 0) { y--; } 16 25 if (btn(1) > 0) { y++; } 17 26 if (btn(2) > 0) { x--; } 18 27 if (btn(3) > 0) { x++; } 19 28 20 - cls(13); 21 29 spr(1+t%60/30*2, x, y, null, 0, 3, 0, 0, 2, 2); 22 30 print(m, 60, 84, 15, 1, 1, 0); 23 31 t++; 32 + 33 + // Mouse example demonstrating use of libc function. 34 + mouse(&md); 35 + if (md.left) { r = r + 2; } 36 + r--; 37 + r = max(0, min(32, r)); 38 + line(md.x, 0, md.x, 136, 11); 39 + line(0, md.y, 240, md.y, 11); 40 + circ(md.x, md.y, r, 11); 41 + 42 + const BUFSIZ = 10; 43 + char[BUFSIZ] buf; 44 + char* bufptr = cast(char*)buf; 45 + snprintf(bufptr, BUFSIZ, "(%03d,%03d)", md.x, md.y); 46 + print(bufptr, 3, 3, 15, 0, 1, 1); 24 47 }
+51 -9
templates/d/src/tic80.d
··· 4 4 5 5 struct MouseData { 6 6 short x; short y; 7 - short scrollx; short scrolly; 7 + byte scrollx; byte scrolly; 8 8 bool left; bool middle; bool right; 9 9 } 10 10 11 11 const int WIDTH = 240; 12 12 const int HEIGHT = 136; 13 13 14 + // These are pointers. 15 + const FRAMEBUFFER_PTR = cast(ubyte*)0; 16 + const TILES_PTR = cast(ubyte*)0x4000; 17 + const SPRITES_PTR = cast(ubyte*)0x6000; 18 + const MAP_PTR = cast(ubyte*)0x8000; 19 + const GAMEPADS_PTR = cast(ubyte*)0xFF80; 20 + const MOUSE_PTR = cast(ubyte*)0xFF84; 21 + const KEYBOARD_PTR = cast(ubyte*)0xFF88; 22 + const SFX_STATE_PTR = cast(ubyte*)0xFF8C; 23 + const SOUND_REGISTERS_PTR = cast(ubyte*)0xFF9C; 24 + const WAVEFORMS_PTR = cast(ubyte*)0xFFE4; 25 + const SFX_PTR = cast(ubyte*)0x100E4; 26 + const MUSIC_PATTERNS_PTR = cast(ubyte*)0x11164; 27 + const MUSIC_TRACKS_PTR = cast(ubyte*)0x13E64; 28 + const SOUND_STATE_PTR = cast(ubyte*)0x13FFC; 29 + const STEREO_VOLUME_PTR = cast(ubyte*)0x14000; 30 + const PERSISTENT_MEMORY_PTR = cast(ubyte*)0x14004; 31 + const SPRITE_FLAGS_PTR = cast(ubyte*)0x14404; 32 + const SYSTEM_FONT_PTR = cast(ubyte*)0x14604; 33 + const WASM_FREE_RAM_PTR = cast(ubyte*)0x18000; 34 + 35 + // These are bounded arrays. 36 + ubyte[] FRAMEBUFFER() { return (cast(ubyte*)0)[0..16319]; } // VRAM bank 0 screen area 37 + ubyte[] TILES() { return (cast(ubyte*)0x4000)[0..8191]; } 38 + ubyte[] SPRITES() { return (cast(ubyte*)0x4000)[0..8191]; } 39 + ubyte[] MAP() { return (cast(ubyte*)0x8000)[0..32639]; } 40 + ubyte[] GAMEPADS() { return (cast(ubyte*)0xFF80)[0..3]; } 41 + ubyte[] MOUSE() { return (cast(ubyte*)0xFF84)[0..3]; } 42 + ubyte[] KEYBOARD() { return (cast(ubyte*)0xFF88)[0..3]; } 43 + ubyte[] SFX_STATE() { return (cast(ubyte*)0xFF8C)[0..15]; } 44 + ubyte[] SOUND_REGISTERS() { return (cast(ubyte*)0xFF9C)[0..71]; } 45 + ubyte[] WAVEFORMS() { return (cast(ubyte*)0xFFE4)[0..255]; } 46 + ubyte[] SFX() { return (cast(ubyte*)0x100E4)[0..4223]; } 47 + ubyte[] MUSIC_PATTERNS() { return (cast(ubyte*)0x11164)[0..11519]; } 48 + ubyte[] MUSIC_TRACKS() { return (cast(ubyte*)0x13E64)[0..407]; } 49 + ubyte[] SOUND_STATE() { return (cast(ubyte*)0x13FFC)[0..3]; } 50 + ubyte[] STEREO_VOLUME() { return (cast(ubyte*)0x14000)[0..3]; } 51 + ubyte[] PERSISTENT_MEMORY() { return (cast(ubyte*)0x14004)[0..1023]; } 52 + ubyte[] SPRITE_FLAGS() { return (cast(ubyte*)0x14404)[0..511]; } 53 + ubyte[] SYSTEM_FONT() { return (cast(ubyte*)0x14604)[0..2047]; } 54 + ubyte[] WASM_FREE_RAM() { return (cast(ubyte*)0x18000)[0..163839]; } // 160kb 55 + 14 56 int btn(int id); 15 57 bool btnp(int id, int hold, int period); 16 58 void circ(int x, int y, int radius, int color); ··· 25 67 bool fset(int id, ubyte flag, bool value); 26 68 bool key(int keycode); 27 69 bool keyp(int keycode, int hold, int period); 28 - void line(int x0, int y0, int x1, int y1, int color); 70 + void line(float x0, float y0, float x1, float y1, byte color); 29 71 void map(int x, int y, int w, int h, int sx, int sy, ubyte transcolors, int colorcount, int scale, int remap); 30 72 void memcpy(uint copyto, uint copyfrom, uint length); 31 73 void memset(uint addr, ubyte value, uint length); ··· 34 76 void mset(int x, int y, bool value); 35 77 void music(int track, int frame, int row, bool loop, bool sustain, int tempo, int speed); 36 78 ubyte peek(int addr, int bits); 37 - int print(const char* txt, int x, int y, int color, int fixed, int scale, int alt); 38 79 ubyte peek4(uint addr4); 39 80 ubyte peek2(uint addr2); 40 81 ubyte peek1(uint bitaddr); 41 82 void pix(int x, int y, int color); 42 83 uint pmem(uint index, uint value); 43 - void poke(uint addr, ubyte value, int bits); 44 - void poke4(uint addr4, ubyte value); 45 - void poke2(uint addr2, ubyte value); 46 - void poke1(uint bitaddr, ubyte value); 84 + void poke(int addr, byte value, byte bits); 85 + void poke4(int addr4, byte value); 86 + void poke2(int addr2, byte value); 87 + void poke1(int bitaddr, byte value); 88 + int print(const char* txt, int x, int y, int color, int fixed, int scale, int alt); 47 89 void rect(int x, int y, int w, int h, int color); 48 90 void rectb(int x, int y, int w, int h, int color); 49 91 void reset(); ··· 51 93 void spr(int id, int x, int y, uint* transcolors, uint colorcount, int scale, int flip, int rotate, int w, int h); 52 94 void sync(int mask, int bank, bool tocart); 53 95 void trace(const char* txt, int color); 54 - void textri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int texsrc, ubyte transcolors, int colorcount, float z1, float z2, float z3, bool persp); 96 + void ttri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int texsrc, ubyte transcolors, int colorcount, float z1, float z2, float z3, bool persp); 55 97 void tri(float x1, float y1, float x2, float y2, float x3, float y3, int color); 56 98 void trib(float x1, float y1, float x2, float y2, float x3, float y3, int color); 57 - int time(); 99 + float time(); 58 100 int tstamp(); 59 101 int vbank(int bank); 60 102