Skip to content

Commit

Permalink
Implement wasm rectangle and background drawcalls
Browse files Browse the repository at this point in the history
  • Loading branch information
tomassedovic committed Dec 20, 2017
1 parent 8d0e939 commit 12225b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
17 changes: 13 additions & 4 deletions dose-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Expand Down
27 changes: 27 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 12225b3

Please sign in to comment.