rust/mapgen/src/theme.rs
changeset 14175 76a52e8149e3
parent 14170 a4c1a2d0ac24
child 14710 946df0bb3b28
equal deleted inserted replaced
14174:fac275de34e5 14175:76a52e8149e3
    21     pixels: Vec2D<u32>
    21     pixels: Vec2D<u32>
    22 }
    22 }
    23 
    23 
    24 impl ThemeSprite {
    24 impl ThemeSprite {
    25     #[inline]
    25     #[inline]
       
    26     pub fn size(&self) -> Size {
       
    27         self.pixels.size()
       
    28     }
       
    29 
       
    30     #[inline]
    26     pub fn width(&self) -> usize {
    31     pub fn width(&self) -> usize {
    27         self.pixels.size().width
    32         self.size().width
    28     }
    33     }
    29 
    34 
    30     #[inline]
    35     #[inline]
    31     pub fn height(&self) -> usize {
    36     pub fn height(&self) -> usize {
    32         self.pixels.size().height
    37         self.size().height
    33     }
       
    34 
       
    35     #[inline]
       
    36     pub fn bounds(&self) -> Size {
       
    37         self.pixels.size()
       
    38     }
    38     }
    39 
    39 
    40     #[inline]
    40     #[inline]
    41     pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
    41     pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
    42         self.pixels.rows()
    42         self.pixels.rows()
    48     }
    48     }
    49 
    49 
    50     #[inline]
    50     #[inline]
    51     pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
    51     pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
    52         self.pixels[y][x]
    52         self.pixels[y][x]
       
    53     }
       
    54 
       
    55     pub fn to_transposed(&self) -> ThemeSprite {
       
    56         let size = self.size().transpose();
       
    57         let mut pixels = Vec2D::new(size, 0u32);
       
    58         for (y, row) in self.pixels.rows().enumerate() {
       
    59             for (x, v) in row.iter().enumerate() {
       
    60                 pixels[x][y] = *v;
       
    61             }
       
    62         }
       
    63         ThemeSprite { pixels }
       
    64     }
       
    65 
       
    66     pub fn to_tiled(&self) -> TiledSprite {
       
    67         let size = self.size();
       
    68         assert!(size.is_power_of_two());
       
    69         let tile_width_shift = size.width.trailing_zeros() as usize + 2;
       
    70         let mut pixels = vec![0u32; size.area()];
       
    71 
       
    72         for (y, row) in self.pixels.rows().enumerate() {
       
    73             for (x, v) in row.iter().enumerate() {
       
    74                 pixels[get_tiled_index(x, y, tile_width_shift)] = *v;
       
    75             }
       
    76         }
       
    77 
       
    78         TiledSprite { tile_width_shift, size, pixels }
       
    79     }
       
    80 }
       
    81 
       
    82 #[inline]
       
    83 fn get_tiled_index(x: usize, y: usize, tile_width_shift: usize) -> usize {
       
    84     (((y >> 2) << tile_width_shift) + ((x >> 2) << 4)) + ((y & 0b11) << 2) + (x & 0b11)
       
    85 }
       
    86 
       
    87 pub struct TiledSprite {
       
    88     tile_width_shift: usize,
       
    89     size: Size,
       
    90     pixels: Vec<u32>
       
    91 }
       
    92 
       
    93 impl TiledSprite {
       
    94     #[inline]
       
    95     pub fn size(&self) -> Size {
       
    96         self.size
       
    97     }
       
    98 
       
    99     #[inline]
       
   100     pub fn width(&self) -> usize {
       
   101         self.size().width
       
   102     }
       
   103 
       
   104     #[inline]
       
   105     pub fn height(&self) -> usize {
       
   106         self.size().height
       
   107     }
       
   108 
       
   109     #[inline]
       
   110     pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
       
   111         self.pixels[get_tiled_index(x, y, self.tile_width_shift)]
    53     }
   112     }
    54 }
   113 }
    55 
   114 
    56 pub struct Theme {
   115 pub struct Theme {
    57     land_texture: Option<ThemeSprite>,
   116     land_texture: Option<ThemeSprite>,