Last active
January 7, 2021 07:27
-
-
Save mebiusbox/dbd47838d17d35f4891882506d5efb9f to your computer and use it in GitHub Desktop.
Use minifb to display the generated image of type image::RgbImage in a window
This file contains 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
// image = "*" | |
// minifb = "0.19.1" | |
use crate::{ | |
IMAGE_HEIGHT, IMAGE_WIDTH, | |
}; | |
use minifb::{Key, Window, WindowOptions}; | |
use image::{RgbImage}; | |
pub fn draw_in_window(mut pixels: RgbImage) -> std::io::Result<()> | |
{ | |
let mut buffer: Vec<u32> = vec![0; (IMAGE_WIDTH * IMAGE_HEIGHT) as usize]; | |
let mut window = Window::new( | |
"ESC to exit", | |
IMAGE_WIDTH as usize, IMAGE_HEIGHT as usize, | |
WindowOptions { | |
topmost: true, | |
..WindowOptions::default() | |
}, | |
) | |
.unwrap_or_else(|e| { | |
panic!("{}", e); | |
}); | |
// Limit to max 30fps | |
window.limit_update_rate(Some(std::time::Duration::from_micros(16600*2))); | |
for (i, (_, _, pixel)) in buffer.iter_mut().zip(pixels.enumerate_pixels_mut()) { | |
*i = u32::from_be_bytes([0, pixel[0], pixel[1], pixel[2]]); | |
} | |
window.update_with_buffer(&buffer, IMAGE_WIDTH as usize, IMAGE_HEIGHT as usize).unwrap(); | |
while window.is_open() && !window.is_key_down(Key::Escape) { | |
window.update(); | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment