summary refs log tree commit diff
path: root/src/lib.rs
blob: cbac12e262d889069dbe67a98316c4ac6422de73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use std::rc::Rc;

use pixels::wgpu::Color;
use pixels::PixelsBuilder;
use pixels::SurfaceTexture;
use rand::distributions::Distribution;
use rand::distributions::Standard;
use wasm_bindgen::prelude::*;
use winit::dpi::LogicalSize;
use winit::event::Event;
use winit::event_loop::EventLoop;
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;

// const WIDTH: usize = 256;
// const HEIGHT: usize = 256;

#[wasm_bindgen(start)]
async fn start() -> Result<(), JsValue> {
    return run().await.map_err(|err| JsValue::from(err.to_string()));
}

async fn run() -> anyhow::Result<()> {
    let event_loop = EventLoop::new();

    // let size = LogicalSize {
    //     width: WIDTH as f64,
    //     height: HEIGHT as f64,
    // };

    let window = WindowBuilder::new()
        // .with_inner_size(size)
        // .with_min_inner_size(size)
        .build(&event_loop)?;

    let window = Rc::new(window);

    #[cfg(target_arch = "wasm32")]
    {
        use winit::platform::web::WindowExtWebSys;

        // Retrieve current width and height dimensions of browser client window
        let get_window_size = || {
            let client_window = web_sys::window().unwrap();
            LogicalSize::new(
                client_window.inner_width().unwrap().as_f64().unwrap(),
                client_window.inner_height().unwrap().as_f64().unwrap(),
            )
        };

        let window = Rc::clone(&window);

        // Initialize winit window with current dimensions of browser client
        window.set_inner_size(get_window_size());

        let client_window = web_sys::window().unwrap();

        // Attach winit canvas to body element
        web_sys::window()
            .and_then(|win| win.document())
            .and_then(|doc| doc.body())
            .and_then(|body| {
                body.append_child(&web_sys::Element::from(window.canvas()))
                    .ok()
            })
            .expect("couldn't append canvas to document body");

        // Listen for resize event on browser client. Adjust winit window dimensions
        // on event trigger
        let closure = wasm_bindgen::closure::Closure::wrap(Box::new(move |_e: web_sys::Event| {
            let size = get_window_size();
            window.set_inner_size(size)
        }) as Box<dyn FnMut(_)>);
        client_window
            .add_event_listener_with_callback("resize", closure.as_ref().unchecked_ref())
            .unwrap();
        closure.forget();
    }

    let mut input = WinitInputHelper::new();

    let window_size = window.inner_size();
    let surface_texture =
        SurfaceTexture::new(window_size.width, window_size.height, window.as_ref());

    let width = window_size.width / 8;
    let height = window_size.height / 8;

    web_sys::console::log_1(&format!("{} x {}", width, height).into());

    let mut pixels = PixelsBuilder::new(width, height, surface_texture)
        .clear_color(Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        })
        .build_async()
        .await?;
    let mut life = Life::new(width as usize, height as usize);

    event_loop.run(move |event, _, control_flow| {
        control_flow.set_poll();

        if let Event::RedrawRequested(_) = event {
            life.draw(pixels.frame_mut());
            pixels.render().expect("failed to render");
        }

        if input.update(&event) {
            if input.close_requested() || input.destroyed() {
                control_flow.set_exit();
                return;
            };

            life.update();
            window.request_redraw();
        };
    });
}

#[derive(Clone, Copy, Debug)]
enum Cell {
    Alive,
    Dead { since: u8 },
}

#[derive(Debug)]
struct Grid {
    pub width: usize,
    pub height: usize,
    pub cells: Vec<Vec<Cell>>,
}

enum FrontGrid {
    A,
    B,
}

struct Life {
    pub width: usize,
    pub height: usize,
    grid_a: Grid,
    grid_b: Grid,
    front_grid: FrontGrid,
}

impl Cell {
    pub fn is_alive(&self) -> bool {
        match self {
            Cell::Alive => true,
            Cell::Dead { .. } => false,
        }
    }
}

impl Grid {
    pub fn blank(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            cells: vec![vec![Cell::Dead { since: 0xff }; width]; height],
        }
    }

    pub fn with_random_borders(width: usize, height: usize) -> Self {
        let mut ret = Self::blank(width, height);
        ret.randomize_border();
        ret
    }

    pub fn randomize_border(&mut self) {
        for col in 0..self.width {
            self.cells[self.height - 1][col] = rand::random();
        }
    }

    pub fn draw(&self, frame: &mut [u8]) {
        for row in 0..self.height {
            for col in 0..self.width {
                let pixel_idx = 4 * (self.width * row + col);

                match self.cells[row][col] {
                    Cell::Alive => {
                        frame[pixel_idx + 0] = 0xff; // R
                        frame[pixel_idx + 1] = 0xff; // G
                        frame[pixel_idx + 2] = 0xff; // B
                        frame[pixel_idx + 3] = 0xff; // A
                    }
                    Cell::Dead { since } => {
                        frame[pixel_idx + 0] = 0xff - since.saturating_mul(16); // R
                        frame[pixel_idx + 1] = if since < 0x8 {
                            0
                        } else {
                            (since - 0x8).saturating_mul(0x8) / 2
                        };
                        // G
                        frame[pixel_idx + 2] = 0xff; // B
                        frame[pixel_idx + 3] = 0xff - since; // A
                    }
                };
            }
        }
    }

    pub fn num_alive_neighbors(&self, row: usize, col: usize) -> u8 {
        let mut ret = 0;

        for i in row.saturating_sub(1)..=(row + 1).min(self.height - 1) {
            for j in col.saturating_sub(1)..=(col + 1).min(self.width - 1) {
                if self.cells[i][j].is_alive() {
                    ret += 1;
                }
            }
        }
        if self.cells[row][col].is_alive() {
            ret -= 1;
        }

        ret
    }

    pub fn new_state(&self, row: usize, col: usize) -> Cell {
        match self.cells[row][col] {
            Cell::Alive => {
                if (2..=3).contains(&self.num_alive_neighbors(row, col)) {
                    Cell::Alive
                } else {
                    Cell::Dead { since: 0 }
                }
            }
            Cell::Dead { since } => {
                if self.num_alive_neighbors(row, col) == 3 {
                    Cell::Alive
                } else {
                    Cell::Dead {
                        since: since.saturating_add(1),
                    }
                }
            }
        }
    }
}

impl Life {
    pub fn new(width: usize, height: usize) -> Self {
        Life {
            width,
            height,
            grid_a: Grid::with_random_borders(width, height),
            grid_b: Grid::blank(width, height),
            front_grid: FrontGrid::A,
        }
    }

    pub fn draw(&self, frame: &mut [u8]) {
        self.front_grid().draw(frame);
    }

    pub fn update(&mut self) {
        let (width, height) = self.dimensions();
        let (front, back) = self.grids();

        for row in 0..height {
            for col in 0..width {
                back.cells[row][col] = front.new_state(row, col);
            }
        }
        back.randomize_border();

        self.swap_grids();
    }

    fn dimensions(&self) -> (usize, usize) {
        (self.width, self.height)
    }

    fn grids(&mut self) -> (&Grid, &mut Grid) {
        match self.front_grid {
            FrontGrid::A => (&self.grid_a, &mut self.grid_b),
            FrontGrid::B => (&self.grid_b, &mut self.grid_a),
        }
    }

    fn front_grid(&self) -> &Grid {
        match self.front_grid {
            FrontGrid::A => &self.grid_a,
            FrontGrid::B => &self.grid_b,
        }
    }

    fn swap_grids(&mut self) {
        self.front_grid = match self.front_grid {
            FrontGrid::A => FrontGrid::B,
            FrontGrid::B => FrontGrid::A,
        }
    }
}

impl Distribution<Cell> for Standard {
    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Cell {
        if rng.gen() {
            Cell::Alive
        } else {
            Cell::Dead { since: 0xff }
        }
    }
}