Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[wasm_bindgen]
- impl Canvas {
- // square
- pub fn new(dim:usize,cell_across:usize) -> Canvas {
- console_error_panic_hook::set_once();
- // figure out the cells
- let u = Universe::new(cell_across as u32,cell_across as u32);
- Canvas{
- width:dim,
- height:dim,
- cell_across,
- cellsize:dim/cell_across,
- universe:u,
- Array:vec![255;dim*dim*4]
- }
- }
- pub fn pointer(&mut self) -> *const u8 {
- self.Array.as_ptr()
- }
- pub fn tick(&mut self) {
- // cleare previous info
- self.Array = vec![255;self.width*self.height*4];
- // call the universe tick
- self.universe.tick();
- self.life_to_canvas();
- }
- }
- impl Canvas{
- //divvy up the canvas array
- fn fill_box(&mut self,xi:usize,yi:usize,xf:usize,yf:usize) {
- for i in xi..xf {
- for j in yi..yf {
- self.Array[i*4 + j*self.width*4] = 0;
- self.Array[i*4 + j*self.width*4 +1] = 0;
- self.Array[i*4 + j*self.width*4 +2] = 0;
- }
- }
- }
- fn life_to_canvas(&mut self) {
- // we know the size of the life cell and the canvas just split the number across and top to
- // bottom normally
- let across = self.width/self.cellsize;
- let tall = self.height/self.cellsize;
- for i in 0..across{
- for j in 0..tall{
- // check on living cells
- let cell = self.universe.cells[self.universe.get_index(i as u32,j as u32)];
- match cell {
- Cell::Alive => self.fill_box(i*self.cellsize,j*self.cellsize,(i+1)*self.cellsize,(j+1)*self.cellsize),
- _=> {}
- };
- }
- }
- }
- }
- //create a graph object which represents the actual canvas buffer to write with
- // When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
- // allocator.
- //
- // If you don't want to use `wee_alloc`, you can safely delete this.
- #[cfg(feature = "wee_alloc")]
- #[global_allocator]
- static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
- // This is like the `main` function, except for JavaScript.
- #[wasm_bindgen(start)]
- pub fn main_js() -> Result<(), JsValue> {
- // This provides better error messages in debug mode.
- // It's disabled in release mode so it doesn't bloat up the file size.
- #[cfg(debug_assertions)]
- console_error_panic_hook::set_once();
- // Your code goes here!
- console::log_1(&JsValue::from_str("Hello world!"));
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement