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