Last active
July 29, 2018 20:42
-
-
Save la10736/4005f4ddc963b978bf03aa97ac1eda3a to your computer and use it in GitHub Desktop.
minfb random gray levels
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
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
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
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
[package] | |
name = "try_minifb" | |
version = "0.1.0" | |
authors = ["michele <michele.damico@gmail.com>"] | |
[dependencies] | |
minifb = "0.10.6" | |
rand = "*" |
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
extern crate minifb; | |
extern crate rand; | |
use minifb::{Key, WindowOptions, Window}; | |
const WIDTH: usize = 640; | |
const HEIGHT: usize = 360; | |
const ALPHA_FILL: u8 = 255; | |
const BLUE_OFFSET: usize = 0; | |
const GREEN_OFFSET: usize = 8; | |
const RED_OFFSET: usize = 16; | |
const ALPHA_OFFSET: usize = 24; | |
struct Pixel(u32); | |
impl Pixel { | |
pub fn by_colors(red: u8, green:u8, blue: u8) -> Self { | |
Self::by_colors_alpha(red, green, blue, ALPHA_FILL) | |
} | |
pub fn by_colors_alpha(red: u8, green:u8, blue: u8, alpha: u8) -> Self { | |
Pixel(((blue as u32) << BLUE_OFFSET) | ((green as u32) << GREEN_OFFSET) | | |
((red as u32) << RED_OFFSET) | ((alpha as u32) << ALPHA_OFFSET) ) | |
} | |
pub fn bw(level: u8) -> Self { | |
Self::bw_alpha(level, ALPHA_FILL) | |
} | |
pub fn bw_alpha(level: u8, alpha: u8) -> Self { | |
Pixel::by_colors_alpha(level, level, level, alpha) | |
} | |
pub fn as_bw(&self) -> Self { | |
let level = (((self.blue() as u32) + (self.green() as u32) + (self.red() as u32)) / 3) as u8; | |
Self::bw_alpha(level, self.alpha()) | |
} | |
pub fn blue(&self) -> u8 { | |
((self.0 >> BLUE_OFFSET) & 0xFF) as u8 | |
} | |
pub fn green(&self) -> u8 { | |
((self.0 >> GREEN_OFFSET) & 0xFF) as u8 | |
} | |
pub fn red(&self) -> u8 { | |
((self.0 >> RED_OFFSET) & 0xFF) as u8 | |
} | |
pub fn alpha(&self) -> u8 { | |
((self.0 >> ALPHA_OFFSET) & 0xFF) as u8 | |
} | |
} | |
impl From<u32> for Pixel { | |
fn from(val: u32) -> Self { | |
Pixel(val) | |
} | |
} | |
impl Into<u32> for Pixel { | |
fn into(self) -> u32 { | |
self.0 | |
} | |
} | |
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) { | |
for i in buffer.iter_mut() { | |
*i = Pixel::from(rand::random::<u32>()).as_bw().into(); // write something more funny here! | |
} | |
// 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_with_buffer(&buffer).unwrap(); | |
} | |
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment