๐Ÿ“ฆ AlistairKeiller / rustoracer

๐Ÿ“„ render.rs ยท 24 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24use crate::car::Car;
use crate::map::OccGrid;

pub fn render_rgb(map: &OccGrid, cars: &[Car]) -> (Vec<u8>, u32, u32) {
    let (w, h) = (map.img.width(), map.img.height());
    let mut buf = vec![0u8; (h * w * 3) as usize];
    for (i, p) in map.img.pixels().enumerate() {
        buf[i * 3..][..3].fill(p.0[0]);
    }
    for car in cars {
        for (x, y) in map.car_pixels(car) {
            set_px(&mut buf, w, h, x, y, [43, 127, 255]);
        }
    }
    (buf, h, w)
}

fn set_px(buf: &mut [u8], w: u32, h: u32, x: u32, y: u32, c: [u8; 3]) {
    if (0..w).contains(&x) && (0..h).contains(&y) {
        let i = (y as usize * w as usize + x as usize) * 3;
        buf[i..i + 3].copy_from_slice(&c);
    }
}