Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #[wasm_bindgen]
  2. impl Canvas {
  3. // square
  4. pub fn new(dim:usize,cell_across:usize) -> Canvas {
  5. console_error_panic_hook::set_once();
  6. // figure out the cells
  7. let u = Universe::new(cell_across as u32,cell_across as u32);
  8. Canvas{
  9. width:dim,
  10. height:dim,
  11. cell_across,
  12. cellsize:dim/cell_across,
  13. universe:u,
  14. Array:vec![255;dim*dim*4]
  15. }
  16. }
  17.  
  18. pub fn pointer(&mut self) -> *const u8 {
  19. self.Array.as_ptr()
  20. }
  21. pub fn tick(&mut self) {
  22. // cleare previous info
  23. self.Array = vec![255;self.width*self.height*4];
  24. // call the universe tick
  25. self.universe.tick();
  26. self.life_to_canvas();
  27. }
  28. }
  29.  
  30. impl Canvas{
  31. //divvy up the canvas array
  32. fn fill_box(&mut self,xi:usize,yi:usize,xf:usize,yf:usize) {
  33. for i in xi..xf {
  34. for j in yi..yf {
  35. self.Array[i*4 + j*self.width*4] = 0;
  36. self.Array[i*4 + j*self.width*4 +1] = 0;
  37. self.Array[i*4 + j*self.width*4 +2] = 0;
  38. }
  39. }
  40. }
  41. fn life_to_canvas(&mut self) {
  42. // we know the size of the life cell and the canvas just split the number across and top to
  43. // bottom normally
  44. let across = self.width/self.cellsize;
  45. let tall = self.height/self.cellsize;
  46. for i in 0..across{
  47. for j in 0..tall{
  48. // check on living cells
  49. let cell = self.universe.cells[self.universe.get_index(i as u32,j as u32)];
  50. match cell {
  51. Cell::Alive => self.fill_box(i*self.cellsize,j*self.cellsize,(i+1)*self.cellsize,(j+1)*self.cellsize),
  52. _=> {}
  53. };
  54. }
  55. }
  56. }
  57. }
  58.  
  59. //create a graph object which represents the actual canvas buffer to write with
  60.  
  61. // When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
  62. // allocator.
  63. //
  64. // If you don't want to use `wee_alloc`, you can safely delete this.
  65. #[cfg(feature = "wee_alloc")]
  66. #[global_allocator]
  67. static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
  68.  
  69.  
  70. // This is like the `main` function, except for JavaScript.
  71. #[wasm_bindgen(start)]
  72. pub fn main_js() -> Result<(), JsValue> {
  73. // This provides better error messages in debug mode.
  74. // It's disabled in release mode so it doesn't bloat up the file size.
  75. #[cfg(debug_assertions)]
  76. console_error_panic_hook::set_once();
  77.  
  78.  
  79. // Your code goes here!
  80. console::log_1(&JsValue::from_str("Hello world!"));
  81.  
  82. Ok(())
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement