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