author | unC0Rr |
Tue, 04 Feb 2025 17:31:55 +0100 | |
branch | qmlrenderer |
changeset 16116 | 8da5a118120b |
parent 16115 | ee8b894272d0 |
permissions | -rw-r--r-- |
16115 | 1 |
use integral_geometry::Size; |
15957 | 2 |
use integral_geometry::{Point, Rect}; |
15141 | 3 |
use png::{ColorType, Decoder, DecodingError}; |
16115 | 4 |
use std::slice::ChunksExact; |
15141 | 5 |
use std::{ |
6 |
fs::{read_dir, File}, |
|
7 |
io, |
|
8 |
io::BufReader, |
|
9 |
path::Path, |
|
10 |
slice::{from_raw_parts, from_raw_parts_mut}, |
|
11 |
}; |
|
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] |
|
16102 | 25 |
pub fn width(&self) -> u32 { |
15141 | 26 |
self.size().width |
27 |
} |
|
28 |
||
29 |
#[inline] |
|
16102 | 30 |
pub fn height(&self) -> u32 { |
15141 | 31 |
self.size().height |
32 |
} |
|
33 |
||
34 |
#[inline] |
|
16111
669cdf697f16
Simplify 'for' loop by keeping more information about iterator
unC0Rr
parents:
16102
diff
changeset
|
35 |
pub fn rows(&self) -> ChunksExact<u32> { |
15141 | 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(); |
|
15944 | 51 |
let mut pixels = Vec2D::new(&size, 0u32); |
15141 | 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; |
|
16102 | 64 |
let mut pixels = vec![0u32; size.area() as usize]; |
15141 | 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] |
|
16102 | 98 |
pub fn width(&self) -> u32 { |
15141 | 99 |
self.size().width |
100 |
} |
|
101 |
||
102 |
#[inline] |
|
16102 | 103 |
pub fn height(&self) -> u32 { |
15141 | 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 |
||
15957 | 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)] |
|
15141 | 164 |
pub struct Theme { |
15957 | 165 |
border_color: Color, |
166 |
clouds_count: u16, |
|
167 |
flatten_flakes: bool, |
|
15141 | 168 |
land_texture: Option<ThemeSprite>, |
169 |
border_texture: Option<ThemeSprite>, |
|
15957 | 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, |
|
15141 | 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 { |
|
15957 | 210 |
Default::default() |
15141 | 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)?)); |
|
16089 | 231 |
let mut reader = decoder.read_info()?; |
232 |
let info = reader.info(); |
|
15141 | 233 |
|
16089 | 234 |
if info.color_type != ColorType::Rgba { |
15141 | 235 |
return Err(ThemeLoadError::Format(format!( |
236 |
"Unexpected format: {:?}", |
|
237 |
info.color_type |
|
238 |
))); |
|
239 |
} |
|
16102 | 240 |
let size = Size::new(info.width, info.height); |
15141 | 241 |
|
15944 | 242 |
let mut pixels: Vec2D<u32> = Vec2D::new(&size, 0); |
15141 | 243 |
reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?; |
244 |
||
245 |
Ok(ThemeSprite { pixels }) |
|
246 |
} |
|
247 |
||
248 |
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] { |
|
249 |
unsafe { from_raw_parts::<u8>(slice_u32.as_ptr() as *const u8, slice_u32.len() * 4) } |
|
250 |
} |
|
251 |
||
252 |
pub fn slice_u32_to_u8_mut(slice_u32: &mut [u32]) -> &mut [u8] { |
|
253 |
unsafe { from_raw_parts_mut::<u8>(slice_u32.as_mut_ptr() as *mut u8, slice_u32.len() * 4) } |
|
254 |
} |