- Using
minifbhow do you make make a image transparent? The window option for transparency makes sense, but gives issues as shown below. - Besides I was wondering if there was a cleaner way to handle my
if mode != 0 && mode != 1bit. I feel I should be able to utilize the code in the if, but I am not sure how.
I use Linux btw so it should be implemented: WindowOptions in minifb - Rust
Code
const WIDTH: usize = 900;
const HEIGHT: usize = 900;
struct Image {
width: usize,
height: usize,
buffer: Vec<u32>,
}
impl Image {
fn load(filename: &str) -> Image {
let image =
lodepng::decode32_file(&filename).expect(&format!("Couldn't load {}", &filename));
Image {
width: image.width,
height: image.height,
buffer: image
.buffer
.iter()
.map(|color| {
return u32::from_le_bytes([color.b, color.g, color.r, color.a]);
})
.collect(),
}
}
}
fn main() {
let image1 = Image::load("mainframe1.png");
let image2 = Image::load("mainframe2.png");
let mut window = minifb::Window::new(
"Rotating images",
WIDTH as usize,
HEIGHT as usize,
minifb::WindowOptions {
resize: false,
topmost: true,
scale: minifb::Scale::X1,
transparency: true,
borderless: true,
none: true,
..minifb::WindowOptions::default()
},
)
.expect("Unable to create window");
let mut buffer = vec![0u32; (WIDTH * HEIGHT) as usize];
// The current rotation in radians (0..2*Pi)
let mut rotation = 0.0;
let mut mode = 0;
while window.is_open() && !window.is_key_down(minifb::Key::Escape) {
rotation += 0.01;
if window.is_key_pressed(minifb::Key::Space, minifb::KeyRepeat::No) {
mode = (mode + 1) % 3;
}
if mode == 0 {
window.update_with_buffer(&image1.buffer, image1.width, image1.height).unwrap();
} else if mode == 1 {
// TODO: Adjust Brightness image 1 to 25% (potentially utilizing https://docs.rs/minifb/latest/minifb/struct.WindowOptions.html)
let new_buffer: Vec<u32> = image1.buffer.to_vec();
let newer_buffer = (new_buffer[3] as f32 * 0.25) as u32;
let newest_buffer: Vec<u32> = vec![new_buffer[0], new_buffer[1], new_buffer[2], newer_buffer];
window.update_with_buffer(&newest_buffer, image1.width, image1.height).unwrap();
} else if mode == 2 {
// TODO: Fill and super impose both images. Decrease both images their brightness to 50%
} else if mode == 3 {
// TODO: Offset and rotate image 1
// let positions: (isize, isize) = window.get_position();
// window.set_position(p, y);
} else if mode == 4 {
// TODO: Offset and rotate two superimposed images in opposite directions
} else {
// Optimized
// For optimization code can be combined here instead of the deduplication
// in the name of speed
}
// Show the buffer in the window
if mode != 0 && mode != 1 {
window.update_with_buffer(&buffer, WIDTH as usize, HEIGHT as usize).unwrap();
}
}
Output
thread 'main' (113142) panicked at src/main.rs:79:84:
called `Result::unwrap()` on an `Err` value: Failed to Update, "Update failed because input buffer is too small. Required size for 885 (885 stride) x 720 buffer is 2548800\n bytes but the size of the input buffer has the size 16 bytes"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: queue 0x626b17bc63e0 destroyed while proxies still attached:
wl_surface#15 still attached
wl_shm_pool#14 still attached
xdg_toplevel#13 still attached
xdg_surface#12 still attached
xdg_wm_base#11 still attached
wl_buffer#10 still attached
wl_shm_pool#9 still attached
wl_surface#8 still attached
wl_shm#7 still attached
wl_compositor#6 still attached
wl_pointer#5 still attached
wl_keyboard#4 still attached
wl_seat#3 still attached
wl_registry#2 still attached