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