33// Configuration imported from JS
44import { BGR_ALIVE , BGR_DEAD , BIT_ROT } from "./config" ;
55
6- var w : i32 , h : i32 , s : i32 ;
6+ var width : i32 , height : i32 , offset : i32 ;
77
88/** Gets an input pixel in the range [0, s]. */
99@inline
1010function get ( x : u32 , y : u32 ) : u32 {
11- return load < u32 > ( ( y * w + x ) << 2 ) ;
11+ return load < u32 > ( ( y * width + x ) << 2 ) ;
1212}
1313
1414/** Sets an output pixel in the range [s, 2*s]. */
1515@inline
1616function set ( x : u32 , y : u32 , v : u32 ) : void {
17- store < u32 > ( ( s + y * w + x ) << 2 , v ) ;
17+ store < u32 > ( ( offset + y * width + x ) << 2 , v ) ;
1818}
1919
2020/** Sets an output pixel in the range [s, 2*s] while fading it out. */
2121@inline
2222function rot ( x : u32 , y : u32 , v : u32 ) : void {
23- var a = max < i32 > ( ( v > >> 24 ) - BIT_ROT , 0 ) ;
24- set ( x , y , ( a << 24 ) | ( v & 0x00ffffff ) ) ;
23+ var alpha = max < i32 > ( ( v >> 24 ) - BIT_ROT , 0 ) ;
24+ set ( x , y , ( alpha << 24 ) | ( v & 0x00ffffff ) ) ;
2525}
2626
2727/** Initializes width and height. Called once from JS. */
28- export function init ( width : i32 , height : i32 ) : void {
29- w = width ;
30- h = height ;
31- s = width * height ;
28+ export function init ( w : i32 , h : i32 ) : void {
29+ width = w ;
30+ height = h ;
31+ offset = w * h ;
3232
3333 // Start by filling output with random live cells.
3434 for ( let y = 0 ; y < h ; ++ y ) {
3535 for ( let x = 0 ; x < w ; ++ x ) {
36- set ( x , y , Math . random ( ) > 0.1 ? BGR_DEAD & 0x00ffffff : BGR_ALIVE | 0xff000000 ) ;
36+ let c = Math . random ( ) > 0.1
37+ ? BGR_DEAD & 0x00ffffff
38+ : BGR_ALIVE | 0xff000000 ;
39+ set ( x , y , c ) ;
3740 }
3841 }
3942}
4043
4144/** Performs one step. Called about 30 times a second from JS. */
4245export function step ( ) : void {
46+ var w = width ,
47+ h = height ;
48+
4349 var hm1 = h - 1 , // h - 1
4450 wm1 = w - 1 ; // w - 1
4551
@@ -55,9 +61,9 @@ export function step(): void {
5561 // Every cell interacts with its eight neighbours, which are the cells that are horizontally,
5662 // vertically, or diagonally adjacent. Least significant bit indicates alive or dead.
5763 let aliveNeighbors = (
58- ( get ( xm1 , ym1 ) & 1 ) + ( get ( x , ym1 ) & 1 ) + ( get ( xp1 , ym1 ) & 1 ) +
59- ( get ( xm1 , y ) & 1 ) + ( get ( xp1 , y ) & 1 ) +
60- ( get ( xm1 , yp1 ) & 1 ) + ( get ( x , yp1 ) & 1 ) + ( get ( xp1 , yp1 ) & 1 )
64+ ( get ( xm1 , ym1 ) & 1 ) + ( get ( x , ym1 ) & 1 ) + ( get ( xp1 , ym1 ) & 1 ) +
65+ ( get ( xm1 , y ) & 1 ) + ( get ( xp1 , y ) & 1 ) +
66+ ( get ( xm1 , yp1 ) & 1 ) + ( get ( x , yp1 ) & 1 ) + ( get ( xp1 , yp1 ) & 1 )
6167 ) ;
6268
6369 let self = get ( x , y ) ;
@@ -78,10 +84,10 @@ export function step(): void {
7884
7985/** Fills the row and column indicated by `x` and `y` with random live cells. */
8086export function fill ( x : u32 , y : u32 , p : f64 ) : void {
81- for ( let ix = 0 ; ix < w ; ++ ix ) {
87+ for ( let ix = 0 ; ix < width ; ++ ix ) {
8288 if ( Math . random ( ) < p ) set ( ix , y , BGR_ALIVE | 0xff000000 ) ;
8389 }
84- for ( let iy = 0 ; iy < h ; ++ iy ) {
90+ for ( let iy = 0 ; iy < height ; ++ iy ) {
8591 if ( Math . random ( ) < p ) set ( x , iy , BGR_ALIVE | 0xff000000 ) ;
8692 }
8793}
0 commit comments