author | alfadur |
Thu, 08 Nov 2018 07:15:22 +0300 | |
changeset 14175 | a4c1a2d0ac24 |
parent 14169 | 1749961647b9 |
child 14180 | 76a52e8149e3 |
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] |
|
14175 | 41 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[u32]> { |
14165 | 42 |
self.pixels.rows() |
43 |
} |
|
44 |
||
45 |
#[inline] |
|
46 |
pub fn get_row(&self, index: usize) -> &[u32] { |
|
47 |
&self.pixels[index] |
|
48 |
} |
|
14175 | 49 |
|
50 |
#[inline] |
|
51 |
pub fn get_pixel(&self, x: usize, y: usize) -> u32 { |
|
52 |
self.pixels[y][x] |
|
53 |
} |
|
14156 | 54 |
} |
55 |
||
56 |
pub struct Theme { |
|
14175 | 57 |
land_texture: Option<ThemeSprite>, |
58 |
border_texture: Option<ThemeSprite> |
|
14156 | 59 |
} |
60 |
||
14165 | 61 |
impl Theme { |
62 |
pub fn land_texture(&self) -> Option<&ThemeSprite> { |
|
63 |
self.land_texture.as_ref() |
|
64 |
} |
|
14175 | 65 |
|
66 |
pub fn border_texture(&self) -> Option<&ThemeSprite> { |
|
67 |
self.border_texture.as_ref() |
|
68 |
} |
|
14165 | 69 |
} |
70 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
71 |
#[derive(Debug)] |
14156 | 72 |
pub enum ThemeLoadError { |
73 |
File(io::Error), |
|
74 |
Decoding(DecodingError), |
|
75 |
Format(String) |
|
76 |
} |
|
77 |
||
78 |
impl From<io::Error> for ThemeLoadError { |
|
79 |
fn from(e: io::Error) -> Self { |
|
80 |
ThemeLoadError::File(e) |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
impl From<DecodingError> for ThemeLoadError { |
|
85 |
fn from(e: DecodingError) -> Self { |
|
86 |
ThemeLoadError::Decoding(e) |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
impl Theme { |
|
91 |
pub fn new() -> Self { |
|
92 |
Theme { |
|
14175 | 93 |
land_texture: None, |
94 |
border_texture: None, |
|
14156 | 95 |
} |
96 |
} |
|
97 |
||
98 |
pub fn load(path: &Path) -> Result<Theme, ThemeLoadError> { |
|
99 |
let mut theme = Self::new(); |
|
100 |
||
101 |
for entry in read_dir(path)? { |
|
102 |
let file = entry?; |
|
103 |
if file.file_name() == "LandTex.png" { |
|
14175 | 104 |
theme.land_texture = Some(load_sprite(&file.path())?) |
105 |
} else if file.file_name() == "Border.png" { |
|
106 |
theme.border_texture = Some(load_sprite(&file.path())?) |
|
14156 | 107 |
} |
108 |
} |
|
109 |
||
110 |
Ok(theme) |
|
111 |
} |
|
112 |
} |
|
113 |
||
14175 | 114 |
fn load_sprite(path: &Path) -> Result<ThemeSprite, ThemeLoadError> { |
115 |
let decoder = Decoder::new( |
|
116 |
BufReader::new(File::open(path)?)); |
|
117 |
let (info, mut reader) = decoder.read_info()?; |
|
118 |
||
119 |
if info.color_type != ColorType::RGBA { |
|
120 |
return Err(ThemeLoadError::Format( |
|
121 |
format!("Unexpected format: {:?}", info.color_type))); |
|
122 |
} |
|
123 |
let size = Size::new(info.width as usize, info.height as usize); |
|
124 |
||
125 |
let mut pixels: Vec2D<u32> = Vec2D::new(size, 0); |
|
126 |
reader.next_frame(slice_u32_to_u8_mut(pixels.as_mut_slice()))?; |
|
127 |
||
128 |
Ok(ThemeSprite { pixels }) |
|
129 |
} |
|
130 |
||
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
131 |
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
|
132 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
133 |
from_raw_parts::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
134 |
slice_u32.as_ptr() as *const u8, |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
135 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
136 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
137 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
138 |
} |
14156 | 139 |
|
14169
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
140 |
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
|
141 |
unsafe { |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
142 |
from_raw_parts_mut::<u8>( |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
143 |
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
|
144 |
slice_u32.len() * 4 |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
145 |
) |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
146 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
147 |
} |
1749961647b9
fix texturing and add a theme loading option to land_dump
alfadur
parents:
14165
diff
changeset
|
148 |