rust/mapgen/src/theme.rs
author alfadur
Sun, 02 Jun 2019 22:40:39 +0300
changeset 15099 07de9e60c305
parent 14710 946df0bb3b28
child 15120 febccab419b1
permissions -rw-r--r--
fix land object placement
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
     1
use png::{ColorType, Decoder, DecodingError};
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     2
use std::{
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
     3
    fs::{read_dir, File},
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     4
    io,
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     5
    io::BufReader,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
     6
    path::Path,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
     7
    slice::{from_raw_parts, from_raw_parts_mut},
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     8
};
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
     9
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    10
use integral_geometry::Size;
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    11
use vec2d::Vec2D;
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    12
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
    13
pub struct ThemeSprite {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    14
    pixels: Vec2D<u32>,
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    15
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    16
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    17
impl ThemeSprite {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    18
    #[inline]
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    19
    pub fn size(&self) -> Size {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    20
        self.pixels.size()
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    21
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    22
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    23
    #[inline]
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    24
    pub fn width(&self) -> usize {
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    25
        self.size().width
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    26
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    27
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    28
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    29
    pub fn height(&self) -> usize {
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    30
        self.size().height
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    31
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    32
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    33
    #[inline]
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    34
    pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    35
        self.pixels.rows()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    36
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    37
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    38
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    39
    pub fn get_row(&self, index: usize) -> &[u32] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    40
        &self.pixels[index]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
    41
    }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    42
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    43
    #[inline]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    44
    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    45
        self.pixels[y][x]
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
    46
    }
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    47
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    48
    pub fn to_transposed(&self) -> ThemeSprite {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    49
        let size = self.size().transpose();
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    50
        let mut pixels = Vec2D::new(size, 0u32);
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    51
        for (y, row) in self.pixels.rows().enumerate() {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    52
            for (x, v) in row.iter().enumerate() {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    53
                pixels[x][y] = *v;
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    54
            }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    55
        }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    56
        ThemeSprite { pixels }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    57
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    58
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    59
    pub fn to_tiled(&self) -> TiledSprite {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    60
        let size = self.size();
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    61
        assert!(size.is_power_of_two());
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    62
        let tile_width_shift = size.width.trailing_zeros() as usize + 2;
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    63
        let mut pixels = vec![0u32; size.area()];
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    64
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    65
        for (y, row) in self.pixels.rows().enumerate() {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    66
            for (x, v) in row.iter().enumerate() {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    67
                pixels[get_tiled_index(x, y, tile_width_shift)] = *v;
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    68
            }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    69
        }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    70
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    71
        TiledSprite {
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    72
            tile_width_shift,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    73
            size,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    74
            pixels,
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    75
        }
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    76
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    77
}
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    78
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    79
#[inline]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    80
fn get_tiled_index(x: usize, y: usize, tile_width_shift: usize) -> usize {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    81
    (((y >> 2) << tile_width_shift) + ((x >> 2) << 4)) + ((y & 0b11) << 2) + (x & 0b11)
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    82
}
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    83
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    84
pub struct TiledSprite {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    85
    tile_width_shift: usize,
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    86
    size: Size,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
    87
    pixels: Vec<u32>,
14175
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    88
}
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    89
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    90
impl TiledSprite {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    91
    #[inline]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    92
    pub fn size(&self) -> Size {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    93
        self.size
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    94
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    95
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    96
    #[inline]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    97
    pub fn width(&self) -> usize {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    98
        self.size().width
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
    99
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   100
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   101
    #[inline]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   102
    pub fn height(&self) -> usize {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   103
        self.size().height
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   104
    }
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   105
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   106
    #[inline]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   107
    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   108
        self.pixels[get_tiled_index(x, y, self.tile_width_shift)]
76a52e8149e3 add some texture transforms
alfadur
parents: 14170
diff changeset
   109
    }
14151
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
pub struct Theme {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   113
    land_texture: Option<ThemeSprite>,
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   114
    border_texture: Option<ThemeSprite>,
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   115
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   116
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   117
impl Theme {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   118
    pub fn land_texture(&self) -> Option<&ThemeSprite> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   119
        self.land_texture.as_ref()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   120
    }
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   121
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   122
    pub fn border_texture(&self) -> Option<&ThemeSprite> {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   123
        self.border_texture.as_ref()
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   124
    }
14160
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   125
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14156
diff changeset
   126
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   127
#[derive(Debug)]
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   128
pub enum ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   129
    File(io::Error),
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   130
    Decoding(DecodingError),
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   131
    Format(String),
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   132
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   133
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   134
impl From<io::Error> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   135
    fn from(e: io::Error) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   136
        ThemeLoadError::File(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   137
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   138
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   139
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   140
impl From<DecodingError> for ThemeLoadError {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   141
    fn from(e: DecodingError) -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   142
        ThemeLoadError::Decoding(e)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   143
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   144
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   145
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   146
impl Theme {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   147
    pub fn new() -> Self {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   148
        Theme {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   149
            land_texture: None,
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   150
            border_texture: None,
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   151
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   152
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   153
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   154
    pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   155
        let mut theme = Self::new();
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   156
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   157
        for entry in read_dir(path)? {
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   158
            let file = entry?;
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   159
            if file.file_name() == "LandTex.png" {
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   160
                theme.land_texture = Some(load_sprite(&file.path())?)
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   161
            } else if file.file_name() == "Border.png" {
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   162
                theme.border_texture = Some(load_sprite(&file.path())?)
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   163
            }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   164
        }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   165
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   166
        Ok(theme)
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   167
    }
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   168
}
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   169
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   170
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   171
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   172
    let (info, mut reader) = decoder.read_info()?;
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   173
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   174
    if info.color_type != ColorType::RGBA {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   175
        return Err(ThemeLoadError::Format(format!(
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   176
            "Unexpected format: {:?}",
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   177
            info.color_type
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   178
        )));
14170
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   179
    }
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   180
    let size = Size::new(info.width as usize, info.height as usize);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   181
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   182
    let mut pixels: Vec2D<u32> = Vec2D::new(size, 0);
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   183
    reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?;
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   184
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   185
    Ok(ThemeSprite { pixels })
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   186
}
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14164
diff changeset
   187
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   188
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   189
    unsafe { from_raw_parts::<u8>(slice_u32.as_ptr() as *const u8, slice_u32.len() * 4) }
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   190
}
14151
3c8a33ba06ba start loading theme textures
alfadur
parents:
diff changeset
   191
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   192
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] {
14710
946df0bb3b28 collapse mapgen back
alfadur
parents: 14175
diff changeset
   193
    unsafe { from_raw_parts_mut::<u8>(slice_u32.as_mut_ptr() as *mut u8, slice_u32.len() * 4) }
14164
1749961647b9 fix texturing and add a theme loading option to land_dump
alfadur
parents: 14160
diff changeset
   194
}