not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.2k stars 313 forks source link

Make Image Safe #634

Open sloganking opened 1 year ago

sloganking commented 1 year ago

Problem

Creating an image in Macroquad is verbose and unsafe. To create an image in macroquad I have to:

let mut image = Image::empty();
image.width = screen_width() as u16;
image.height = screen_height() as u16;
image.bytes = vec![0; image.width as usize * image.height as usize * 4];

If you do ANY of this wrong. You'll get segfaults or unsafe and undefined behavior.

Feature request

Why can't we have a safe image abstraction like the Image crate does? This is not only safer but also more convenient as it lets you create an image in one line.

let mut img = RgbImage::new(32, 32);

See their docs for more.

cyrgani commented 2 months ago

Actually, there already is the Image::gen_image_color function, which creates a new image with the given dimensions filled with the given color. Therefore, one can simply call Image::gen_image_color(screen_width() as u16, screen_height() as u16, BLANK), so I don't see much value in adding a new constructor just for blank images.