use crate::lib::*;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct RawTile {
pub index: usize,
pub color: Color,
}
impl Default for RawTile {
fn default() -> Self {
RawTile {
index: 0,
color: Color::WHITE,
}
}
}
pub(crate) fn dense_tiles_to_attributes(tiles: &[RawTile]) -> (Vec<f32>, Vec<[f32; 4]>) {
let capacity = tiles.len() * 4;
let mut tile_indexes: Vec<f32> = Vec::with_capacity(capacity);
let mut tile_colors: Vec<[f32; 4]> = Vec::with_capacity(capacity);
for tile in tiles.iter() {
tile_indexes.extend([tile.index as f32; 4].iter());
tile_colors.extend([tile.color.into(); 4].iter());
}
(tile_indexes, tile_colors)
}
pub(crate) fn sparse_tiles_to_attributes(
dimension: Dimension3,
tiles: &HashMap<usize, RawTile>,
) -> (Vec<f32>, Vec<[f32; 4]>) {
let area = (dimension.width * dimension.height) as usize;
let mut tile_indexes = vec![0.; area * 4];
let mut tile_colors = vec![[0.0, 0.0, 0.0, 0.0]; area * 4];
for (index, tile) in tiles.iter() {
for i in 0..4 {
if let Some(index) = tile_indexes.get_mut(index * 4 + i) {
*index = tile.index as f32;
}
if let Some(index) = tile_colors.get_mut(index * 4 + i) {
*index = tile.color.into();
}
}
}
(tile_indexes, tile_colors)
}