Permalink
Browse files

Implement wasm rectangle and background drawcalls

  • Loading branch information...
tomassedovic committed Dec 20, 2017
1 parent 8d0e939 commit 12225b377068e7774dad3069b04043681e8ac18f
Showing with 40 additions and 4 deletions.
  1. +13 −4 dose-response.js
  2. +27 −0 src/main.rs
@@ -125,17 +125,26 @@ fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')

memory = new Uint8Array(wasm_instance.exports.memory.buffer, ptr, len);

ctx.clearRect(0, 0, width * squareSize, height * squareSize);

for(let i = 0; i < len; i += 6) {
let x = memory[i + 0];
let y = memory[i + 1];
let glyph = String.fromCharCode(memory[i + 2]);
var glyph = null;
if(memory[i + 2] != 0) {
glyph = String.fromCharCode(memory[i + 2]);
}
let r = memory[i + 3];
let g = memory[i + 4];
let b = memory[i + 5];

ctx.fillStyle = `rgb(${r},${g},${b})`;
ctx.clearRect(x * squareSize, y * squareSize, squareSize, squareSize);
ctx.fillText(glyph, x * squareSize + squareSize / 2, y * squareSize + squareSize / 2);
if(glyph === null) {
ctx.fillStyle = `rgb(${r},${g},${b})`;
ctx.fillRect(x * squareSize, y * squareSize, squareSize, squareSize);
} else {
ctx.fillStyle = `rgb(${r},${g},${b})`;
ctx.fillText(glyph, x * squareSize + squareSize / 2, y * squareSize + squareSize / 2);
}
}

}
@@ -436,6 +436,33 @@ pub fn update(state_ptr: *mut State) {
}
}

&engine::Draw::Rectangle(top_left, dimensions, color) => {
if dimensions.x > 0 && dimensions.y > 0 {
let rect = rect::Rectangle::from_point_and_size(top_left, dimensions);
for pos in rect.points() {
assert!(pos.x >= 0 && pos.x < 255);
assert!(pos.y >= 0 && pos.y < 255);
js_drawcalls.push(pos.x as u8);
js_drawcalls.push(pos.y as u8);
js_drawcalls.push(0);
js_drawcalls.push(color.r);
js_drawcalls.push(color.g);
js_drawcalls.push(color.b);
}
}
}

&engine::Draw::Background(pos, color) => {
assert!(pos.x >= 0 && pos.x < 255);
assert!(pos.y >= 0 && pos.y < 255);
js_drawcalls.push(pos.x as u8);
js_drawcalls.push(pos.y as u8);
js_drawcalls.push(0);
js_drawcalls.push(color.r);
js_drawcalls.push(color.g);
js_drawcalls.push(color.b);
}

_ => {} // TODO
}
}

0 comments on commit 12225b3

Please sign in to comment.