import scale var from js, more usefull exemple

This commit is contained in:
nemo 2024-10-14 21:49:59 +02:00
parent 9db10d6064
commit 01acd62776
2 changed files with 11 additions and 8 deletions

14
app.c
View File

@ -8,6 +8,8 @@ const unsigned int width = 800;
const unsigned int height = 600; const unsigned int height = 600;
unsigned int BUFFER[width * height]; unsigned int BUFFER[width * height];
int scale(void);
typedef struct v2 { typedef struct v2 {
float x, y; float x, y;
} v2; } v2;
@ -40,7 +42,7 @@ void init()
} }
} }
void go(float dt, int scale) void go(float dt)
{ {
for (int i = 0; i < NB; i++) { for (int i = 0; i < NB; i++) {
v2 npos; v2 npos;
@ -76,15 +78,15 @@ void go(float dt, int scale)
} }
} }
v2 penger_origin = {(float) width/2 - 32*scale/2, (float) height/2 - 32*scale/2}; v2 penger_origin = {(float) width/2 - 32*scale()/2, (float) height/2 - 32*scale()/2};
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
for (int y = 0; y < 32; y++) { for (int y = 0; y < 32; y++) {
if (penger_img[i][y] == 0) if (penger_img[i][y] == 0)
continue; continue;
for (int s1 = 0; s1 < scale; s1++) { for (int s1 = 0; s1 < scale(); s1++) {
for (int s2 = 0; s2 < scale; s2++) { for (int s2 = 0; s2 < scale(); s2++) {
int idx_x = penger_origin.x + y*scale+s1; int idx_x = penger_origin.x + y*scale()+s1;
int idx_y = penger_origin.y + i*scale+s2; int idx_y = penger_origin.y + i*scale()+s2;
if (idx_x < 0 || idx_x > width || idx_y < 0 || idx_y > height) if (idx_x < 0 || idx_x > width || idx_y < 0 || idx_y > height)
continue; continue;
BUFFER[idx_y*width + idx_x] = penger_img[i][y]; BUFFER[idx_y*width + idx_x] = penger_img[i][y];

View File

@ -33,6 +33,7 @@ function make_environment(...envs) {
const { instance } = await WebAssembly.instantiateStreaming(fetch("./app.wasm"), { const { instance } = await WebAssembly.instantiateStreaming(fetch("./app.wasm"), {
"env": make_environment({ "env": make_environment({
'random': Math.random, 'random': Math.random,
'scale': () => {return scale},
}) })
}); });
@ -62,14 +63,14 @@ instance.exports.init();
let prev = null; let prev = null;
function first(timestamp) { function first(timestamp) {
prev = timestamp; prev = timestamp;
instance.exports.go(0.16, scale); instance.exports.go(0.16);
window.requestAnimationFrame(loop); window.requestAnimationFrame(loop);
} }
function loop(timestamp) { function loop(timestamp) {
const dt = timestamp - prev; const dt = timestamp - prev;
prev = timestamp; prev = timestamp;
instance.exports.go(dt/1000, scale); instance.exports.go(dt/1000);
ctx.putImageData(image, 0, 0); ctx.putImageData(image, 0, 0);
window.requestAnimationFrame(loop); window.requestAnimationFrame(loop);
} }