rust/mapgen/src/theme.rs
author alfadur
Mon, 25 Mar 2024 00:23:41 +0300
changeset 15998 14fe5bfe9862
parent 15927 0710feb75c99
child 15975 2146cb7be36f
permissions -rw-r--r--
grow network buffer periodically
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15927
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
     1
use integral_geometry::{Point, Rect};
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     2
use png::{ColorType, Decoder, DecodingError};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     3
use std::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     4
    fs::{read_dir, File},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     5
    io,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     6
    io::BufReader,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     7
    path::Path,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     8
    slice::{from_raw_parts, from_raw_parts_mut},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
     9
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    11
use integral_geometry::Size;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    12
use vec2d::Vec2D;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    13
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    14
pub struct ThemeSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    15
    pixels: Vec2D<u32>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    16
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    17
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    18
impl ThemeSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    19
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    20
    pub fn size(&self) -> Size {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    21
        self.pixels.size()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    22
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    23
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    24
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    25
    pub fn width(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    26
        self.size().width
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    27
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    28
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    29
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    30
    pub fn height(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    31
        self.size().height
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    32
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    33
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    34
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    35
    pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    36
        self.pixels.rows()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    37
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    38
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    39
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    40
    pub fn get_row(&self, index: usize) -> &[u32] {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    41
        &self.pixels[index]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    42
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    43
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    44
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    45
    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    46
        self.pixels[y][x]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    47
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    48
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    49
    pub fn to_transposed(&self) -> ThemeSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    50
        let size = self.size().transpose();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    51
        let mut pixels = Vec2D::new(size, 0u32);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    52
        for (y, row) in self.pixels.rows().enumerate() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    53
            for (x, v) in row.iter().enumerate() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    54
                pixels[x][y] = *v;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    55
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    56
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    57
        ThemeSprite { pixels }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    58
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    59
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    60
    pub fn to_tiled(&self) -> TiledSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    61
        let size = self.size();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    62
        assert!(size.is_power_of_two());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    63
        let tile_width_shift = size.width.trailing_zeros() as usize + 2;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    64
        let mut pixels = vec![0u32; size.area()];
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    65
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    66
        for (y, row) in self.pixels.rows().enumerate() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    67
            for (x, v) in row.iter().enumerate() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    68
                pixels[get_tiled_index(x, y, tile_width_shift)] = *v;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    69
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    70
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    71
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    72
        TiledSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    73
            tile_width_shift,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    74
            size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    75
            pixels,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    76
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    77
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    78
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    79
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    80
#[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    81
fn get_tiled_index(x: usize, y: usize, tile_width_shift: usize) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    82
    (((y >> 2) << tile_width_shift) + ((x >> 2) << 4)) + ((y & 0b11) << 2) + (x & 0b11)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    83
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    84
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    85
pub struct TiledSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    86
    tile_width_shift: usize,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    87
    size: Size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    88
    pixels: Vec<u32>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    89
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    90
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    91
impl TiledSprite {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    92
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    93
    pub fn size(&self) -> Size {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    94
        self.size
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    95
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    96
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    97
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    98
    pub fn width(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
    99
        self.size().width
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   100
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   101
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   102
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   103
    pub fn height(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   104
        self.size().height
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   105
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   106
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   107
    #[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   108
    pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   109
        self.pixels[get_tiled_index(x, y, self.tile_width_shift)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   110
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   111
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   112
15927
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   113
#[derive(Default)]
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   114
struct Color(u8, u8, u8, u8);
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   115
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   116
pub struct LandObjectOverlay {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   117
    texture: ThemeSprite,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   118
    offset: Point,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   119
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   120
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   121
pub struct LandObject {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   122
    texture: ThemeSprite,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   123
    inland_rects: Vec<Rect>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   124
    outland_rects: Vec<Rect>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   125
    anchors: Vec<Rect>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   126
    overlays: Vec<LandObjectOverlay>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   127
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   128
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   129
pub struct LandSpray {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   130
    texture: ThemeSprite,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   131
    count: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   132
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   133
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   134
#[derive(Default)]
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   135
pub struct ThemeColors {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   136
    border: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   137
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   138
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   139
pub struct Flakes {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   140
    texture: ThemeSprite,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   141
    frames_count: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   142
    frame_ticks: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   143
    velocity: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   144
    fall_speed: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   145
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   146
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   147
#[derive(Default)]
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   148
pub struct Water {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   149
    top_color: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   150
    bottom_color: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   151
    opacity: u8,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   152
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   153
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   154
#[derive(Default)]
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   155
pub struct ThemeParts {
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   156
    water: Water,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   157
    flakes: Option<Flakes>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   158
    music: String,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   159
    sky: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   160
    tint: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   161
}
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   162
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   163
#[derive(Default)]
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   164
pub struct Theme {
15927
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   165
    border_color: Color,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   166
    clouds_count: u16,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   167
    flatten_flakes: bool,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   168
    land_texture: Option<ThemeSprite>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   169
    border_texture: Option<ThemeSprite>,
15927
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   170
    land_objects: Vec<LandObject>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   171
    spays: Vec<LandSpray>,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   172
    use_ice: bool,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   173
    use_snow: bool,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   174
    music: String,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   175
    normal_parts: ThemeParts,
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   176
    sd_parts: ThemeParts,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   177
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   178
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   179
impl Theme {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   180
    pub fn land_texture(&self) -> Option<&ThemeSprite> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   181
        self.land_texture.as_ref()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   182
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   183
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   184
    pub fn border_texture(&self) -> Option<&ThemeSprite> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   185
        self.border_texture.as_ref()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   186
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   187
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   188
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   189
#[derive(Debug)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   190
pub enum ThemeLoadError {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   191
    File(io::Error),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   192
    Decoding(DecodingError),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   193
    Format(String),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   194
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   195
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   196
impl From<io::Error> for ThemeLoadError {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   197
    fn from(e: io::Error) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   198
        ThemeLoadError::File(e)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   199
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   200
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   201
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   202
impl From<DecodingError> for ThemeLoadError {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   203
    fn from(e: DecodingError) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   204
        ThemeLoadError::Decoding(e)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   205
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   206
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   207
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   208
impl Theme {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   209
    pub fn new() -> Self {
15927
0710feb75c99 expand theme definition
alfadur
parents: 15120
diff changeset
   210
        Default::default()
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   211
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   212
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   213
    pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   214
        let mut theme = Self::new();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   215
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   216
        for entry in read_dir(path)? {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   217
            let file = entry?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   218
            if file.file_name() == "LandTex.png" {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   219
                theme.land_texture = Some(load_sprite(&file.path())?)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   220
            } else if file.file_name() == "Border.png" {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   221
                theme.border_texture = Some(load_sprite(&file.path())?)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   222
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   223
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   224
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   225
        Ok(theme)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   226
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   227
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   228
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   229
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   230
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   231
    let (info, mut reader) = decoder.read_info()?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   232
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   233
    if info.color_type != ColorType::RGBA {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   234
        return Err(ThemeLoadError::Format(format!(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   235
            "Unexpected format: {:?}",
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   236
            info.color_type
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   237
        )));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   238
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   239
    let size = Size::new(info.width as usize, info.height as usize);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   240
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   241
    let mut pixels: Vec2D<u32> = Vec2D::new(size, 0);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   242
    reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   243
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   244
    Ok(ThemeSprite { pixels })
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   245
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   246
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   247
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   248
    unsafe { from_raw_parts::<u8>(slice_u32.as_ptr() as *const u8, slice_u32.len() * 4) }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   249
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   250
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   251
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   252
    unsafe { from_raw_parts_mut::<u8>(slice_u32.as_mut_ptr() as *mut u8, slice_u32.len() * 4) }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14710
diff changeset
   253
}