rust/mapgen/src/theme.rs
changeset 14165 c24a76f131d6
parent 14161 74ca70cb753d
child 14169 1749961647b9
equal deleted inserted replaced
14164:0aeea29ef890 14165:c24a76f131d6
     4     io::BufReader,
     4     io::BufReader,
     5     fs::{File, read_dir},
     5     fs::{File, read_dir},
     6     path::Path
     6     path::Path
     7 };
     7 };
     8 use png::{
     8 use png::{
     9     BitDepth,
       
    10     ColorType,
     9     ColorType,
    11     Decoder,
    10     Decoder,
    12     DecodingError
    11     DecodingError
    13 };
    12 };
    14 
    13 
    15 use integral_geometry::{
    14 use integral_geometry::Size;
    16     Rect, Size
    15 use vec2d::Vec2D;
    17 };
       
    18 
    16 
    19 pub struct ThemeSprite {
    17 pub struct ThemeSprite {
    20     bounds: Size,
    18     pixels: Vec2D<u32>
    21     pixels: Vec<u32>
    19 }
       
    20 
       
    21 impl ThemeSprite {
       
    22     #[inline]
       
    23     pub fn width(&self) -> usize {
       
    24         self.pixels.size().width
       
    25     }
       
    26 
       
    27     #[inline]
       
    28     pub fn height(&self) -> usize {
       
    29         self.pixels.size().height
       
    30     }
       
    31 
       
    32     #[inline]
       
    33     pub fn bounds(&self) -> Size {
       
    34         self.pixels.size()
       
    35     }
       
    36 
       
    37     #[inline]
       
    38     pub fn rows(&self) -> impl Iterator<Item = &[u32]> {
       
    39         self.pixels.rows()
       
    40     }
       
    41 
       
    42     #[inline]
       
    43     pub fn get_row(&self, index: usize) -> &[u32] {
       
    44         &self.pixels[index]
       
    45     }
    22 }
    46 }
    23 
    47 
    24 pub struct Theme {
    48 pub struct Theme {
    25     land_texture: Option<ThemeSprite>
    49     land_texture: Option<ThemeSprite>
       
    50 }
       
    51 
       
    52 impl Theme {
       
    53     pub fn land_texture(&self) -> Option<&ThemeSprite> {
       
    54         self.land_texture.as_ref()
       
    55     }
    26 }
    56 }
    27 
    57 
    28 pub enum ThemeLoadError {
    58 pub enum ThemeLoadError {
    29     File(io::Error),
    59     File(io::Error),
    30     Decoding(DecodingError),
    60     Decoding(DecodingError),
    64                     return Err(ThemeLoadError::Format(
    94                     return Err(ThemeLoadError::Format(
    65                         format!("Unexpected format: {:?}", info.color_type)));
    95                         format!("Unexpected format: {:?}", info.color_type)));
    66                 }
    96                 }
    67                 let size = Size::new(info.width as usize, info.height as usize);
    97                 let size = Size::new(info.width as usize, info.height as usize);
    68 
    98 
    69                 let mut buffer: Vec<u32> = Vec::with_capacity(size.area());
    99                 let mut buffer: Vec2D<u32> = Vec2D::new(size, 0);
    70                 let mut slice_u32 = buffer.as_mut_slice();
   100                 let slice_u32 = buffer.as_mut_slice();
    71                 let mut slice_u8 = unsafe {
   101                 let slice_u8 = unsafe {
    72                     from_raw_parts_mut::<u8>(
   102                     from_raw_parts_mut::<u8>(
    73                         slice_u32.as_mut_ptr() as *mut u8,
   103                         slice_u32.as_mut_ptr() as *mut u8,
    74                         slice_u32.len() / 4
   104                         slice_u32.len() / 4
    75                     )
   105                     )
    76                 };
   106                 };
    77                 reader.next_frame(slice_u8);
   107                 reader.next_frame(slice_u8)?;
    78 
   108 
    79                 let land_tex = ThemeSprite {
   109                 let land_tex = ThemeSprite {
    80                     bounds: size,
       
    81                     pixels: buffer
   110                     pixels: buffer
    82                 };
   111                 };
    83                 theme.land_texture = Some(land_tex)
   112                 theme.land_texture = Some(land_tex)
    84             }
   113             }
    85         }
   114         }