rust/mapgen/src/theme.rs
author alfadur
Wed, 07 Nov 2018 22:01:47 +0300
changeset 14165 c24a76f131d6
parent 14161 74ca70cb753d
child 14169 1749961647b9
permissions -rw-r--r--
implement basic land texturing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     1
use std::{
14161
74ca70cb753d fix mapgen
alfadur
parents: 14156
diff changeset
     2
    slice::from_raw_parts_mut,
14156
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
    ColorType,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    10
    Decoder,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    11
    DecodingError
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    12
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    13
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    14
use integral_geometry::Size;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    15
use vec2d::Vec2D;
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    16
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    17
pub struct ThemeSprite {
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    18
    pixels: Vec2D<u32>
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    19
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    20
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    21
impl ThemeSprite {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    22
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    23
    pub fn width(&self) -> usize {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    24
        self.pixels.size().width
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    25
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    26
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    27
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    28
    pub fn height(&self) -> usize {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    29
        self.pixels.size().height
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    30
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    31
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    32
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    33
    pub fn bounds(&self) -> Size {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    34
        self.pixels.size()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    35
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    36
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    37
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    38
    pub fn rows(&self) -> impl Iterator<Item = &[u32]> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    39
        self.pixels.rows()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    40
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    41
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    42
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    43
    pub fn get_row(&self, index: usize) -> &[u32] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    44
        &self.pixels[index]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    45
    }
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    46
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    47
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    48
pub struct Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    49
    land_texture: Option<ThemeSprite>
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    50
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    51
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    52
impl Theme {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    53
    pub fn land_texture(&self) -> Option<&ThemeSprite> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    54
        self.land_texture.as_ref()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    55
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    56
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    57
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    58
pub enum ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    59
    File(io::Error),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    60
    Decoding(DecodingError),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    61
    Format(String)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    62
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    63
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    64
impl From<io::Error> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    65
    fn from(e: io::Error) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    66
        ThemeLoadError::File(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    67
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    68
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    69
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    70
impl From<DecodingError> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    71
    fn from(e: DecodingError) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    72
        ThemeLoadError::Decoding(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    73
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    74
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    75
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    76
impl Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    77
    pub fn new() -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    78
        Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    79
            land_texture: None
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    80
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    81
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    82
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    83
    pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    84
        let mut theme = Self::new();
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    85
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    86
        for entry in read_dir(path)? {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    87
            let file = entry?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    88
            if file.file_name() == "LandTex.png" {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    89
                let buffer = BufReader::new(File::create(file.path())?);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    90
                let decoder = Decoder::new(buffer);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    91
                let (info, mut reader) = decoder.read_info()?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    92
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    93
                if info.color_type != ColorType::RGBA {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    94
                    return Err(ThemeLoadError::Format(
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    95
                        format!("Unexpected format: {:?}", info.color_type)));
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    96
                }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    97
                let size = Size::new(info.width as usize, info.height as usize);
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    98
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
    99
                let mut buffer: Vec2D<u32> = Vec2D::new(size, 0);
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
   100
                let slice_u32 = buffer.as_mut_slice();
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
   101
                let slice_u8 = unsafe {
14161
74ca70cb753d fix mapgen
alfadur
parents: 14156
diff changeset
   102
                    from_raw_parts_mut::<u8>(
74ca70cb753d fix mapgen
alfadur
parents: 14156
diff changeset
   103
                        slice_u32.as_mut_ptr() as *mut u8,
74ca70cb753d fix mapgen
alfadur
parents: 14156
diff changeset
   104
                        slice_u32.len() / 4
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   105
                    )
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   106
                };
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14161
diff changeset
   107
                reader.next_frame(slice_u8)?;
14156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   108
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   109
                let land_tex = ThemeSprite {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   110
                    pixels: buffer
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   111
                };
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   112
                theme.land_texture = Some(land_tex)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   113
            }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   114
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   115
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   116
        Ok(theme)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   117
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   118
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   119
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   120