author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 02 Jun 2019 18:22:06 +0200 | |
changeset 15102 | 260e96addf92 |
parent 14715 | 946df0bb3b28 |
child 15125 | febccab419b1 |
permissions | -rw-r--r-- |
14715 | 1 |
use png::{ColorType, Decoder, DecodingError}; |
14156 | 2 |
use std::{ |
14715 | 3 |
fs::{read_dir, File}, |
14156 | 4 |
io, |
5 |
io::BufReader, |
|
14715 | 6 |
path::Path, |
7 |
slice::{from_raw_parts, from_raw_parts_mut}, |
|
14156 | 8 |
}; |
9 |
||
14165 | 10 |
use integral_geometry::Size; |
11 |
use vec2d::Vec2D; |
|
14156 | 12 |
|
13 |
pub struct ThemeSprite { |
|
14715 | 14 |
pixels: Vec2D<u32>, |
14165 | 15 |
} |
16 |
||
17 |
impl ThemeSprite { |
|
18 |
#[inline] |
|
14180 | 19 |
pub fn size(&self) -> Size { |
20 |
self.pixels.size() |
|
21 |
} |
|
22 |
||
23 |
#[inline] |
|
14165 | 24 |
pub fn width(&self) -> usize { |
14180 | 25 |
self.size().width |
14165 | 26 |
} |
27 |
||
28 |
#[inline] |
|
29 |
pub fn height(&self) -> usize { |
|
14180 | 30 |
self.size().height |
14165 | 31 |
} |
32 |
||
33 |
#[inline] |
|
14175 | 34 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> { |
14165 | 35 |
self.pixels.rows() |
36 |
} |
|
37 |
||
38 |
#[inline] |
|
39 |
pub fn get_row(&self, index: usize) -> &[u32] { |
|
40 |
&self.pixels[index] |
|
41 |
} |
|
14175 | 42 |
|
43 |
#[inline] |
|
44 |
pub fn get_pixel(&self, x: usize, y: usize) -> u32 { |
|
45 |
self.pixels[y][x] |
|
46 |
} |
|
14180 | 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 |
||
14715 | 71 |
TiledSprite { |
72 |
tile_width_shift, |
|
73 |
size, |
|
74 |
pixels, |
|
75 |
} |
|
14180 | 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, |
|
14715 | 87 |
pixels: Vec<u32>, |
14180 | 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 |
} |
|
14156 | 110 |
} |
111 |
||
112 |
pub struct Theme { |
|
14175 | 113 |
land_texture: Option<ThemeSprite>, |
14715 | 114 |
border_texture: Option<ThemeSprite>, |
14156 | 115 |
} |
116 |
||
14165 | 117 |
impl Theme { |
118 |
pub fn land_texture(&self) -> Option<&ThemeSprite> { |
|
119 |
self.land_texture.as_ref() |
|
120 |
} |
|
14175 | 121 |
|
122 |
pub fn border_texture(&self) -> Option<&ThemeSprite> { |
|
123 |
self.border_texture.as_ref() |
|
124 |
} |
|
14165 | 125 |
} |
126 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
127 |
#[derive(Debug)] |
14156 | 128 |
pub enum ThemeLoadError { |
129 |
File(io::Error), |
|
130 |
Decoding(DecodingError), |
|
14715 | 131 |
Format(String), |
14156 | 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 { |
|
14175 | 149 |
land_texture: None, |
150 |
border_texture: None, |
|
14156 | 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" { |
|
14175 | 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())?) |
|
14156 | 163 |
} |
164 |
} |
|
165 |
||
166 |
Ok(theme) |
|
167 |
} |
|
168 |
} |
|
169 |
||
14175 | 170 |
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> { |
14715 | 171 |
let decoder = Decoder::new(BufReader::new(File::open(path)?)); |
14175 | 172 |
let (info, mut reader) = decoder.read_info()?; |
173 |
||
174 |
if info.color_type != ColorType::RGBA { |
|
14715 | 175 |
return Err(ThemeLoadError::Format(format!( |
176 |
"Unexpected format: {:?}", |
|
177 |
info.color_type |
|
178 |
))); |
|
14175 | 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 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
188 |
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] { |
14715 | 189 |
unsafe { from_raw_parts::<u8>(slice_u32.as_ptr() as *const u8, slice_u32.len() * 4) } |
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
190 |
} |
14156 | 191 |
|
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
192 |
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] { |
14715 | 193 |
unsafe { from_raw_parts_mut::<u8>(slice_u32.as_mut_ptr() as *mut u8, slice_u32.len() * 4) } |
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
194 |
} |