Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
rs-wasm-glife/src/lib.rs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (51 sloc)
1.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod utils; | |
use wasm_bindgen::prelude::*; | |
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global | |
// allocator. | |
#[cfg(feature = "wee_alloc")] | |
#[global_allocator] | |
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | |
mod universe; | |
use universe::Universe; | |
#[wasm_bindgen] | |
extern { | |
fn alert(s: &str); | |
#[wasm_bindgen(js_namespace = console)] | |
fn log(s: &str); | |
} | |
#[wasm_bindgen] | |
pub fn greet() { | |
alert("Hello, world! wasm-glife!"); | |
let mut universe = universe::Universe::new(8, 8); | |
log(&universe.sum().to_string()); | |
log(&universe.to_string()); | |
universe.tick(); | |
log(&universe.sum().to_string()); | |
log(&universe.to_string()); | |
} | |
#[wasm_bindgen] | |
pub struct Game { | |
universe: Universe, | |
} | |
#[wasm_bindgen] | |
impl Game { | |
pub fn new(width: u32, height: u32) -> Game { | |
Game { | |
universe: Universe::new(width, height) | |
} | |
} | |
#[allow(non_snake_case)] | |
pub fn toString(&self) -> String { | |
self.universe.to_string() | |
} | |
pub fn tick(&mut self) { | |
self.universe.tick(); | |
} | |
#[allow(non_snake_case)] | |
pub fn isChanged(&self, i: usize) -> bool { | |
self.universe.changed(i) | |
} | |
pub fn toggle(&mut self, i: usize) { | |
self.universe.toggle(i); | |
} | |
} |