💾 Archived View for thingvellir.net › toys › applet › tunnel.c captured on 2023-03-20 at 17:53:29.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

➡️ Next capture (2024-07-09)

-=-=-=-=-=-=-

/* 'tunnel' wasm applet demo, Ada <sarahsooup@protonmail.com> 2022, CC0-1.0 */
#include <stdint.h>

static uint8_t pixel(uint32_t x, uint32_t y, float t);

/*
 * applet section
 */
static uint32_t ticks = 0;
static uint32_t fb[256 * 256] = {0};

#define IMPORT(name) \
	[[clang::import_module("applet"), clang::import_name(name)]]

IMPORT("log") void ffi_log(const char *ptr, uint32_t len);
IMPORT("set_framerate") void ffi_framerate(uint32_t fps);
IMPORT("quit") void ffi_quit(uint32_t code);
IMPORT("sin") float ffi_sin(float x);
IMPORT("cos") float ffi_cos(float x);
IMPORT("atan2") float ffi_atan2(float y, float x);

uint32_t *applet_fbptr(void) { return fb; }
uint32_t applet_fbsize(void) { return (256 << 16) | 256; }
void applet_init(void) { ffi_framerate(30); }
void applet_input(uint32_t type, int arg1, int arg2) {}

void applet_render(void) {
	float t = ticks / 30.0f;
	for (uint32_t x = 0; x < 256; x++) {
		for (uint32_t y = 0; y < 256; y++) {
			uint32_t pos = (y * 256) + x;
			uint8_t val = pixel(x, y, t);
			fb[pos] = (0xFF << 24) | (val << 16) | (val << 8) | val;
		}
	}
	ticks += 1;
}

/*
 * demo section
 */
static uint8_t pixel(uint32_t px, uint32_t py, float t) {
	float x = (px - 128) / 128.0f;
	float y = (py - 128) / 128.0f;
	float r = __builtin_sqrt((x*x) + (y*y)) - t;
	float phi = ffi_atan2(y, x);

	return (uint8_t)(r * 256.0);
}