use blit::*;
use image::*;
use minifb::*;
use rotsprite::rotsprite;
use std::thread::sleep;
use std::time::Duration;
const MASK_COLOR: u32 = 0xFF_00_FF;
fn main() {
let img = image::open("examples/threeforms.png").unwrap();
let size = img.dimensions();
let mut img_buf: Vec<u32> = vec![0xFF_FF_FF; (size.0 * size.1) as usize];
img.as_rgba8()
.expect("Could not convert image to RGBA8 array")
.blit(
&mut img_buf,
size.0 as usize,
(0, 0),
Color::from_u32(MASK_COLOR),
);
let width = size.0 as usize * 2;
let height = size.1 as usize * 2;
let options = WindowOptions {
scale: Scale::X1,
..WindowOptions::default()
};
let mut window = Window::new(
"Rotsprite Example - ESC to exit & click to draw",
width,
height,
options,
)
.expect("Unable to open window");
let mut rotation: f64 = 0.0;
let mut buf: Vec<u32> = vec![0; width * height];
while window.is_open() && !window.is_key_down(Key::Escape) {
buf.iter_mut().for_each(|p| *p = 0xAA_AA_AA);
let (rotated_width, rotated_height, rotated) = rotsprite(
&img_buf,
&0xFF_FF_FF_FF,
img.width() as usize,
(rotation / 15.0).round() * 15.0,
)
.expect("Could not rotate sprite");
for y in 0..rotated_height {
for x in 0..rotated_width {
buf[y * width + x] = rotated[y * rotated_width + x];
}
}
window.update_with_buffer(&buf, width, height).unwrap();
rotation += 0.5;
sleep(Duration::from_millis(12));
}
}