rust/theme-editor/src/main.rs
changeset 14099 7845beb87cb5
parent 14098 a8e194ae26e8
child 14102 c6923ce139a2
equal deleted inserted replaced
14098:a8e194ae26e8 14099:7845beb87cb5
    63 
    63 
    64 fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T {
    64 fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T {
    65     thread_rng().gen_range(T::default(), max)
    65     thread_rng().gen_range(T::default(), max)
    66 }
    66 }
    67 
    67 
    68 const WIDTH: u32 = 512;
    68 const WINDOW_WIDTH: u32 = 800;
    69 const HEIGHT: u32 = 512;
    69 const WINDOW_HEIGHT: u32 = 600;
    70 const SIZE: Size = Size {width: 512, height: 512};
    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};
    71 
    79 
    72 fn point() -> Point {
    80 fn point() -> Point {
    73     Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
    81     Point::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32))
    74 }
    82 }
    75 fn rect() -> Rect {
    83 fn rect() -> Rect {
    76     Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(120) + 8, rnd(120) + 8)
    84     Rect::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32), rnd(120) + 8, rnd(120) + 8)
    77 }
    85 }
    78 
    86 
    79 fn land_rect() -> Rect {
    87 fn land_rect() -> Rect {
    80     Rect::at_origin(SIZE)
    88     Rect::at_origin(PLAY_SIZE)
    81 }
    89 }
    82 
    90 
    83 fn test_template() -> OutlineTemplate {
    91 fn basic_template() -> OutlineTemplate {
    84     OutlineTemplate::new(SIZE)
    92     OutlineTemplate::new(PLAY_SIZE)
    85         .with_fill_points(vec![land_rect().center()])
    93         .with_fill_points(vec![land_rect().center()])
    86         .add_island(&land_rect().split_at(land_rect().center()))
    94         .add_island(&land_rect().split_at(land_rect().center()))
    87 }
    95 }
    88 
    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 
    89 fn init_source() -> LandSource<TemplatedLandGenerator> {
   131 fn init_source() -> LandSource<TemplatedLandGenerator> {
    90     let template = test_template();
   132     let template = test_template();
    91 
       
    92     let generator = TemplatedLandGenerator::new(template);
   133     let generator = TemplatedLandGenerator::new(template);
    93     LandSource::new(generator)
   134     LandSource::new(generator)
    94 }
   135 }
    95 
   136 
    96 fn draw_random_lines(land: &mut Land2D<u32>) {
   137 fn draw_random_lines(land: &mut Land2D<u32>) {
    97     for i in 0..32 {
   138     for i in 0..32 {
    98         land.draw_thick_line(Line::new(point(), point()), rnd(5), u32::max_value());
   139         land.draw_thick_line(Line::new(point(), point()), rnd(5), 128);
    99 
   140 
   100         land.fill_circle(point(), rnd(60), u32::max_value());
   141         land.fill_circle(point(), rnd(60), 128);
   101     }
   142     }
   102 }
   143 }
   103 
   144 
   104 fn main() {
   145 fn main() {
   105     let sdl = sdl2::init().unwrap();
   146     let sdl = sdl2::init().unwrap();
   106     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
   147     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
   107     let events = sdl.event().unwrap();
   148     let events = sdl.event().unwrap();
   108 
   149 
   109     let mut pump = sdl.event_pump().unwrap();
   150     let mut pump = sdl.event_pump().unwrap();
   110     let video = sdl.video().unwrap();
   151     let video = sdl.video().unwrap();
   111     let window = video.window("Theme Editor", WIDTH, HEIGHT)
   152     let window = video.window("Theme Editor", WINDOW_WIDTH, WINDOW_HEIGHT)
   112         .position_centered()
   153         .position_centered()
   113         .build().unwrap();
   154         .build().unwrap();
   114 
   155 
   115     let mut source = init_source();
   156     let mut source = init_source();
   116     let mut land = source.next(
   157     let mut land = source.next(
   117         LandGenerationParameters::new(0, u32::max_value()));
   158         LandGenerationParameters::new(0, u32::max_value()));
       
   159     draw_random_lines(&mut land);
   118 
   160 
   119     let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
   161 
       
   162     let mut land_surf = Surface::new(LAND_WIDTH, LAND_HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
   120 
   163 
   121     fill_texture(&mut land_surf, &land);
   164     fill_texture(&mut land_surf, &land);
   122 
   165 
   123     let mut win_surf = window.surface(&pump).unwrap();
   166     let mut win_surf = window.surface(&pump).unwrap();
   124     let win_rect = win_surf.rect();
   167     let dest_rect = win_surf.rect();
   125     land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap();
   168     land_surf.blit_scaled(land_surf.rect(), &mut win_surf, dest_rect).unwrap();
   126     win_surf.update_window();
   169     win_surf.update_window();
   127 
   170 
   128     'pool: loop {
   171     'pool: loop {
   129         use sdl2::event::Event::*;
   172         use sdl2::event::Event::*;
   130         pump.pump_events();
   173         pump.pump_events();