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