rust/theme-editor/src/main.rs
changeset 14075 3185fb34f3b5
parent 14054 bf77c4d2294f
child 14111 43d956f41cd4
equal deleted inserted replaced
14074:38eb5937169e 14075:3185fb34f3b5
     5     pixels::{
     5     pixels::{
     6         PixelFormatEnum, Color
     6         PixelFormatEnum, Color
     7     }
     7     }
     8 };
     8 };
     9 
     9 
    10 use integral_geometry::{Point, Size};
    10 use integral_geometry::{Point, Size, Rect};
    11 
    11 
    12 use rand::{
    12 use rand::{
    13     thread_rng, RngCore, Rng,
    13     thread_rng, RngCore, Rng,
    14     distributions::uniform::SampleUniform
    14     distributions::uniform::SampleUniform
    15 };
    15 };
    16 
    16 
    17 use landgen::{
    17 use landgen::{
       
    18     template_based::{
       
    19         OutlineTemplate,
       
    20         TemplatedLandGenerator
       
    21     },
    18     LandGenerator,
    22     LandGenerator,
    19     LandGenerationParameters
    23     LandGenerationParameters
    20 };
    24 };
    21 use land2d::Land2D;
    25 use land2d::Land2D;
    22 use lfprng::LaggedFibonacciPRNG;
    26 use lfprng::LaggedFibonacciPRNG;
    33         LandSource {
    37         LandSource {
    34             rnd: LaggedFibonacciPRNG::new(&init),
    38             rnd: LaggedFibonacciPRNG::new(&init),
    35             generator
    39             generator
    36         }
    40         }
    37     }
    41     }
       
    42 
    38     fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> {
    43     fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> {
    39         self.generator.generate_land(parameters, &mut self.rnd)
    44         self.generator.generate_land(parameters, &mut self.rnd)
    40     }
    45     }
    41 }
    46 }
    42 
    47 
    64 
    69 
    65 const WIDTH: u32 = 512;
    70 const WIDTH: u32 = 512;
    66 const HEIGHT: u32 = 512;
    71 const HEIGHT: u32 = 512;
    67 const SIZE: Size = Size {width: 512, height: 512};
    72 const SIZE: Size = Size {width: 512, height: 512};
    68 
    73 
       
    74 fn point() -> Point {
       
    75     Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
       
    76 }
       
    77 fn rect() -> Rect {
       
    78     Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(128), rnd(128))
       
    79 }
       
    80 
       
    81 fn init_source() -> LandSource<TemplatedLandGenerator> {
       
    82     let template = OutlineTemplate::new(SIZE)
       
    83         .with_fill_points((0..32).map(|_| point()).collect())
       
    84         .with_islands((0..16).map(|_| vec![rect()]).collect());
       
    85 
       
    86     let generator = TemplatedLandGenerator::new(template);
       
    87     LandSource::new(generator)
       
    88 }
       
    89 
       
    90 fn draw_random_lines(land: &mut Land2D<u32>) {
       
    91     for i in 0..32 {
       
    92         land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
       
    93 
       
    94         land.fill_circle(point(), rnd(60), u32::max_value());
       
    95     }
       
    96 }
       
    97 
    69 fn main() {
    98 fn main() {
    70     let sdl = sdl2::init().unwrap();
    99     let sdl = sdl2::init().unwrap();
    71     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
   100     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
    72     let events = sdl.event().unwrap();
   101     let events = sdl.event().unwrap();
    73 
   102 
    75     let video = sdl.video().unwrap();
   104     let video = sdl.video().unwrap();
    76     let window = video.window("Theme Editor", WIDTH, HEIGHT)
   105     let window = video.window("Theme Editor", WIDTH, HEIGHT)
    77         .position_centered()
   106         .position_centered()
    78         .build().unwrap();
   107         .build().unwrap();
    79 
   108 
       
   109     let mut source = init_source();
       
   110     let mut land = source.next(
       
   111         LandGenerationParameters::new(0, u32::max_value()));
       
   112 
    80     let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
   113     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 
       
    86     let mut land = Land2D::new(SIZE, 0);
       
    87     for i in 0..32 {
       
    88         land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
       
    89 
       
    90         land.fill_circle(point(), rnd(60), u32::max_value());
       
    91     }
       
    92 
   114 
    93     fill_texture(&mut land_surf, &land);
   115     fill_texture(&mut land_surf, &land);
    94 
   116 
    95     let mut win_surf = window.surface(&pump).unwrap();
   117     let mut win_surf = window.surface(&pump).unwrap();
    96     let win_rect = win_surf.rect();
   118     let win_rect = win_surf.rect();