rust/theme-editor/src/main.rs
author alfadur
Fri, 02 Nov 2018 22:55:15 +0300
changeset 14098 a8e194ae26e8
parent 14090 43d956f41cd4
child 14099 7845beb87cb5
permissions -rw-r--r--
add basic land template to theme editor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
     1
use sdl2::{
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
     2
    keyboard::Scancode,
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
     3
    event::EventType,
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
     4
    surface::Surface,
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
     5
    pixels::{
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
     6
        PixelFormatEnum, Color
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
     7
    }
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
     8
};
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
     9
14090
43d956f41cd4 update theme editor
alfadur
parents: 14054
diff changeset
    10
use integral_geometry::{Point, Size, Rect, Line};
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    11
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    12
use rand::{
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    13
    thread_rng, RngCore, Rng,
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    14
    distributions::uniform::SampleUniform
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    15
};
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    16
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    17
use landgen::{
14090
43d956f41cd4 update theme editor
alfadur
parents: 14054
diff changeset
    18
    template_based::TemplatedLandGenerator,
43d956f41cd4 update theme editor
alfadur
parents: 14054
diff changeset
    19
    outline_template::OutlineTemplate,
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    20
    LandGenerator,
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    21
    LandGenerationParameters
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    22
};
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    23
use land2d::Land2D;
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    24
use lfprng::LaggedFibonacciPRNG;
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    25
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    26
struct LandSource<T> {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    27
    rnd: LaggedFibonacciPRNG,
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    28
    generator: T
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    29
}
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    30
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    31
impl <T: LandGenerator> LandSource<T> {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    32
    fn new(generator: T) -> Self {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    33
        let mut init = [0u8; 64];
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    34
        thread_rng().fill_bytes(&mut init);
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    35
        LandSource {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    36
            rnd: LaggedFibonacciPRNG::new(&init),
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    37
            generator
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    38
        }
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    39
    }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    40
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    41
    fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    42
        self.generator.generate_land(parameters, &mut self.rnd)
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    43
    }
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    44
}
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
    45
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    46
fn fill_pixels(pixels: &mut [u8], land: &Land2D<u32>) {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    47
    for (surf_row, land_row) in pixels.chunks_mut(land.width() * 4).zip(land.rows()) {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    48
        for (surf_pixel, land_pixel) in surf_row.chunks_mut(4).zip(land_row) {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    49
            if let [b, g, r, a] = surf_pixel {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    50
                *a = 255; *r = *land_pixel as u8;
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    51
            }
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    52
        }
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    53
    }
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    54
}
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    55
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    56
fn fill_texture(surface: &mut Surface, land: &Land2D<u32>) {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    57
    if surface.must_lock() {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    58
        surface.with_lock_mut(|data| fill_pixels(data, land));
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    59
    } else {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    60
        surface.without_lock_mut().map(|data| fill_pixels(data, land));
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    61
    }
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    62
}
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    63
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    64
fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    65
    thread_rng().gen_range(T::default(), max)
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    66
}
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    67
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    68
const WIDTH: u32 = 512;
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    69
const HEIGHT: u32 = 512;
14033
bf77c4d2294f fix theme editor
alfadur
parents: 14031
diff changeset
    70
const SIZE: Size = Size {width: 512, height: 512};
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
    71
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    72
fn point() -> Point {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    73
    Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    74
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    75
fn rect() -> Rect {
14090
43d956f41cd4 update theme editor
alfadur
parents: 14054
diff changeset
    76
    Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(120) + 8, rnd(120) + 8)
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    77
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    78
14098
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    79
fn land_rect() -> Rect {
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    80
    Rect::at_origin(SIZE)
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    81
}
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    82
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    83
fn test_template() -> OutlineTemplate {
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    84
    OutlineTemplate::new(SIZE)
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    85
        .with_fill_points(vec![land_rect().center()])
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    86
        .add_island(&land_rect().split_at(land_rect().center()))
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    87
}
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    88
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    89
fn init_source() -> LandSource<TemplatedLandGenerator> {
14098
a8e194ae26e8 add basic land template to theme editor
alfadur
parents: 14090
diff changeset
    90
    let template = test_template();
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    91
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    92
    let generator = TemplatedLandGenerator::new(template);
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    93
    LandSource::new(generator)
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    94
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    95
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    96
fn draw_random_lines(land: &mut Land2D<u32>) {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    97
    for i in 0..32 {
14090
43d956f41cd4 update theme editor
alfadur
parents: 14054
diff changeset
    98
        land.draw_thick_line(Line::new(point(), point()), rnd(5), u32::max_value());
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
    99
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   100
        land.fill_circle(point(), rnd(60), u32::max_value());
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   101
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   102
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   103
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   104
fn main() {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   105
    let sdl = sdl2::init().unwrap();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   106
    let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   107
    let events = sdl.event().unwrap();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   108
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   109
    let mut pump = sdl.event_pump().unwrap();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   110
    let video = sdl.video().unwrap();
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   111
    let window = video.window("Theme Editor", WIDTH, HEIGHT)
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   112
        .position_centered()
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   113
        .build().unwrap();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   114
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   115
    let mut source = init_source();
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   116
    let mut land = source.next(
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   117
        LandGenerationParameters::new(0, u32::max_value()));
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   118
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14033
diff changeset
   119
    let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   120
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   121
    fill_texture(&mut land_surf, &land);
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   122
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   123
    let mut win_surf = window.surface(&pump).unwrap();
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   124
    let win_rect = win_surf.rect();
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   125
    land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap();
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   126
    win_surf.update_window();
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   127
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   128
    'pool: loop {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   129
        use sdl2::event::Event::*;
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   130
        pump.pump_events();
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   131
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   132
        while let Some(event) = pump.poll_event() {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   133
            match event {
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   134
                Quit{ .. } => break 'pool,
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   135
                _ => ()
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   136
            }
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   137
        }
14028
0f517cbfe16d start a theme editor as an excuse to see land generator in action
alfadur
parents:
diff changeset
   138
    }
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 14028
diff changeset
   139
}