photo_frame/src/main.rs
2024-11-04 00:48:49 +01:00

62 lines
1.6 KiB
Rust

use dimensions::{calculate_resize_dimensions, Dimensions};
use image::ImageReader;
use macroquad::prelude::*;
mod dimensions;
fn window_conf() -> Conf {
Conf {
window_title: String::from("Photo"),
window_width: 2560,
window_height: 1440,
fullscreen: true,
..Default::default()
}
}
fn display_image(texture: &Texture2D) {
let screen_width = screen_width();
let screen_height = screen_height();
draw_texture(
&texture,
clamp(screen_width - texture.width(), 0.0f32, screen_width) / 2.0f32,
clamp(screen_height - texture.height(), 0.0f32, screen_height) / 2.0f32,
WHITE,
);
}
#[macroquad::main(window_conf)]
async fn main() {
show_mouse(false);
let image = ImageReader::open("examples/test.jpg")
.unwrap()
.decode()
.unwrap();
let screen_dim = Dimensions::new(screen_width() as u32, screen_height() as u32);
let image_dim = Dimensions::new(image.width(), image.height());
let new_dim = calculate_resize_dimensions(screen_dim, image_dim);
let image = image.resize(
new_dim.width,
new_dim.height,
image::imageops::FilterType::Lanczos3,
);
let texture = Texture2D::from_rgba8(
image.width().try_into().unwrap(),
image.height().try_into().unwrap(),
&image.into_rgba8().to_vec(),
);
loop {
clear_background(BLACK);
if is_quit_requested() || is_key_released(KeyCode::Escape) {
break;
}
display_image(&texture);
next_frame().await
}
}