Files
curlywas/examples/microw8/palette.cwa
2021-11-15 14:33:23 +01:00

95 lines
2.7 KiB
Plaintext

import "env.memory" memory(4);
import "env.pow" fn pow(f32, f32) -> f32;
import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32;
import "env.atan2" fn atan2(f32, f32) -> f32;
import "env.tan" fn tan(f32) -> f32;
import "env.atan" fn atan(f32) -> f32;
import "env.rectangle" fn rect(f32, f32, f32, f32, i32);
//export fn tic(time: i32) {
// let i: i32;
// loop pixels {
// let lazy x = (i % 320) as f32 - 160.5;
// let lazy y = (i / 320 - 120) as f32;
//
// let lazy dist = 4000 as f32 / sqrt(x*x + y*y + 10 as f32);
// let lazy angle = atan2(x, y) * (64.0 / 3.141);
//
// i?120 = ((((dist + time as f32 / 63 as f32) as i32 ^ angle as i32) #% 32 + 32) >> ((dist as i32 - i % 7 * 3) / 40)) + 192;
//
// branch_if (i := i + 1) < 320*240: pixels;
// }
//}
export fn tic(time: i32) {
let i: i32;
loop colors {
rect((i % 16 * 15) as f32, (i / 16 * 15) as f32, 15 as f32, 15 as f32, i);
branch_if (i := i + 1) < 256: colors
}
}
fn make_gradient(c: f32, base: i32) -> i32 {
let i: i32;
loop colors {
let lazy f = (i + 4) as f32 / 64 as f32;
let lazy a = f * (tan(c*1.1) * -0.7 + 0.4);
let lazy b = f * f * 0.7;
let lazy v = ((a + b - a * b) * 255 as f32) as i32;
(base + i)?(120+320*240) = select(v < 0, 0, select(v > 255, 255, v));
branch_if (i := i + 4) < 64: colors;
}
base + 1
}
start fn gen_palette() {
let i: i32;
loop hsv {
let a = i as f32 * (3.141 / 6.0);
let inline base2 = make_gradient(sin(a) * 0.707 - cos(a) * 0.804, i * 64);
let inline base3 = make_gradient(cos(a) * 0.816, base2);
make_gradient(cos(a) * 0.804 + sin(a) * 0.707, base3);
branch_if (i := i + 1) < 11: hsv;
}
make_gradient(0 as f32, make_gradient(0 as f32, make_gradient(0 as f32, 11*16*4)));
i = 255;
loop expand_sweetie {
let lazy channel = i & 3;
let lazy index = i >> 2;
let lazy first_step = index >= 32;
let inline src1 = select(first_step, index % 32 / 2, index * 2);
let inline src2 = select(first_step, (index + 1) % 32 / 2, index * 2 + 1);
let inline c1 = (src1 * 4 + channel)?(120+320*240+192*4);
let inline c2 = (src2 * 4 + channel)?(120+320*240+192*4);
i?(120+320*240+192*4) = (c1 + c2) * (3 + first_step) / 8;
branch_if (i := i - 1) >= 0: expand_sweetie;
}
}
data 120+320*240+192*4 {
i32(
0x2c1c1a,
0x5d275d,
0x533eb1,
0x577def,
0x75cdff,
0x70f0a7,
0x64b738,
0x797125,
0x6f3629,
0xc95d3b,
0xf6a641,
0xf7ef73,
0xf4f4f4,
0xc2b094,
0x866c56,
0x573c33
)
}