author | alfadur |
Wed, 07 Nov 2018 22:58:54 +0300 | |
changeset 14169 | 1749961647b9 |
parent 14165 | c24a76f131d6 |
child 14175 | a4c1a2d0ac24 |
permissions | -rw-r--r-- |
14156 | 1 |
use std::{ |
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
2 |
slice::{ |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
3 |
from_raw_parts, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
4 |
from_raw_parts_mut |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
5 |
}, |
14156 | 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 |
||
14165 | 17 |
use integral_geometry::Size; |
18 |
use vec2d::Vec2D; |
|
14156 | 19 |
|
20 |
pub struct ThemeSprite { |
|
14165 | 21 |
pixels: Vec2D<u32> |
22 |
} |
|
23 |
||
24 |
impl ThemeSprite { |
|
25 |
#[inline] |
|
26 |
pub fn width(&self) -> usize { |
|
27 |
self.pixels.size().width |
|
28 |
} |
|
29 |
||
30 |
#[inline] |
|
31 |
pub fn height(&self) -> usize { |
|
32 |
self.pixels.size().height |
|
33 |
} |
|
34 |
||
35 |
#[inline] |
|
36 |
pub fn bounds(&self) -> Size { |
|
37 |
self.pixels.size() |
|
38 |
} |
|
39 |
||
40 |
#[inline] |
|
41 |
pub fn rows(&self) -> impl Iterator<Item = &[u32]> { |
|
42 |
self.pixels.rows() |
|
43 |
} |
|
44 |
||
45 |
#[inline] |
|
46 |
pub fn get_row(&self, index: usize) -> &[u32] { |
|
47 |
&self.pixels[index] |
|
48 |
} |
|
14156 | 49 |
} |
50 |
||
51 |
pub struct Theme { |
|
52 |
land_texture: Option<ThemeSprite> |
|
53 |
} |
|
54 |
||
14165 | 55 |
impl Theme { |
56 |
pub fn land_texture(&self) -> Option<&ThemeSprite> { |
|
57 |
self.land_texture.as_ref() |
|
58 |
} |
|
59 |
} |
|
60 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
61 |
#[derive(Debug)] |
14156 | 62 |
pub enum ThemeLoadError { |
63 |
File(io::Error), |
|
64 |
Decoding(DecodingError), |
|
65 |
Format(String) |
|
66 |
} |
|
67 |
||
68 |
impl From<io::Error> for ThemeLoadError { |
|
69 |
fn from(e: io::Error) -> Self { |
|
70 |
ThemeLoadError::File(e) |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
impl From<DecodingError> for ThemeLoadError { |
|
75 |
fn from(e: DecodingError) -> Self { |
|
76 |
ThemeLoadError::Decoding(e) |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
impl Theme { |
|
81 |
pub fn new() -> Self { |
|
82 |
Theme { |
|
83 |
land_texture: None |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> { |
|
88 |
let mut theme = Self::new(); |
|
89 |
||
90 |
for entry in read_dir(path)? { |
|
91 |
let file = entry?; |
|
92 |
if file.file_name() == "LandTex.png" { |
|
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
93 |
let decoder = Decoder::new( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
94 |
BufReader::new(File::open(file.path())?)); |
14156 | 95 |
let (info, mut reader) = decoder.read_info()?; |
96 |
||
97 |
if info.color_type != ColorType::RGBA { |
|
98 |
return Err(ThemeLoadError::Format( |
|
99 |
format!("Unexpected format: {:?}", info.color_type))); |
|
100 |
} |
|
101 |
let size = Size::new(info.width as usize, info.height as usize); |
|
102 |
||
14165 | 103 |
let mut buffer: Vec2D<u32> = Vec2D::new(size, 0); |
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
104 |
reader.next_frame(slice_u32_to_u8_mut(buffer.as_mut_slice()))?; |
14156 | 105 |
|
106 |
let land_tex = ThemeSprite { |
|
107 |
pixels: buffer |
|
108 |
}; |
|
109 |
theme.land_texture = Some(land_tex) |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
Ok(theme) |
|
114 |
} |
|
115 |
} |
|
116 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
117 |
pub fn slice_u32_to_u8(slice_u32: &[u32]) -> &[u8] { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
118 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
119 |
from_raw_parts::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
120 |
slice_u32.as_ptr() as *const u8, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
121 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
122 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
123 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
124 |
} |
14156 | 125 |
|
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
126 |
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:
14165
diff
changeset
|
127 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
128 |
from_raw_parts_mut::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
129 |
slice_u32.as_mut_ptr() as *mut u8, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
130 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
131 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
132 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
133 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
134 |