author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 31 Oct 2018 16:50:34 +0100 | |
changeset 14044 | e9e2d85532ce |
parent 14033 | bf77c4d2294f |
child 14054 | 3185fb34f3b5 |
permissions | -rw-r--r-- |
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
1 |
use sdl2::{ |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
2 |
keyboard::Scancode, |
14030 | 3 |
event::EventType, |
4 |
surface::Surface, |
|
5 |
pixels::{ |
|
6 |
PixelFormatEnum, Color |
|
7 |
} |
|
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
8 |
}; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
9 |
|
14033 | 10 |
use integral_geometry::{Point, Size}; |
14030 | 11 |
|
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
12 |
use rand::{ |
14030 | 13 |
thread_rng, RngCore, Rng, |
14 |
distributions::uniform::SampleUniform |
|
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
15 |
}; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
16 |
|
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
17 |
use landgen::{ |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
18 |
LandGenerator, |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
19 |
LandGenerationParameters |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
20 |
}; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
21 |
use land2d::Land2D; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
22 |
use lfprng::LaggedFibonacciPRNG; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
23 |
|
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
24 |
struct LandSource<T> { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
25 |
rnd: LaggedFibonacciPRNG, |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
26 |
generator: T |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
27 |
} |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
28 |
|
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
29 |
impl <T: LandGenerator> LandSource<T> { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
30 |
fn new(generator: T) -> Self { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
31 |
let mut init = [0u8; 64]; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
32 |
thread_rng().fill_bytes(&mut init); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
33 |
LandSource { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
34 |
rnd: LaggedFibonacciPRNG::new(&init), |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
35 |
generator |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
36 |
} |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
37 |
} |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
38 |
fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
39 |
self.generator.generate_land(parameters, &mut self.rnd) |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
40 |
} |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
41 |
} |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
42 |
|
14030 | 43 |
fn fill_pixels(pixels: &mut [u8], land: &Land2D<u32>) { |
44 |
for (surf_row, land_row) in pixels.chunks_mut(land.width() * 4).zip(land.rows()) { |
|
45 |
for (surf_pixel, land_pixel) in surf_row.chunks_mut(4).zip(land_row) { |
|
46 |
if let [b, g, r, a] = surf_pixel { |
|
47 |
*a = 255; *r = *land_pixel as u8; |
|
48 |
} |
|
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
fn fill_texture(surface: &mut Surface, land: &Land2D<u32>) { |
|
54 |
if surface.must_lock() { |
|
55 |
surface.with_lock_mut(|data| fill_pixels(data, land)); |
|
56 |
} else { |
|
57 |
surface.without_lock_mut().map(|data| fill_pixels(data, land)); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T { |
|
62 |
thread_rng().gen_range(T::default(), max) |
|
63 |
} |
|
64 |
||
65 |
const WIDTH: u32 = 512; |
|
66 |
const HEIGHT: u32 = 512; |
|
14033 | 67 |
const SIZE: Size = Size {width: 512, height: 512}; |
14030 | 68 |
|
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
69 |
fn main() { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
70 |
let sdl = sdl2::init().unwrap(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
71 |
let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
72 |
let events = sdl.event().unwrap(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
73 |
|
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
74 |
let mut pump = sdl.event_pump().unwrap(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
75 |
let video = sdl.video().unwrap(); |
14030 | 76 |
let window = video.window("Theme Editor", WIDTH, HEIGHT) |
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
77 |
.position_centered() |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
78 |
.build().unwrap(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
79 |
|
14030 | 80 |
let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap(); |
81 |
||
82 |
fn point() -> Point { |
|
83 |
Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32)) |
|
84 |
} |
|
85 |
||
14033 | 86 |
let mut land = Land2D::new(SIZE, 0); |
14031 | 87 |
for i in 0..32 { |
14030 | 88 |
land.draw_thick_line(point(), point(), rnd(5), u32::max_value()); |
14031 | 89 |
|
90 |
land.fill_circle(point(), rnd(60), u32::max_value()); |
|
14030 | 91 |
} |
92 |
||
93 |
fill_texture(&mut land_surf, &land); |
|
94 |
||
95 |
let mut win_surf = window.surface(&pump).unwrap(); |
|
96 |
let win_rect = win_surf.rect(); |
|
97 |
land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap(); |
|
98 |
win_surf.update_window(); |
|
99 |
||
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
100 |
'pool: loop { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
101 |
use sdl2::event::Event::*; |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
102 |
pump.pump_events(); |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
103 |
|
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
104 |
while let Some(event) = pump.poll_event() { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
105 |
match event { |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
106 |
Quit{ .. } => break 'pool, |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
107 |
_ => () |
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
108 |
} |
14030 | 109 |
} |
14028
0f517cbfe16d
start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff
changeset
|
110 |
} |
14030 | 111 |
} |