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