author | GunChleoc |
Sat, 10 Nov 2018 19:53:40 +0100 | |
changeset 14192 | 3f6a6277f8e9 |
parent 14175 | 76a52e8149e3 |
child 14710 | 946df0bb3b28 |
permissions | -rw-r--r-- |
14151 | 1 |
use std::{ |
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
2 |
slice::{ |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
3 |
from_raw_parts, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
4 |
from_raw_parts_mut |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
5 |
}, |
14151 | 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 |
||
14160 | 17 |
use integral_geometry::Size; |
18 |
use vec2d::Vec2D; |
|
14151 | 19 |
|
20 |
pub struct ThemeSprite { |
|
14160 | 21 |
pixels: Vec2D<u32> |
22 |
} |
|
23 |
||
24 |
impl ThemeSprite { |
|
25 |
#[inline] |
|
14175 | 26 |
pub fn size(&self) -> Size { |
27 |
self.pixels.size() |
|
28 |
} |
|
29 |
||
30 |
#[inline] |
|
14160 | 31 |
pub fn width(&self) -> usize { |
14175 | 32 |
self.size().width |
14160 | 33 |
} |
34 |
||
35 |
#[inline] |
|
36 |
pub fn height(&self) -> usize { |
|
14175 | 37 |
self.size().height |
14160 | 38 |
} |
39 |
||
40 |
#[inline] |
|
14170 | 41 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> { |
14160 | 42 |
self.pixels.rows() |
43 |
} |
|
44 |
||
45 |
#[inline] |
|
46 |
pub fn get_row(&self, index: usize) -> &[u32] { |
|
47 |
&self.pixels[index] |
|
48 |
} |
|
14170 | 49 |
|
50 |
#[inline] |
|
51 |
pub fn get_pixel(&self, x: usize, y: usize) -> u32 { |
|
52 |
self.pixels[y][x] |
|
53 |
} |
|
14175 | 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 |
} |
|
14151 | 113 |
} |
114 |
||
115 |
pub struct Theme { |
|
14170 | 116 |
land_texture: Option<ThemeSprite>, |
117 |
border_texture: Option<ThemeSprite> |
|
14151 | 118 |
} |
119 |
||
14160 | 120 |
impl Theme { |
121 |
pub fn land_texture(&self) -> Option<&ThemeSprite> { |
|
122 |
self.land_texture.as_ref() |
|
123 |
} |
|
14170 | 124 |
|
125 |
pub fn border_texture(&self) -> Option<&ThemeSprite> { |
|
126 |
self.border_texture.as_ref() |
|
127 |
} |
|
14160 | 128 |
} |
129 |
||
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
130 |
#[derive(Debug)] |
14151 | 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 { |
|
14170 | 152 |
land_texture: None, |
153 |
border_texture: None, |
|
14151 | 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" { |
|
14170 | 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())?) |
|
14151 | 166 |
} |
167 |
} |
|
168 |
||
169 |
Ok(theme) |
|
170 |
} |
|
171 |
} |
|
172 |
||
14170 | 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 |
||
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
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:
14160
diff
changeset
|
191 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
192 |
from_raw_parts::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
193 |
slice_u32.as_ptr() as *const u8, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
194 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
195 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
196 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
197 |
} |
14151 | 198 |
|
14164
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
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:
14160
diff
changeset
|
200 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
201 |
from_raw_parts_mut::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
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:
14160
diff
changeset
|
203 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
204 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
205 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
206 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14160
diff
changeset
|
207 |