mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add line function to platform
This commit is contained in:
@@ -3,7 +3,7 @@ import "env.memory" memory(4);
|
||||
global mut base_end: i32 = 0;
|
||||
|
||||
export fn load_uw8(module_size: i32) -> i32 {
|
||||
let lazy version = ?0 - 1;
|
||||
let lazy version = 0?0 - 1;
|
||||
if version < 0 {
|
||||
return module_size;
|
||||
}
|
||||
@@ -21,10 +21,10 @@ export fn load_uw8(module_size: i32) -> i32 {
|
||||
let src = 0x1e001;
|
||||
|
||||
loop sections {
|
||||
if src < module_end & (base_start >= base_end | ?src <= ?base_start) {
|
||||
if src < module_end & (base_start >= base_end | src?0 <= base_start?0) {
|
||||
let lazy length2 = copy_section(dest, src);
|
||||
dest = dest + length2;
|
||||
if base_start < base_end & ?src == ?base_start {
|
||||
if base_start < base_end & src?0 == base_start?0 {
|
||||
base_start = base_start + section_size(base_start);
|
||||
}
|
||||
src = src + length2;
|
||||
@@ -47,7 +47,7 @@ fn section_size(ptr: i32) -> i32 {
|
||||
let l = 0;
|
||||
let shift = 0;
|
||||
loop size {
|
||||
let lazy b = ?p;
|
||||
let lazy b = p?0;
|
||||
l = l | ((b & 127) << shift);
|
||||
shift = shift + 7;
|
||||
p = p + 1;
|
||||
@@ -65,7 +65,7 @@ fn copy_section(dest: i32, src: i32) -> i32 {
|
||||
fn copy(dest: i32, src: i32, len: i32) {
|
||||
if len > 0 {
|
||||
loop bytes {
|
||||
?(dest + (len := len - 1)) = ?(src + len);
|
||||
(dest + (len := len - 1))?0 = (src + len)?0;
|
||||
branch_if len: bytes
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ fn upkr_bit(context_index: i32) -> i32 {
|
||||
|
||||
loop refill {
|
||||
if upkr_state < 1<<16 {
|
||||
upkr_state = (upkr_state << 8) | ?upkr_src_ptr;
|
||||
upkr_state = (upkr_state << 8) | upkr_src_ptr?0;
|
||||
upkr_src_ptr = upkr_src_ptr + 1;
|
||||
branch refill;
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ fn main() -> Result<()> {
|
||||
convert_font()?;
|
||||
|
||||
println!("Compiling loader module");
|
||||
let loader = curlywas::compile_file("src/loader.cwa")?;
|
||||
let loader = curlywas::compile_file("src/loader.cwa", curlywas::Options::default())?;
|
||||
File::create("bin/loader.wasm")?.write_all(&loader)?;
|
||||
|
||||
println!("Loader (including base module): {} bytes", loader.len());
|
||||
|
||||
println!("Compiling platform module");
|
||||
let platform = curlywas::compile_file("src/platform.cwa")?;
|
||||
let platform = curlywas::compile_file("src/platform.cwa", curlywas::Options::default())?;
|
||||
println!("Compressing platform module");
|
||||
let platform = uw8_tool::pack(
|
||||
&platform,
|
||||
|
||||
@@ -135,6 +135,89 @@ export fn circle(cx: f32, cy: f32, radius: f32, col: i32) {
|
||||
}
|
||||
}
|
||||
|
||||
export fn line(x1: f32, y1: f32, x2: f32, y2: f32, col: i32) {
|
||||
let swapTmp: f32;
|
||||
if x1 > x2 {
|
||||
swapTmp = x1;
|
||||
x1 = x2;
|
||||
x2 = swapTmp;
|
||||
swapTmp = y1;
|
||||
y1 = y2;
|
||||
y2 = swapTmp;
|
||||
}
|
||||
if x1 < 0 as f32 & x2 >= 0 as f32 {
|
||||
y1 = y1 + (y2 - y1) * -x1 / (x2 - x1);
|
||||
x1 = 0 as f32;
|
||||
}
|
||||
if x1 < 320 as f32 & x2 >= 320 as f32 {
|
||||
y2 = y2 + (y2 - y1) * (320 as f32 - x2) / (x2 - x1);
|
||||
x2 = 320 as f32;
|
||||
}
|
||||
|
||||
if y1 > y2 {
|
||||
swapTmp = x1;
|
||||
x1 = x2;
|
||||
x2 = swapTmp;
|
||||
swapTmp = y1;
|
||||
y1 = y2;
|
||||
y2 = swapTmp;
|
||||
}
|
||||
if y1 < 0 as f32 & y2 >= 0 as f32 {
|
||||
x1 = x1 + (x2 - x1) * -y1 / (y2 - y1);
|
||||
y1 = 0 as f32;
|
||||
}
|
||||
if y1 < 240 as f32 & y2 >= 240 as f32 {
|
||||
x2 = x2 + (x2 - x1) * (240 as f32 - y2) / (y2 - y1);
|
||||
y2 = 240 as f32;
|
||||
}
|
||||
|
||||
let lazy dx = x2 - x1;
|
||||
let lazy dy = y2 - y1;
|
||||
let max_axis: f32;
|
||||
let p: f32;
|
||||
if abs(dx) >= dy {
|
||||
max_axis = dx;
|
||||
p = x1;
|
||||
} else {
|
||||
max_axis = dy;
|
||||
p = y1;
|
||||
}
|
||||
|
||||
let steps = floor(p + max_axis) as i32 - floor(p) as i32;
|
||||
p = floor(p) + 0.5 - p;
|
||||
if max_axis < 0 as f32 {
|
||||
steps = -steps;
|
||||
p = -p;
|
||||
max_axis = -max_axis;
|
||||
}
|
||||
dx = dx / max_axis;
|
||||
dy = dy / max_axis;
|
||||
|
||||
let f = min(max_axis, max(0 as f32, p));
|
||||
setPixel((x1 + f * dx) as i32, (y1 + f * dy) as i32, col);
|
||||
|
||||
if !steps {
|
||||
return;
|
||||
}
|
||||
|
||||
x1 = x1 + (1 as f32 + p) * dx;
|
||||
y1 = y1 + (1 as f32 + p) * dy;
|
||||
|
||||
p = p + steps as f32;
|
||||
|
||||
loop pixels {
|
||||
if steps := steps - 1 {
|
||||
setPixel(x1 as i32, y1 as i32, col);
|
||||
x1 = x1 + dx;
|
||||
y1 = y1 + dy;
|
||||
branch pixels;
|
||||
}
|
||||
}
|
||||
|
||||
f = min(max_axis, p) - p;
|
||||
setPixel((x1 + f * dx) as i32, (y1 + f * dy) as i32, col);
|
||||
}
|
||||
|
||||
//////////
|
||||
// TEXT //
|
||||
//////////
|
||||
|
||||
Reference in New Issue
Block a user