3
3
// Configuration imported from JS
4
4
import { BGR_ALIVE , BGR_DEAD , BIT_ROT } from "./config" ;
5
5
6
- var w : i32 , h : i32 , s : i32 ;
6
+ var width : i32 , height : i32 , offset : i32 ;
7
7
8
8
/** Gets an input pixel in the range [0, s]. */
9
9
@inline
10
10
function get ( x : u32 , y : u32 ) : u32 {
11
- return load < u32 > ( ( y * w + x ) << 2 ) ;
11
+ return load < u32 > ( ( y * width + x ) << 2 ) ;
12
12
}
13
13
14
14
/** Sets an output pixel in the range [s, 2*s]. */
15
15
@inline
16
16
function 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 ) ;
18
18
}
19
19
20
20
/** Sets an output pixel in the range [s, 2*s] while fading it out. */
21
21
@inline
22
22
function 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 ) ) ;
25
25
}
26
26
27
27
/** 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 ;
32
32
33
33
// Start by filling output with random live cells.
34
34
for ( let y = 0 ; y < h ; ++ y ) {
35
35
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 ) ;
37
40
}
38
41
}
39
42
}
40
43
41
44
/** Performs one step. Called about 30 times a second from JS. */
42
45
export function step ( ) : void {
46
+ var w = width ,
47
+ h = height ;
48
+
43
49
var hm1 = h - 1 , // h - 1
44
50
wm1 = w - 1 ; // w - 1
45
51
@@ -55,9 +61,9 @@ export function step(): void {
55
61
// Every cell interacts with its eight neighbours, which are the cells that are horizontally,
56
62
// vertically, or diagonally adjacent. Least significant bit indicates alive or dead.
57
63
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 )
61
67
) ;
62
68
63
69
let self = get ( x , y ) ;
@@ -78,10 +84,10 @@ export function step(): void {
78
84
79
85
/** Fills the row and column indicated by `x` and `y` with random live cells. */
80
86
export 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 ) {
82
88
if ( Math . random ( ) < p ) set ( ix , y , BGR_ALIVE | 0xff000000 ) ;
83
89
}
84
- for ( let iy = 0 ; iy < h ; ++ iy ) {
90
+ for ( let iy = 0 ; iy < height ; ++ iy ) {
85
91
if ( Math . random ( ) < p ) set ( x , iy , BGR_ALIVE | 0xff000000 ) ;
86
92
}
87
93
}
0 commit comments