rust/theme-editor/src/main.rs
changeset 14030 2ebd505e62c1
parent 14028 0f517cbfe16d
child 14031 c47283feafac
--- a/rust/theme-editor/src/main.rs	Mon Oct 29 23:40:17 2018 +0300
+++ b/rust/theme-editor/src/main.rs	Tue Oct 30 02:14:00 2018 +0300
@@ -1,10 +1,17 @@
 use sdl2::{
     keyboard::Scancode,
-    event::EventType
+    event::EventType,
+    surface::Surface,
+    pixels::{
+        PixelFormatEnum, Color
+    }
 };
 
+use integral_geometry::Point;
+
 use rand::{
-    thread_rng, RngCore
+    thread_rng, RngCore, Rng,
+    distributions::uniform::SampleUniform
 };
 
 use landgen::{
@@ -33,6 +40,31 @@
     }
 }
 
+fn fill_pixels(pixels: &mut [u8], land: &Land2D<u32>) {
+    for (surf_row, land_row) in pixels.chunks_mut(land.width() * 4).zip(land.rows()) {
+        for (surf_pixel, land_pixel) in surf_row.chunks_mut(4).zip(land_row) {
+            if let [b, g, r, a] = surf_pixel {
+                *a = 255; *r = *land_pixel as u8;
+            }
+        }
+    }
+}
+
+fn fill_texture(surface: &mut Surface, land: &Land2D<u32>) {
+    if surface.must_lock() {
+        surface.with_lock_mut(|data| fill_pixels(data, land));
+    } else {
+        surface.without_lock_mut().map(|data| fill_pixels(data, land));
+    }
+}
+
+fn rnd<T: Default + SampleUniform + Ord>(max: T) -> T {
+    thread_rng().gen_range(T::default(), max)
+}
+
+const WIDTH: u32 = 512;
+const HEIGHT: u32 = 512;
+
 fn main() {
     let sdl = sdl2::init().unwrap();
     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
@@ -40,10 +72,28 @@
 
     let mut pump = sdl.event_pump().unwrap();
     let video = sdl.video().unwrap();
-    let _window = video.window("Theme Editor", 640, 480)
+    let window = video.window("Theme Editor", WIDTH, HEIGHT)
         .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 land = Land2D::new(WIDTH as usize, HEIGHT as usize, 0);
+    for i in 0..64 {
+        land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
+    }
+
+    fill_texture(&mut land_surf, &land);
+
+    let mut win_surf = window.surface(&pump).unwrap();
+    let win_rect = win_surf.rect();
+    land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap();
+    win_surf.update_window();
+
     'pool: loop {
         use sdl2::event::Event::*;
         pump.pump_events();
@@ -53,8 +103,6 @@
                 Quit{ .. } => break 'pool,
                 _ => ()
             }
-        }    
+        }
     }
-}
-
-
+}
\ No newline at end of file