rust/mapgen/src/theme.rs
author alfadur
Wed, 07 Nov 2018 18:30:33 +0300
changeset 14156 74ca70cb753d
parent 14151 3c8a33ba06ba
child 14160 c24a76f131d6
permissions -rw-r--r--
fix mapgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     1
use std::{
14156
74ca70cb753d fix mapgen
alfadur
parents: 14151
diff changeset
     2
    slice::from_raw_parts_mut,
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     3
    io,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     4
    io::BufReader,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     5
    fs::{File, read_dir},
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     6
    path::Path
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     7
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     8
use png::{
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     9
    BitDepth,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    10
    ColorType,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    11
    Decoder,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    12
    DecodingError
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    13
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    14
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    15
use integral_geometry::{
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    16
    Rect, Size
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    17
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    18
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    19
pub struct ThemeSprite {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    20
    bounds: Size,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    21
    pixels: Vec<u32>
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    22
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    23
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    24
pub struct Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    25
    land_texture: Option<ThemeSprite>
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    26
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    27
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    28
pub enum ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    29
    File(io::Error),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    30
    Decoding(DecodingError),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    31
    Format(String)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    32
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    33
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    34
impl From<io::Error> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    35
    fn from(e: io::Error) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    36
        ThemeLoadError::File(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    37
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    38
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    39
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    40
impl From<DecodingError> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    41
    fn from(e: DecodingError) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    42
        ThemeLoadError::Decoding(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    43
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    44
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    45
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    46
impl Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    47
    pub fn new() -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    48
        Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    49
            land_texture: None
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    50
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    51
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    52
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    53
    pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    54
        let mut theme = Self::new();
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    55
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    56
        for entry in read_dir(path)? {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    57
            let file = entry?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    58
            if file.file_name() == "LandTex.png" {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    59
                let buffer = BufReader::new(File::create(file.path())?);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    60
                let decoder = Decoder::new(buffer);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    61
                let (info, mut reader) = decoder.read_info()?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    62
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    63
                if info.color_type != ColorType::RGBA {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    64
                    return Err(ThemeLoadError::Format(
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    65
                        format!("Unexpected format: {:?}", info.color_type)));
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    66
                }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    67
                let size = Size::new(info.width as usize, info.height as usize);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    68
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    69
                let mut buffer: Vec<u32> = Vec::with_capacity(size.area());
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    70
                let mut slice_u32 = buffer.as_mut_slice();
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    71
                let mut slice_u8 = unsafe {
14156
74ca70cb753d fix mapgen
alfadur
parents: 14151
diff changeset
    72
                    from_raw_parts_mut::<u8>(
74ca70cb753d fix mapgen
alfadur
parents: 14151
diff changeset
    73
                        slice_u32.as_mut_ptr() as *mut u8,
74ca70cb753d fix mapgen
alfadur
parents: 14151
diff changeset
    74
                        slice_u32.len() / 4
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    75
                    )
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    76
                };
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    77
                reader.next_frame(slice_u8);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    78
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    79
                let land_tex = ThemeSprite {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    80
                    bounds: size,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    81
                    pixels: buffer
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    82
                };
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    83
                theme.land_texture = Some(land_tex)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    84
            }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    85
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    86
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    87
        Ok(theme)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    88
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    89
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    90
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    91