Skip to content

Instantly share code, notes, and snippets.

@j-walk
Created February 14, 2018 06:44
Show Gist options
  • Save j-walk/8e7091c84b24c4bbbdc9ed5acbd52e8b to your computer and use it in GitHub Desktop.
Save j-walk/8e7091c84b24c4bbbdc9ed5acbd52e8b to your computer and use it in GitHub Desktop.
extern crate sdl2;
extern crate noise;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;
use noise::{NoiseModule, Perlin};
use noise::Seedable;
use std::{thread, time};
// mapping range a-b to range c-d
fn map(x : f32, a : f32, b : f32, c : f32, d : f32) -> f32 {
(x - a) / (b - a) * (d-c) + x
}
fn main() {
const STEP : f32 = 0.01;
// dumb shit for init sdl
let mut sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("noise", 800,800)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut time = 0.0;
canvas.set_draw_color(sdl2::pixels::Color::RGB(0,0,0));
canvas.clear();
// new perlin struct with seed
let perlin = Perlin::new().set_seed(31415926535);
'running: loop {
time += STEP;
// dumb shit so SDL doesn't cry when you exit it
for e in event_pump.poll_iter() {
match e {
Event::Quit {..} |
Event::KeyDown { keycode : Some(Keycode::Escape), .. }
=> break 'running,
_ => ()
}
}
// cancer plz dn't replicate
for i in 0..400i32 {
for j in 0..400i32 {
// getting a shitty noise value from mapped, then remapped values
let c = map(perlin.get([ (i as f32) / 100.0
, (j as f32) / 100.0
, time, 0.0])
, -1.0,1.0,0.0,255.0) as u8;
// setting the color to the noise value
canvas.set_draw_color(sdl2::pixels::Color::RGB(c,c,c));
// then drawing at that point
canvas.fill_rect(Rect::new(i*2,j*2,2,2)).unwrap();
}
}
canvas.present();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment