# HG changeset patch # User alfadur # Date 1541032693 -10800 # Node ID 3185fb34f3b5aa8d56c1abf354df20a794f82d4f # Parent 38eb5937169e5e70cc629331e84774689969536d update theme editor to use new land generator implementation diff -r 38eb5937169e -r 3185fb34f3b5 rust/integral-geometry/src/lib.rs --- a/rust/integral-geometry/src/lib.rs Thu Nov 01 01:07:26 2018 +0100 +++ b/rust/integral-geometry/src/lib.rs Thu Nov 01 03:38:13 2018 +0300 @@ -167,6 +167,16 @@ pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { Self { x, y, width, height } } + + #[inline] + pub fn size(&self) -> Size { + Size::new(self.width as usize, self.height as usize) + } + + #[inline] + pub fn area(&self) -> usize { + self.size().area() + } } diff -r 38eb5937169e -r 3185fb34f3b5 rust/landgen/src/lib.rs --- a/rust/landgen/src/lib.rs Thu Nov 01 01:07:26 2018 +0100 +++ b/rust/landgen/src/lib.rs Thu Nov 01 03:38:13 2018 +0300 @@ -1,4 +1,4 @@ -mod template_based; +pub mod template_based; extern crate integral_geometry; extern crate land2d; @@ -9,6 +9,12 @@ basic: T, } +impl LandGenerationParameters { + pub fn new(zero: T, basic: T) -> Self { + Self { zero, basic } + } +} + pub trait LandGenerator { fn generate_land>( &self, diff -r 38eb5937169e -r 3185fb34f3b5 rust/landgen/src/template_based.rs --- a/rust/landgen/src/template_based.rs Thu Nov 01 01:07:26 2018 +0100 +++ b/rust/landgen/src/template_based.rs Thu Nov 01 03:38:13 2018 +0300 @@ -46,7 +46,7 @@ } } -struct OutlineTemplate { +pub struct OutlineTemplate { islands: Vec>, fill_points: Vec, size: Size, @@ -56,7 +56,54 @@ is_negative: bool, } -struct TemplatedLandGenerator { +impl OutlineTemplate { + pub fn new(size: Size) -> Self { + OutlineTemplate { + size, + islands: Vec::new(), + fill_points: Vec::new(), + can_flip: false, + can_invert: false, + can_mirror: false, + is_negative: false + } + } + + pub fn flippable(self) -> Self { + Self { can_flip: true, ..self } + } + + pub fn mirrorable(self) -> Self { + Self { can_mirror: true, ..self } + } + + pub fn invertable(self) -> Self { + Self { can_invert: true, ..self } + } + + pub fn negative(self) -> Self { + Self { is_negative: true, ..self } + } + + pub fn with_fill_points(self, fill_points: Vec) -> Self + { + Self { fill_points, ..self } + } + + pub fn with_islands(mut self, islands: Vec>) -> Self { + Self { islands, ..self } + } + + pub fn add_fill_points(mut self, points: &[Point]) -> Self { + self.fill_points.extend_from_slice(points); self + } + + pub fn add_island(mut self, island: &[Rect]) -> Self { + self.islands.push(island.into()); self + } +} + +pub struct TemplatedLandGenerator { outline_template: OutlineTemplate, } diff -r 38eb5937169e -r 3185fb34f3b5 rust/theme-editor/src/main.rs --- a/rust/theme-editor/src/main.rs Thu Nov 01 01:07:26 2018 +0100 +++ b/rust/theme-editor/src/main.rs Thu Nov 01 03:38:13 2018 +0300 @@ -7,7 +7,7 @@ } }; -use integral_geometry::{Point, Size}; +use integral_geometry::{Point, Size, Rect}; use rand::{ thread_rng, RngCore, Rng, @@ -15,6 +15,10 @@ }; use landgen::{ + template_based::{ + OutlineTemplate, + TemplatedLandGenerator + }, LandGenerator, LandGenerationParameters }; @@ -35,6 +39,7 @@ generator } } + fn next(&mut self, parameters: LandGenerationParameters) -> Land2D { self.generator.generate_land(parameters, &mut self.rnd) } @@ -66,6 +71,30 @@ const HEIGHT: u32 = 512; const SIZE: Size = Size {width: 512, height: 512}; +fn point() -> Point { + Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32)) +} +fn rect() -> Rect { + Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(128), rnd(128)) +} + +fn init_source() -> LandSource { + let template = OutlineTemplate::new(SIZE) + .with_fill_points((0..32).map(|_| point()).collect()) + .with_islands((0..16).map(|_| vec![rect()]).collect()); + + let generator = TemplatedLandGenerator::new(template); + LandSource::new(generator) +} + +fn draw_random_lines(land: &mut Land2D) { + for i in 0..32 { + land.draw_thick_line(point(), point(), rnd(5), u32::max_value()); + + land.fill_circle(point(), rnd(60), u32::max_value()); + } +} + fn main() { let sdl = sdl2::init().unwrap(); let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); @@ -77,18 +106,11 @@ .position_centered() .build().unwrap(); - let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap(); - - fn point() -> Point { - Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32)) - } + let mut source = init_source(); + let mut land = source.next( + LandGenerationParameters::new(0, u32::max_value())); - let mut land = Land2D::new(SIZE, 0); - for i in 0..32 { - land.draw_thick_line(point(), point(), rnd(5), u32::max_value()); - - land.fill_circle(point(), rnd(60), u32::max_value()); - } + let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap(); fill_texture(&mut land_surf, &land);