15927
|
1 |
use integral_geometry::{Point, Rect};
|
15120
|
2 |
use png::{ColorType, Decoder, DecodingError};
|
|
3 |
use std::{
|
|
4 |
fs::{read_dir, File},
|
|
5 |
io,
|
|
6 |
io::BufReader,
|
|
7 |
path::Path,
|
|
8 |
slice::{from_raw_parts, from_raw_parts_mut},
|
|
9 |
};
|
|
10 |
|
|
11 |
use integral_geometry::Size;
|
|
12 |
use vec2d::Vec2D;
|
|
13 |
|
|
14 |
pub struct ThemeSprite {
|
|
15 |
pixels: Vec2D<u32>,
|
|
16 |
}
|
|
17 |
|
|
18 |
impl ThemeSprite {
|
|
19 |
#[inline]
|
|
20 |
pub fn size(&self) -> Size {
|
|
21 |
self.pixels.size()
|
|
22 |
}
|
|
23 |
|
|
24 |
#[inline]
|
|
25 |
pub fn width(&self) -> usize {
|
|
26 |
self.size().width
|
|
27 |
}
|
|
28 |
|
|
29 |
#[inline]
|
|
30 |
pub fn height(&self) -> usize {
|
|
31 |
self.size().height
|
|
32 |
}
|
|
33 |
|
|
34 |
#[inline]
|
|
35 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> {
|
|
36 |
self.pixels.rows()
|
|
37 |
}
|
|
38 |
|
|
39 |
#[inline]
|
|
40 |
pub fn get_row(&self, index: usize) -> &[u32] {
|
|
41 |
&self.pixels[index]
|
|
42 |
}
|
|
43 |
|
|
44 |
#[inline]
|
|
45 |
pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
|
|
46 |
self.pixels[y][x]
|
|
47 |
}
|
|
48 |
|
|
49 |
pub fn to_transposed(&self) -> ThemeSprite {
|
|
50 |
let size = self.size().transpose();
|
15914
|
51 |
let mut pixels = Vec2D::new(&size, 0u32);
|
15120
|
52 |
for (y, row) in self.pixels.rows().enumerate() {
|
|
53 |
for (x, v) in row.iter().enumerate() {
|
|
54 |
pixels[x][y] = *v;
|
|
55 |
}
|
|
56 |
}
|
|
57 |
ThemeSprite { pixels }
|
|
58 |
}
|
|
59 |
|
|
60 |
pub fn to_tiled(&self) -> TiledSprite {
|
|
61 |
let size = self.size();
|
|
62 |
assert!(size.is_power_of_two());
|
|
63 |
let tile_width_shift = size.width.trailing_zeros() as usize + 2;
|
|
64 |
let mut pixels = vec![0u32; size.area()];
|
|
65 |
|
|
66 |
for (y, row) in self.pixels.rows().enumerate() {
|
|
67 |
for (x, v) in row.iter().enumerate() {
|
|
68 |
pixels[get_tiled_index(x, y, tile_width_shift)] = *v;
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
TiledSprite {
|
|
73 |
tile_width_shift,
|
|
74 |
size,
|
|
75 |
pixels,
|
|
76 |
}
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
#[inline]
|
|
81 |
fn get_tiled_index(x: usize, y: usize, tile_width_shift: usize) -> usize {
|
|
82 |
(((y >> 2) << tile_width_shift) + ((x >> 2) << 4)) + ((y & 0b11) << 2) + (x & 0b11)
|
|
83 |
}
|
|
84 |
|
|
85 |
pub struct TiledSprite {
|
|
86 |
tile_width_shift: usize,
|
|
87 |
size: Size,
|
|
88 |
pixels: Vec<u32>,
|
|
89 |
}
|
|
90 |
|
|
91 |
impl TiledSprite {
|
|
92 |
#[inline]
|
|
93 |
pub fn size(&self) -> Size {
|
|
94 |
self.size
|
|
95 |
}
|
|
96 |
|
|
97 |
#[inline]
|
|
98 |
pub fn width(&self) -> usize {
|
|
99 |
self.size().width
|
|
100 |
}
|
|
101 |
|
|
102 |
#[inline]
|
|
103 |
pub fn height(&self) -> usize {
|
|
104 |
self.size().height
|
|
105 |
}
|
|
106 |
|
|
107 |
#[inline]
|
|
108 |
pub fn get_pixel(&self, x: usize, y: usize) -> u32 {
|
|
109 |
self.pixels[get_tiled_index(x, y, self.tile_width_shift)]
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
15927
|
113 |
#[derive(Default)]
|
|
114 |
struct Color(u8, u8, u8, u8);
|
|
115 |
|
|
116 |
pub struct LandObjectOverlay {
|
|
117 |
texture: ThemeSprite,
|
|
118 |
offset: Point,
|
|
119 |
}
|
|
120 |
|
|
121 |
pub struct LandObject {
|
|
122 |
texture: ThemeSprite,
|
|
123 |
inland_rects: Vec<Rect>,
|
|
124 |
outland_rects: Vec<Rect>,
|
|
125 |
anchors: Vec<Rect>,
|
|
126 |
overlays: Vec<LandObjectOverlay>,
|
|
127 |
}
|
|
128 |
|
|
129 |
pub struct LandSpray {
|
|
130 |
texture: ThemeSprite,
|
|
131 |
count: u16,
|
|
132 |
}
|
|
133 |
|
|
134 |
#[derive(Default)]
|
|
135 |
pub struct ThemeColors {
|
|
136 |
border: Color,
|
|
137 |
}
|
|
138 |
|
|
139 |
pub struct Flakes {
|
|
140 |
texture: ThemeSprite,
|
|
141 |
frames_count: u16,
|
|
142 |
frame_ticks: u16,
|
|
143 |
velocity: u16,
|
|
144 |
fall_speed: u16,
|
|
145 |
}
|
|
146 |
|
|
147 |
#[derive(Default)]
|
|
148 |
pub struct Water {
|
|
149 |
top_color: Color,
|
|
150 |
bottom_color: Color,
|
|
151 |
opacity: u8,
|
|
152 |
}
|
|
153 |
|
|
154 |
#[derive(Default)]
|
|
155 |
pub struct ThemeParts {
|
|
156 |
water: Water,
|
|
157 |
flakes: Option<Flakes>,
|
|
158 |
music: String,
|
|
159 |
sky: Color,
|
|
160 |
tint: Color,
|
|
161 |
}
|
|
162 |
|
|
163 |
#[derive(Default)]
|
15120
|
164 |
pub struct Theme {
|
15927
|
165 |
border_color: Color,
|
|
166 |
clouds_count: u16,
|
|
167 |
flatten_flakes: bool,
|
15120
|
168 |
land_texture: Option<ThemeSprite>,
|
|
169 |
border_texture: Option<ThemeSprite>,
|
15927
|
170 |
land_objects: Vec<LandObject>,
|
|
171 |
spays: Vec<LandSpray>,
|
|
172 |
use_ice: bool,
|
|
173 |
use_snow: bool,
|
|
174 |
music: String,
|
|
175 |
normal_parts: ThemeParts,
|
|
176 |
sd_parts: ThemeParts,
|
15120
|
177 |
}
|
|
178 |
|
|
179 |
impl Theme {
|
|
180 |
pub fn land_texture(&self) -> Option<&ThemeSprite> {
|
|
181 |
self.land_texture.as_ref()
|
|
182 |
}
|
|
183 |
|
|
184 |
pub fn border_texture(&self) -> Option<&ThemeSprite> {
|
|
185 |
self.border_texture.as_ref()
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
#[derive(Debug)]
|
|
190 |
pub enum ThemeLoadError {
|
|
191 |
File(io::Error),
|
|
192 |
Decoding(DecodingError),
|
|
193 |
Format(String),
|
|
194 |
}
|
|
195 |
|
|
196 |
impl From<io::Error> for ThemeLoadError {
|
|
197 |
fn from(e: io::Error) -> Self {
|
|
198 |
ThemeLoadError::File(e)
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
impl From<DecodingError> for ThemeLoadError {
|
|
203 |
fn from(e: DecodingError) -> Self {
|
|
204 |
ThemeLoadError::Decoding(e)
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
impl Theme {
|
|
209 |
pub fn new() -> Self {
|
15927
|
210 |
Default::default()
|
15120
|
211 |
}
|
|
212 |
|
|
213 |
pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> {
|
|
214 |
let mut theme = Self::new();
|
|
215 |
|
|
216 |
for entry in read_dir(path)? {
|
|
217 |
let file = entry?;
|
|
218 |
if file.file_name() == "LandTex.png" {
|
|
219 |
theme.land_texture = Some(load_sprite(&file.path())?)
|
|
220 |
} else if file.file_name() == "Border.png" {
|
|
221 |
theme.border_texture = Some(load_sprite(&file.path())?)
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
Ok(theme)
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> {
|
|
230 |
let decoder = Decoder::new(BufReader::new(File::open(path)?));
|
|
231 |
let (info, mut reader) = decoder.read_info()?;
|
|
232 |
|
|
233 |
if info.color_type != ColorType::RGBA {
|
|
234 |
return Err(ThemeLoadError::Format(format!(
|
|
235 |
"Unexpected format: {:?}",
|
|
236 |
info.color_type
|
|
237 |
)));
|
|
238 |
}
|
|
239 |
let size = Size::new(info.width as usize, info.height as usize);
|
|
240 |
|
15914
|
241 |
let mut pixels: Vec2D<u32> = Vec2D::new(&size, 0);
|
15120
|
242 |
reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?;
|
|
243 |
|
|
244 |
Ok(ThemeSprite { pixels })
|
|
245 |
}
|
|
246 |
|
|
247 |
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] {
|
|
248 |
unsafe { from_raw_parts::<u8>(slice_u32.as_ptr() as *const u8, slice_u32.len() * 4) }
|
|
249 |
}
|
|
250 |
|
|
251 |
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] {
|
|
252 |
unsafe { from_raw_parts_mut::<u8>(slice_u32.as_mut_ptr() as *mut u8, slice_u32.len() * 4) }
|
|
253 |
}
|