rust/mapgen/src/theme.rs
author alfadur
Thu, 08 Nov 2018 07:15:22 +0300
changeset 14170 a4c1a2d0ac24
parent 14164 1749961647b9
child 14175 76a52e8149e3
permissions -rw-r--r--
implement basic land bordering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     1
use std::{
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
     2
    slice::{
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
     3
        from_raw_parts,
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
     4
        from_raw_parts_mut
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
     5
    },
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     6
    io,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     7
    io::BufReader,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     8
    fs::{File, read_dir},
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     9
    path::Path
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    10
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    11
use png::{
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    12
    ColorType,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    13
    Decoder,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    14
    DecodingError
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    15
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    16
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    17
use integral_geometry::Size;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    18
use vec2d::Vec2D;
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    19
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    20
pub struct ThemeSprite {
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    21
    pixels: Vec2D<u32>
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    22
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    23
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    24
impl ThemeSprite {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    25
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    26
    pub fn width(&self) -> usize {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    27
        self.pixels.size().width
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    28
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    29
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    30
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    31
    pub fn height(&self) -> usize {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    32
        self.pixels.size().height
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    33
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    34
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    35
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    36
    pub fn bounds(&self) -> Size {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    37
        self.pixels.size()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    38
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    39
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    40
    #[inline]
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    41
    pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    42
        self.pixels.rows()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    43
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    44
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    45
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    46
    pub fn get_row(&self, index: usize) -> &[u32] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    47
        &self.pixels[index]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    48
    }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    49
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    50
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    51
    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    52
        self.pixels[y][x]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    53
    }
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    54
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    55
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    56
pub struct Theme {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    57
    land_texture: Option<ThemeSprite>,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    58
    border_texture: Option<ThemeSprite>
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    59
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    60
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    61
impl Theme {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    62
    pub fn land_texture(&self) -> Option<&ThemeSprite> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    63
        self.land_texture.as_ref()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    64
    }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    65
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    66
    pub fn border_texture(&self) -> Option<&ThemeSprite> {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    67
        self.border_texture.as_ref()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    68
    }
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    69
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    70
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
    71
#[derive(Debug)]
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    72
pub enum ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    73
    File(io::Error),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    74
    Decoding(DecodingError),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    75
    Format(String)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    76
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    77
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    78
impl From<io::Error> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    79
    fn from(e: io::Error) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    80
        ThemeLoadError::File(e)
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
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    84
impl From<DecodingError> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    85
    fn from(e: DecodingError) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    86
        ThemeLoadError::Decoding(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    87
    }
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
impl Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    91
    pub fn new() -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    92
        Theme {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    93
            land_texture: None,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    94
            border_texture: None,
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    95
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    96
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    97
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    98
    pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    99
        let mut theme = Self::new();
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   100
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   101
        for entry in read_dir(path)? {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   102
            let file = entry?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   103
            if file.file_name() == "LandTex.png" {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   104
                theme.land_texture = Some(load_sprite(&file.path())?)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   105
            } else if file.file_name() == "Border.png" {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   106
                theme.border_texture = Some(load_sprite(&file.path())?)
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   107
            }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   108
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   109
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   110
        Ok(theme)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   111
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   112
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   113
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   114
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   115
    let decoder = Decoder::new(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   116
        BufReader::new(File::open(path)?));
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   117
    let (info, mut reader) = decoder.read_info()?;
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   118
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   119
    if info.color_type != ColorType::RGBA {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   120
        return Err(ThemeLoadError::Format(
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   121
            format!("Unexpected format: {:?}", info.color_type)));
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   122
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   123
    let size = Size::new(info.width as usize, info.height as usize);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   124
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   125
    let mut pixels: Vec2D<u32> = Vec2D::new(size, 0);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   126
    reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?;
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   127
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   128
    Ok(ThemeSprite { pixels })
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   129
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   130
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   131
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   132
    unsafe {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   133
        from_raw_parts::<u8>(
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   134
            slice_u32.as_ptr() as *const u8,
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   135
            slice_u32.len() * 4
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   136
        )
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   137
    }
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   138
}
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   139
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   140
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   141
    unsafe {
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   142
        from_raw_parts_mut::<u8>(
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   143
            slice_u32.as_mut_ptr() as *mut u8,
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   144
            slice_u32.len() * 4
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   145
        )
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   146
    }
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   147
}
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   148