port tunnel example to C

This commit is contained in:
2022-01-02 15:22:11 +01:00
parent 88ee0e1bef
commit 20365a0dd0
2 changed files with 37 additions and 0 deletions

31
examples/c/cart.c Normal file
View File

@@ -0,0 +1,31 @@
#define IMPORT(MODULE, NAME) __attribute__((import_module(MODULE), import_name(NAME)))
IMPORT("env", "atan2") extern float atan2(float, float);
IMPORT("env", "time") extern float time();
int ftoi(float v) {
return __builtin_wasm_trunc_s_i32_f32(v);
}
float sqrt(float v) {
return __builtin_sqrt(v);
}
#define FRAMEBUFFER ((unsigned char*)120)
void upd() {
int i = 0;
for( ;; ) {
float t = time() * 63.0f;
float x = (float)(i % 320 - 160);
float y = (float)(i / 320 - 120);
float d = 40000.0f / sqrt(x * x + y * y + 1.0f);
float u = atan2(x, y) * 512.0f / 3.141f;
unsigned char c = (unsigned char)(ftoi(d + t * 2.0f) ^ ftoi(u + t)) >> 4;
FRAMEBUFFER[i] = c;
i += 1;
if(i >= 320*240) break;
}
}