diff --git a/Cargo.toml b/Cargo.toml index 89b8f39..8eeb626 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,12 @@ edition = "2018" [lib] crate-type = ["cdylib", "rlib"] +[dependencies.web-sys] +version = "0.3" +features = [ + "console", +] + [features] default = ["console_error_panic_hook"] diff --git a/src/lib.rs b/src/lib.rs index d1a97e2..05d0858 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,15 @@ mod utils; use std::fmt; use wasm_bindgen::prelude::*; +extern crate web_sys; + +// A macro to provide `println!(..)`-style syntax for `console.log` logging. +macro_rules! log { + ( $( $t:tt )* ) => { + web_sys::console::log_1(&format!( $( $t )* ).into()); + } +} + // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. #[cfg(feature = "wee_alloc")] @@ -63,6 +72,14 @@ impl Universe { let cell = self.cells[idx]; let live_neighbors = self.live_neighbor_count(row, col); + log!( + "cell[{}, {}] is initially {:?} and has {} live neighbors", + row, + col, + cell, + live_neighbors + ); + let next_cell = match (cell, live_neighbors) { // Rule 1: Any live cell with fewer than two live neighbours // dies, as if caused by underpopulation. @@ -88,6 +105,8 @@ impl Universe { } pub fn new() -> Universe { + utils::set_panic_hook(); + let width = 64; let height = 64; diff --git a/www/index.js b/www/index.js index 268a627..1f8bc3e 100644 --- a/www/index.js +++ b/www/index.js @@ -21,6 +21,7 @@ canvas.width = (CELL_SIZE + 1) * width + 1; const ctx = canvas.getContext('2d'); const renderLoop = () => { + debugger; universe.tick(); drawGrid();