31 fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> { |
38 fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> { |
32 self.generator.generate_land(parameters, &mut self.rnd) |
39 self.generator.generate_land(parameters, &mut self.rnd) |
33 } |
40 } |
34 } |
41 } |
35 |
42 |
|
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; |
|
67 |
36 fn main() { |
68 fn main() { |
37 let sdl = sdl2::init().unwrap(); |
69 let sdl = sdl2::init().unwrap(); |
38 let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); |
70 let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); |
39 let events = sdl.event().unwrap(); |
71 let events = sdl.event().unwrap(); |
40 |
72 |
41 let mut pump = sdl.event_pump().unwrap(); |
73 let mut pump = sdl.event_pump().unwrap(); |
42 let video = sdl.video().unwrap(); |
74 let video = sdl.video().unwrap(); |
43 let _window = video.window("Theme Editor", 640, 480) |
75 let window = video.window("Theme Editor", WIDTH, HEIGHT) |
44 .position_centered() |
76 .position_centered() |
45 .build().unwrap(); |
77 .build().unwrap(); |
|
78 |
|
79 let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap(); |
|
80 |
|
81 fn point() -> Point { |
|
82 Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32)) |
|
83 } |
|
84 |
|
85 let mut land = Land2D::new(WIDTH as usize, HEIGHT as usize, 0); |
|
86 for i in 0..64 { |
|
87 land.draw_thick_line(point(), point(), rnd(5), u32::max_value()); |
|
88 } |
|
89 |
|
90 fill_texture(&mut land_surf, &land); |
|
91 |
|
92 let mut win_surf = window.surface(&pump).unwrap(); |
|
93 let win_rect = win_surf.rect(); |
|
94 land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap(); |
|
95 win_surf.update_window(); |
46 |
96 |
47 'pool: loop { |
97 'pool: loop { |
48 use sdl2::event::Event::*; |
98 use sdl2::event::Event::*; |
49 pump.pump_events(); |
99 pump.pump_events(); |
50 |
100 |
51 while let Some(event) = pump.poll_event() { |
101 while let Some(event) = pump.poll_event() { |
52 match event { |
102 match event { |
53 Quit{ .. } => break 'pool, |
103 Quit{ .. } => break 'pool, |
54 _ => () |
104 _ => () |
55 } |
105 } |
56 } |
106 } |
57 } |
107 } |
58 } |
108 } |
59 |
|
60 |
|