15915
|
1 |
use super::tile_image::TileImage;
|
15913
|
2 |
use super::wavefront_collapse::WavefrontCollapse;
|
|
3 |
use crate::{LandGenerationParameters, LandGenerator};
|
15915
|
4 |
use integral_geometry::Size;
|
|
5 |
use png::Decoder;
|
|
6 |
use std::fs::File;
|
|
7 |
use std::io::BufReader;
|
15913
|
8 |
|
|
9 |
pub struct WavefrontCollapseLandGenerator {
|
|
10 |
wfc: WavefrontCollapse,
|
|
11 |
}
|
|
12 |
|
|
13 |
impl WavefrontCollapseLandGenerator {
|
|
14 |
pub fn new() -> Self {
|
|
15 |
Self {
|
|
16 |
wfc: WavefrontCollapse::default(),
|
|
17 |
}
|
|
18 |
}
|
|
19 |
|
15915
|
20 |
pub fn load_template<T: Copy + PartialEq + Default>(&self, parameters: &LandGenerationParameters<T>) -> Vec<TileImage<T>> {
|
|
21 |
let file = File::open("sample.png").expect("file exists");
|
|
22 |
let decoder = Decoder::new(BufReader::new(file));
|
|
23 |
let mut reader = decoder.read_info().unwrap();
|
|
24 |
|
|
25 |
let info = reader.info();
|
|
26 |
let mut tiles_image = vec2d::Vec2D::new(
|
|
27 |
&Size::new(info.width as usize, info.height as usize),
|
|
28 |
parameters.zero,
|
|
29 |
);
|
|
30 |
|
|
31 |
let mut buf = vec![0; reader.output_buffer_size()];
|
|
32 |
let info = reader.next_frame(&mut buf).unwrap();
|
|
33 |
let bytes = &buf[..info.buffer_size()];
|
15913
|
34 |
|
15915
|
35 |
let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter();
|
|
36 |
|
|
37 |
for line in bytes.chunks_exact(info.line_size) {
|
|
38 |
for value in line.chunks_exact(info.color_type.samples()) {
|
|
39 |
*tiles_image_pixels
|
|
40 |
.next()
|
|
41 |
.expect("vec2d size matching image dimensions") =
|
|
42 |
if value.into_iter().all(|p| *p == 0) {
|
|
43 |
parameters.zero
|
|
44 |
} else {
|
|
45 |
parameters.basic
|
|
46 |
};
|
|
47 |
}
|
|
48 |
}
|
|
49 |
|
|
50 |
TileImage::<T>::new(tiles_image).split(3, 3)
|
15913
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
impl LandGenerator for WavefrontCollapseLandGenerator {
|
|
55 |
fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
|
|
56 |
&self,
|
|
57 |
parameters: &LandGenerationParameters<T>,
|
|
58 |
random_numbers: &mut I,
|
|
59 |
) -> land2d::Land2D<T> {
|
15915
|
60 |
let tiles = self.load_template(parameters);
|
|
61 |
|
15913
|
62 |
todo!()
|
|
63 |
}
|
|
64 |
}
|
15915
|
65 |
|
|
66 |
#[cfg(test)]
|
|
67 |
mod tests {
|
|
68 |
use super::WavefrontCollapseLandGenerator;
|
|
69 |
use crate::{LandGenerator, LandGenerationParameters};
|
|
70 |
use integral_geometry::Size;
|
|
71 |
use vec2d::Vec2D;
|
|
72 |
|
|
73 |
#[test]
|
|
74 |
fn test_generation() {
|
|
75 |
let wfc_gen =WavefrontCollapseLandGenerator::new();
|
|
76 |
let landgen_params = LandGenerationParameters::new(0u8, 255u8, 0, true, true);
|
|
77 |
wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(1u32));
|
|
78 |
}
|
|
79 |
}
|