init
This commit is contained in:
commit
9db10d6064
|
@ -0,0 +1,2 @@
|
|||
app.wasm
|
||||
app.wat
|
|
@ -0,0 +1,95 @@
|
|||
#include "penger.c"
|
||||
|
||||
#define BLUE 0xffff0000
|
||||
#define RED 0xff0000ff
|
||||
#define BLACK 0xff000000
|
||||
|
||||
const unsigned int width = 800;
|
||||
const unsigned int height = 600;
|
||||
unsigned int BUFFER[width * height];
|
||||
|
||||
typedef struct v2 {
|
||||
float x, y;
|
||||
} v2;
|
||||
|
||||
#define NB 10
|
||||
v2 pos[NB];
|
||||
|
||||
#define GRAVITY 5
|
||||
|
||||
#define SPEED 500
|
||||
v2 dir[NB];
|
||||
|
||||
unsigned int radius = 30;
|
||||
|
||||
float random(void);
|
||||
|
||||
int rand(int min, int max)
|
||||
{
|
||||
return min + random() * (max - min);
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
for (int i = 0; i < NB; i++) {
|
||||
pos[i].x = rand(radius, width - radius);
|
||||
pos[i].y = rand(radius, height - radius);
|
||||
|
||||
dir[i].x = SPEED;
|
||||
dir[i].y = SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
void go(float dt, int scale)
|
||||
{
|
||||
for (int i = 0; i < NB; i++) {
|
||||
v2 npos;
|
||||
npos.x = pos[i].x + dir[i].x * dt;
|
||||
npos.y = pos[i].y + dir[i].y * dt;
|
||||
|
||||
if (npos.x - radius < 0 || npos.x + radius > width) {
|
||||
dir[i].x = -dir[i].x;
|
||||
}
|
||||
if (npos.y - radius < 0 || npos.y + radius > height) {
|
||||
dir[i].y = -dir[i].y;
|
||||
}
|
||||
|
||||
pos[i].x += dir[i].x * dt;
|
||||
pos[i].y += dir[i].y * dt;
|
||||
}
|
||||
|
||||
for (int h = 0; h < height; h++) {
|
||||
for (int w = 0; w < width; w++) {
|
||||
unsigned int color = BLUE;
|
||||
for (int p = 0; p < NB; p++) {
|
||||
unsigned int dw = (unsigned int) pos[p].x - w;
|
||||
unsigned int dh = (unsigned int) pos[p].y - h;
|
||||
unsigned int d = dw * dw + dh * dh;
|
||||
|
||||
if (d <= radius*radius) {
|
||||
unsigned short int red = (float) d/(radius*radius) * 255;
|
||||
color = BLACK + red;
|
||||
break;
|
||||
}
|
||||
}
|
||||
BUFFER[h*width + w] = color;
|
||||
}
|
||||
}
|
||||
|
||||
v2 penger_origin = {(float) width/2 - 32*scale/2, (float) height/2 - 32*scale/2};
|
||||
for (int i = 0; i < 32; i++) {
|
||||
for (int y = 0; y < 32; y++) {
|
||||
if (penger_img[i][y] == 0)
|
||||
continue;
|
||||
for (int s1 = 0; s1 < scale; s1++) {
|
||||
for (int s2 = 0; s2 < scale; s2++) {
|
||||
int idx_x = penger_origin.x + y*scale+s1;
|
||||
int idx_y = penger_origin.y + i*scale+s2;
|
||||
if (idx_x < 0 || idx_x > width || idx_y < 0 || idx_y > height)
|
||||
continue;
|
||||
BUFFER[idx_y*width + idx_x] = penger_img[i][y];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#!/bin/bash
|
||||
|
||||
export_sym="init go BUFFER width height"
|
||||
export_cmd=""
|
||||
for e in $export_sym; do
|
||||
export_cmd="$export_cmd -Wl,--export=$e";
|
||||
done;
|
||||
|
||||
f=$(echo $1 | sed "s/\..*$//g")
|
||||
|
||||
clang -O2 --target=wasm32 -fno-builtin -nostdlib --no-standard-libraries -Wl,--no-entry $export_cmd -Wl,--allow-undefined -o $f.wasm $1
|
||||
|
||||
wasm2wat $f.wasm > $f.wat
|
|
@ -0,0 +1,10 @@
|
|||
version: "3"
|
||||
|
||||
services:
|
||||
web:
|
||||
container_name: "web"
|
||||
image: "httpd"
|
||||
ports:
|
||||
- "6969:80"
|
||||
volumes:
|
||||
- ".:/usr/local/apache2/htdocs/"
|
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="load.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="demo-canvas"></canvas>
|
||||
<br>
|
||||
<p id='scale'></p>
|
||||
<button id='descaling'>scale - 1</button>
|
||||
<button id='scaling'>scale + 1</button>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,78 @@
|
|||
var scale = 12;
|
||||
|
||||
window.onload = () => {
|
||||
var scale_but = document.getElementById("scaling");
|
||||
var descale_but = document.getElementById("descaling");
|
||||
var scale_p = document.getElementById("scale");
|
||||
scale_p.innerText = "scale: " + scale;
|
||||
scale_but.onclick = () => {
|
||||
scale++;
|
||||
scale_p.innerText = "scale: " + scale;
|
||||
}
|
||||
descale_but.onclick = () => {
|
||||
scale--;
|
||||
scale_p.innerText = "scale: " + scale;
|
||||
}
|
||||
};
|
||||
|
||||
function make_environment(...envs) {
|
||||
return new Proxy(envs, {
|
||||
get(target, prop, receiver) {
|
||||
for (let env of envs) {
|
||||
if (env.hasOwnProperty(prop)) {
|
||||
return env[prop];
|
||||
}
|
||||
}
|
||||
return (...args) => {console.error("NOT IMPLEMENTED: "+prop, args)}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
(async() => {
|
||||
|
||||
const { instance } = await WebAssembly.instantiateStreaming(fetch("./app.wasm"), {
|
||||
"env": make_environment({
|
||||
'random': Math.random,
|
||||
})
|
||||
});
|
||||
|
||||
const memory = new Uint32Array(instance.exports.memory.buffer);
|
||||
|
||||
const width = memory[instance.exports.width.value / 4];
|
||||
const height = memory[instance.exports.height.value / 4];
|
||||
|
||||
const canvas = document.getElementById("demo-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,
|
||||
4 * width * height,
|
||||
),
|
||||
width,
|
||||
);
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
instance.exports.init();
|
||||
|
||||
let prev = null;
|
||||
function first(timestamp) {
|
||||
prev = timestamp;
|
||||
instance.exports.go(0.16, scale);
|
||||
window.requestAnimationFrame(loop);
|
||||
}
|
||||
function loop(timestamp) {
|
||||
const dt = timestamp - prev;
|
||||
prev = timestamp;
|
||||
|
||||
instance.exports.go(dt/1000, scale);
|
||||
ctx.putImageData(image, 0, 0);
|
||||
window.requestAnimationFrame(loop);
|
||||
}
|
||||
window.requestAnimationFrame(first);
|
||||
|
||||
})()
|
|
@ -0,0 +1,40 @@
|
|||
#define T 0x00000000
|
||||
#define B 0xff000000
|
||||
#define G 0xff383838
|
||||
#define W 0xffc0c0c0
|
||||
#define Y 0xff36c7fb
|
||||
|
||||
unsigned int penger_img[32][32] = {
|
||||
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, B, B, T, T, T, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, T, T, T, B, B, B, B, G, B, B, B, T, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, B, B, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, G, G, B, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, G, G, G, B, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, G, G, B, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, G, G, G, G, G, B, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, B, G, G, G, G, G, B, B, B, G, G, G, G, B, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, T, B, G, G, G, G, G, G, B, B, G, G, G, G, B, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, G, G, G, G, G, G, B, B, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, G, G, G, B, B, B, B, B, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, B, B, B, B, Y, Y, Y, Y, B, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, B, B, Y, Y, Y, Y, Y, Y, Y, B, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, B, G, G, G, G, G, G, G, G, G, B, B, B, Y, Y, Y, Y, Y, B, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, G, G, G, G, B, B, B, Y, Y, Y, B, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, G, G, G, G, G, B, B, B, B, B, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, G, G, G, G, B, B, B, B, B, T, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, G, B, B, B, B, W, W, B, B, B, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, G, B, W, W, W, W, W, W, W, B, T, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, G, B, W, W, W, W, W, W, W, W, B, B, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, B, W, W, W, W, W, W, W, W, W, W, B, T, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, B, W, W, W, W, W, W, W, W, W, W, B, B, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, B, W, W, W, W, W, W, W, W, W, W, W, B, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, G, G, G, G, B, W, W, W, W, W, W, W, W, W, W, W, B, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, B, G, G, G, B, W, W, W, W, W, W, W, W, W, W, W, B, T, T, T, T, T, T, T,
|
||||
T, T, T, T, T, T, T, B, B, G, G, G, B, W, W, W, W, W, W, W, W, W, W, B, Y, B, B, T, T, T, T, T,
|
||||
T, T, T, T, T, B, B, B, Y, B, G, G, B, W, W, W, W, W, W, W, W, B, B, B, Y, Y, B, B, T, T, T, T,
|
||||
T, T, T, T, B, B, Y, Y, Y, Y, B, B, B, B, B, B, B, B, B, B, B, Y, Y, Y, Y, Y, Y, B, B, T, T, T,
|
||||
T, T, T, T, B, B, Y, Y, Y, Y, Y, Y, B, T, T, T, T, T, T, T, T, B, B, Y, Y, Y, Y, Y, B, T, T, T,
|
||||
T, T, T, T, T, B, B, B, Y, Y, B, B, B, T, T, T, T, T, T, T, T, T, B, B, B, Y, Y, B, B, T, T, T,
|
||||
T, T, T, T, T, T, T, B, B, B, B, T, T, T, T, T, T, T, T, T, T, T, T, T, B, B, B, B, T, T, T, T,
|
||||
};
|
Loading…
Reference in New Issue