make theme editor render some random map lines
authoralfadur
Tue, 30 Oct 2018 02:14:00 +0300
changeset 14051 2ebd505e62c1
parent 14050 259175ab7e8c
child 14052 c47283feafac
make theme editor render some random map lines
rust/land2d/src/lib.rs
rust/theme-editor/Cargo.toml
rust/theme-editor/src/main.rs
rust/vec2d/src/lib.rs
--- a/rust/land2d/src/lib.rs	Mon Oct 29 23:40:17 2018 +0300
+++ b/rust/land2d/src/lib.rs	Tue Oct 30 02:14:00 2018 +0300
@@ -50,6 +50,11 @@
     }
 
     #[inline]
+    pub fn rows(&self) -> impl Iterator<Item = &[T]> {
+        self.pixels.rows()
+    }
+
+    #[inline]
     pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U {
         if self.is_valid_coordinate(x, y) {
             unsafe {
--- a/rust/theme-editor/Cargo.toml	Mon Oct 29 23:40:17 2018 +0300
+++ b/rust/theme-editor/Cargo.toml	Tue Oct 30 02:14:00 2018 +0300
@@ -13,4 +13,5 @@
 land2d = { path = "../land2d" }
 landgen = { path = "../landgen" }
 lfprng = { path = "../lfprng" }
+integral-geometry = { path = "../integral-geometry" }
 rand = "0.5"
\ No newline at end of file
--- 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
--- a/rust/vec2d/src/lib.rs	Mon Oct 29 23:40:17 2018 +0300
+++ b/rust/vec2d/src/lib.rs	Tue Oct 30 02:14:00 2018 +0300
@@ -69,6 +69,11 @@
     pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
         self.data.get_unchecked_mut(row * self.width + column)
     }
+
+    #[inline]
+    pub fn rows(&self) -> impl Iterator<Item = &[T]> {
+        self.data.chunks(self.width)
+    }
 }
 
 #[cfg(test)]