DEV Community

Edmond-keaton
Edmond-keaton

Posted on

How to draw the province borders

I'm making an AoH2-like strategy game. I have a provinces.bmp image where each province has a unique color. The border coordinates are currently extracted by looping through pixels and checking neighbors. How can I draw the borders and fill the provinces with color? Also, is there a better way to extract border coords?
fn draw_borders(mut commands: Commands) {
let img =
image::open("assets/maps/testmap/provinces.bmp").expect("Failed to open provinces.bmp");
let (width, height) = img.dimensions();

let mut pixels: Vec<(u8, u8, u8)> = Vec::with_capacity((width * height) as usize);
for (_, _, pixel) in img.pixels() {
    pixels.push((pixel[0], pixel[1], pixel[2]))
}

let mut border_coords = Vec::new();
for y in 0..height {
    for x in 0..width {
        let current = pixels[(y * width + y) as usize];

        let neighbors = [
            (x.saturating_sub(1), y),
            ((x + 1).min(width - 1), y),
            (x, y.saturating_sub(1)),
            (x, (y + 1).min(height - 1)),
        ];

        for &(nx, ny) in neighbors.iter() {
            if pixels[(ny * width + nx) as usize] != current {
                border_coords.push((x, y));
                break;
            }
        }
    }
}

let border_coords: HashSet<_> = border_coords.into_iter().collect(); // remove duplicates
// render borders
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)