Created
June 25, 2017 19:29
-
-
Save yupferris/25345b335d8cf19559d8640ec20e1ffb to your computer and use it in GitHub Desktop.
wavy stuff
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
extern crate minifb; | |
extern crate time; | |
use minifb::{Key, Scale, WindowOptions, Window}; | |
use std::f64::consts::PI; | |
fn circle(x: f64, y: f64, x_offset: f64, y_offset: f64, rad: f64) -> bool { | |
let x_distance = x - x_offset; | |
let y_distance = y - y_offset; | |
let distance = (x_distance * x_distance + y_distance * y_distance).sqrt(); | |
distance <= rad | |
} | |
fn main() { | |
const WIDTH: usize = 256; | |
const HEIGHT: usize = 256; | |
let mut buffer = vec![0; WIDTH * HEIGHT].into_boxed_slice(); | |
let mut window = Window::new( | |
"BG stuff", | |
WIDTH, | |
HEIGHT, | |
WindowOptions { | |
borderless: false, | |
title: true, | |
resize: false, | |
scale: Scale::X2, | |
}).unwrap(); | |
const CHAR_SIZE: usize = 8; | |
let mut tile = Vec::new(); | |
const TILE_SIZE: usize = 32; | |
for y in 0..TILE_SIZE { | |
for x in 0..TILE_SIZE { | |
let mut c = 0; | |
let large_circle_x = -3.0; | |
let large_circle_y = -3.0; | |
let large_circle_rad = (TILE_SIZE / 3) as f64; | |
if circle(x as _, y as _, large_circle_x, large_circle_y, large_circle_rad) || | |
circle(x as _, y as _, large_circle_x + (TILE_SIZE as f64), large_circle_y, large_circle_rad) || | |
circle(x as _, y as _, large_circle_x, large_circle_y + (TILE_SIZE as f64), large_circle_rad) || | |
circle(x as _, y as _, large_circle_x + (TILE_SIZE as f64), large_circle_y + (TILE_SIZE as f64), large_circle_rad) { | |
c = 1; | |
} | |
if circle(x as _, y as _, (TILE_SIZE / 3) as _, (TILE_SIZE / 3) as _, (TILE_SIZE / 4) as _) { | |
c = 2; | |
} | |
if circle(x as _, y as _, (TILE_SIZE / 2) as _, (TILE_SIZE / 2) as _, (TILE_SIZE / 3) as _) { | |
c = 3; | |
} | |
tile.push(c); | |
} | |
} | |
let tile = tile.into_boxed_slice(); | |
let palette = [ | |
0xfaf6d3, | |
0xaa4ca0, | |
0x56bfba, | |
0x5f4584, | |
]; | |
let start_time = time::precise_time_s(); | |
while window.is_open() && !window.is_key_down(Key::Escape) { | |
let time = time::precise_time_s() - start_time; | |
let t = time * 3.0; | |
for y in 0..HEIGHT { | |
for x in 0..WIDTH { | |
let tile_x = (x / CHAR_SIZE) as i32; | |
let tile_y = (y / CHAR_SIZE) as i32; | |
let offset_x = (x as i32) + (((tile_x as f64) * 0.2 + (tile_y as f64) * 0.1 + t).sin() * 16.0) as i32; | |
let offset_y = (y as i32) + (((tile_y as f64) * 0.2 + (tile_x as f64) * 0.1 + t * 0.9).sin() * 16.0) as i32; | |
let c = tile[((offset_y as usize) % TILE_SIZE) * TILE_SIZE + ((offset_x as usize) % TILE_SIZE)]; | |
buffer[(y * WIDTH + x) as usize] = palette[c as usize] as u32; | |
} | |
} | |
window.update_with_buffer(&buffer); | |
} | |
println!("Hello, world!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment