Last active
September 9, 2019 01:57
-
-
Save jackmott/2fb5074ed183a025fe8f641548cd2f9e to your computer and use it in GitHub Desktop.
chunk noise example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use minifb::{Key, Window, WindowOptions}; | |
use simdnoise::NoiseBuilder; | |
use std::{thread, time}; | |
const WIDTH: usize = 640; | |
const HEIGHT: usize = 640; | |
fn main() { | |
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT]; | |
let mut window = Window::new( | |
"Test - ESC to exit", | |
WIDTH, | |
HEIGHT, | |
WindowOptions::default(), | |
) | |
.unwrap_or_else(|e| { | |
panic!("{}", e); | |
}); | |
while window.is_open() && !window.is_key_down(Key::Escape) { | |
let chunk_size = WIDTH / 2; | |
println!("test"); | |
for chunk_y in 0..2 { | |
for chunk_x in 0..2 { | |
let x_offset = (chunk_x * chunk_size); | |
let y_offset = (chunk_y * chunk_size); | |
let W = WIDTH / 2; | |
let H = HEIGHT / 2; | |
println!("xoffset:{} yoffset:{} W:{} H:{}", x_offset, y_offset, W, H); | |
let noise = NoiseBuilder::fbm_2d_offset(x_offset as f32, W, y_offset as f32, H) | |
.generate_scaled(0.0, 1.0); | |
for i in 0..W * H { | |
let n = (noise[i] * 255.0).floor() as u32; | |
let y = i / W; | |
let x = i % W; | |
let winX = x + x_offset; | |
let winY = y + y_offset; | |
buffer[winY * WIDTH + winX] = (n << 16) | (n << 8) | n; | |
} | |
window.update_with_buffer(&buffer).unwrap(); | |
thread::sleep(time::Duration::from_millis(1000)); | |
} | |
} | |
// We unwrap here as we want this code to exit if it fails. Real applications may want to handle this in a different way | |
window.update(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment