|
1 use sdl2::{ |
|
2 keyboard::Scancode, |
|
3 event::EventType, |
|
4 surface::Surface, |
|
5 pixels::{ |
|
6 PixelFormatEnum, Color |
|
7 } |
|
8 }; |
|
9 |
|
10 use integral_geometry::{Point, Size, Rect, Line}; |
|
11 |
|
12 use rand::{ |
|
13 thread_rng, RngCore, Rng, |
|
14 distributions::uniform::SampleUniform |
|
15 }; |
|
16 |
|
17 use landgen::{ |
|
18 template_based::TemplatedLandGenerator, |
|
19 outline_template::OutlineTemplate, |
|
20 LandGenerator, |
|
21 LandGenerationParameters |
|
22 }; |
|
23 use land2d::Land2D; |
|
24 use lfprng::LaggedFibonacciPRNG; |
|
25 |
|
26 struct LandSource<T> { |
|
27 rnd: LaggedFibonacciPRNG, |
|
28 generator: T |
|
29 } |
|
30 |
|
31 impl <T: LandGenerator> LandSource<T> { |
|
32 fn new(generator: T) -> Self { |
|
33 let mut init = [0u8; 64]; |
|
34 thread_rng().fill_bytes(&mut init); |
|
35 LandSource { |
|
36 rnd: LaggedFibonacciPRNG::new(&init), |
|
37 generator |
|
38 } |
|
39 } |
|
40 |
|
41 fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> { |
|
42 self.generator.generate_land(¶meters, &mut self.rnd) |
|
43 } |
|
44 } |
|
45 |
|
46 fn fill_pixels(pixels: &mut [u8], land: &Land2D<u32>) { |
|
47 for (surf_row, land_row) in pixels.chunks_mut(land.width() * 4).zip(land.rows()) { |
|
48 for (surf_pixel, land_pixel) in surf_row.chunks_mut(4).zip(land_row) { |
|
49 if let [b, g, r, a] = surf_pixel { |
|
50 *a = 255; *r = *land_pixel as u8; |
|
51 } |
|
52 } |
|
53 } |
|
54 } |
|
55 |
|
56 fn fill_texture(surface: &mut Surface, land: &Land2D<u32>) { |
|
57 if surface.must_lock() { |
|
58 surface.with_lock_mut(|data| fill_pixels(data, land)); |
|
59 } else { |
|
60 surface.without_lock_mut().map(|data| fill_pixels(data, land)); |
|
61 } |
|
62 } |
|
63 |
|
64 fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T { |
|
65 thread_rng().gen_range(T::default(), max) |
|
66 } |
|
67 |
|
68 const WINDOW_WIDTH: u32 = 800; |
|
69 const WINDOW_HEIGHT: u32 = 600; |
|
70 const WINDOW_SIZE: Size = Size {width: WINDOW_WIDTH as usize, height: WINDOW_HEIGHT as usize}; |
|
71 |
|
72 const PLAY_WIDTH: u32 = 3072; |
|
73 const PLAY_HEIGHT: u32 = 1424; |
|
74 const PLAY_SIZE: Size = Size {width: PLAY_WIDTH as usize, height: PLAY_HEIGHT as usize}; |
|
75 |
|
76 const LAND_WIDTH: u32 = 4096; |
|
77 const LAND_HEIGHT: u32 = 2048; |
|
78 const LAND_SIZE: Size = Size {width: LAND_WIDTH as usize, height: LAND_HEIGHT as usize}; |
|
79 |
|
80 fn point() -> Point { |
|
81 Point::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32)) |
|
82 } |
|
83 fn rect() -> Rect { |
|
84 Rect::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32), rnd(120) + 8, rnd(120) + 8) |
|
85 } |
|
86 |
|
87 fn land_rect() -> Rect { |
|
88 Rect::at_origin(PLAY_SIZE) |
|
89 } |
|
90 |
|
91 fn basic_template() -> OutlineTemplate { |
|
92 OutlineTemplate::new(PLAY_SIZE) |
|
93 .with_fill_points(vec![land_rect().center()]) |
|
94 .add_island(&land_rect().split_at(land_rect().center())) |
|
95 } |
|
96 |
|
97 macro_rules! pseudo_yaml { |
|
98 [$({x: $x: tt, y: $y: tt, w: $w: tt, h: $h: tt}),*] => { |
|
99 [$(Rect::new($x, $y, $w, $h)),*] |
|
100 } |
|
101 } |
|
102 |
|
103 fn test_template() -> OutlineTemplate { |
|
104 let island = pseudo_yaml![ |
|
105 {x: 810, y: 1424, w: 1, h: 1}, |
|
106 {x: 560, y: 1160, w: 130, h: 170}, |
|
107 {x: 742, y: 1106, w: 316, h: 150}, |
|
108 {x: 638, y: 786, w: 270, h: 180}, |
|
109 {x: 646, y: 576, w: 242, h: 156}, |
|
110 {x: 952, y: 528, w: 610, h: 300}, |
|
111 {x: 1150, y: 868, w: 352, h: 324}, |
|
112 {x: 1050, y: 1424, w: 500, h: 1}, |
|
113 {x: 1650, y: 1500, w: 1, h: 1}, |
|
114 {x: 1890, y: 1424, w: 1, h: 1}, |
|
115 {x: 1852, y: 1304, w: 74, h: 12}, |
|
116 {x: 1648, y: 975, w: 68, h: 425}, |
|
117 {x: 1826, y: 992, w: 140, h: 142}, |
|
118 {x: 1710, y: 592, w: 150, h: 350}, |
|
119 {x: 1988, y: 594, w: 148, h: 242}, |
|
120 {x: 2018, y: 872, w: 276, h: 314}, |
|
121 {x: 2110, y: 1250, w: 130, h: 86}, |
|
122 {x: 2134, y: 1424, w: 1, h: 1} |
|
123 ]; |
|
124 |
|
125 OutlineTemplate::new(PLAY_SIZE) |
|
126 .add_island(&island) |
|
127 .add_fill_points(&[Point::new(1023, 0)]) |
|
128 } |
|
129 |
|
130 fn init_source() -> LandSource<TemplatedLandGenerator> { |
|
131 let template = test_template(); |
|
132 let generator = TemplatedLandGenerator::new(template); |
|
133 LandSource::new(generator) |
|
134 } |
|
135 |
|
136 fn draw_center_mark(land: &mut Land2D<u32>) { |
|
137 for i in 0..32 { |
|
138 land.draw_thick_line(Line::new(Point::new(LAND_WIDTH as i32 / 2, 0), |
|
139 Point::new(LAND_WIDTH as i32 / 2, LAND_HEIGHT as i32)), 10, 128); |
|
140 land.draw_thick_line(Line::new(Point::new(0, LAND_HEIGHT as i32 / 2), |
|
141 Point::new(LAND_WIDTH as i32, LAND_HEIGHT as i32 / 2)), 10, 128); |
|
142 land.fill_circle(Point::new(LAND_WIDTH as i32, LAND_HEIGHT as i32) / 2, 60, 128); |
|
143 } |
|
144 } |
|
145 |
|
146 fn draw_random_lines(land: &mut Land2D<u32>) { |
|
147 for i in 0..32 { |
|
148 land.draw_thick_line(Line::new(point(), point()), rnd(5), 128); |
|
149 |
|
150 land.fill_circle(point(), rnd(60), 128); |
|
151 } |
|
152 } |
|
153 |
|
154 fn main() { |
|
155 let sdl = sdl2::init().unwrap(); |
|
156 let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); |
|
157 let events = sdl.event().unwrap(); |
|
158 |
|
159 let mut pump = sdl.event_pump().unwrap(); |
|
160 let video = sdl.video().unwrap(); |
|
161 let window = video.window("Theme Editor", WINDOW_WIDTH, WINDOW_HEIGHT) |
|
162 .position_centered() |
|
163 .build().unwrap(); |
|
164 |
|
165 let mut source = init_source(); |
|
166 let mut land = source.next( |
|
167 LandGenerationParameters::new(0, u32::max_value(), 1, false, false)); |
|
168 draw_center_mark(&mut land); |
|
169 |
|
170 let mut land_surf = Surface::new(LAND_WIDTH, LAND_HEIGHT, PixelFormatEnum::ARGB8888).unwrap(); |
|
171 |
|
172 fill_texture(&mut land_surf, &land); |
|
173 |
|
174 let mut win_surf = window.surface(&pump).unwrap(); |
|
175 let dest_rect = win_surf.rect(); |
|
176 land_surf.blit_scaled(land_surf.rect(), &mut win_surf, dest_rect).unwrap(); |
|
177 win_surf.update_window(); |
|
178 |
|
179 'pool: loop { |
|
180 use sdl2::event::Event::*; |
|
181 pump.pump_events(); |
|
182 |
|
183 while let Some(event) = pump.poll_event() { |
|
184 match event { |
|
185 Quit{ .. } => break 'pool, |
|
186 _ => () |
|
187 } |
|
188 } |
|
189 } |
|
190 } |