diff options
| -rwxr-xr-x | build.sh | 10 | ||||
| -rw-r--r-- | graphics.c | 32 | ||||
| -rwxr-xr-x | graphics.wasm | bin | 0 -> 834 bytes | |||
| -rw-r--r-- | index.html | 42 |
4 files changed, 84 insertions, 0 deletions
diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..79d0e2c --- /dev/null +++ b/build.sh @@ -0,0 +1,10 @@ +#!/bin/sh +printf 'graphics.c\n' +clang \ + --target=wasm32 \ + -O2 \ + -nostdlib \ + -Wl,--no-entry \ + -Wl,--export-all \ + -o graphics.wasm \ + graphics.c diff --git a/graphics.c b/graphics.c new file mode 100644 index 0000000..66a00bc --- /dev/null +++ b/graphics.c @@ -0,0 +1,32 @@ +typedef unsigned int u32; +typedef unsigned char u8; + +#define WIDTH 600 +#define HEIGHT 600 +u32 BUFFER[WIDTH * HEIGHT]; + +extern u8 __heap_base; +u32 BumpPointer = (u32)&__heap_base; + +void* Malloc(u32 Size) +{ + u32 Result = BumpPointer; + Result += Size; + return (void *)Result; +} + +void Free(u32 Size) +{ + BumpPointer -= Size; +} + +void go(u32 *Pixels, int Width, int Height) { + for(int Y = 0; Y < Height; Y++) + { + for(int X = 0; X < Width; X++) + { + u32 Color = 0xFFFF0000; + Pixels[Y*Width + X] = Color; + } + } +} diff --git a/graphics.wasm b/graphics.wasm Binary files differnew file mode 100755 index 0000000..5da2edf --- /dev/null +++ b/graphics.wasm diff --git a/index.html b/index.html new file mode 100644 index 0000000..d2ab090 --- /dev/null +++ b/index.html @@ -0,0 +1,42 @@ +<!DOCTYPE html> +<html> + <head> + <script type="module"> + async function init() { + const { instance } = await WebAssembly.instantiateStreaming( + fetch("./graphics.wasm") + ); + + const width = 600; + const height = 600; + const bytes_per_pixel = 4; + + const canvas = document.getElementById("graphics-canvas"); + canvas.width = width; + canvas.height = height; + + const buffer_address = instance.exports.BUFFER.value; + const image = new ImageData( + new Uint8ClampedArray( + instance.exports.memory.buffer, + buffer_address, + bytes_per_pixel * width * height, + ), + width, + height, + ); + + const ctx = canvas.getContext("2d"); + + instance.exports.go(buffer_address, width, height); + + ctx.putImageData(image, 0, 0); + } + + init(); + </script> + </head> + <body> + <canvas id="graphics-canvas"></canvas> + </body> +</html> |
