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