not-fl3 / macroquad

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

`draw_rectangle_lines` with thickness `1.0` only draws half of the rectangle #704

Open walter76 opened 4 months ago

walter76 commented 4 months ago

Hi,

I have no idea if this is on purpose, but with macroquad v0.4.5, when I use draw_rectangle_lines with a thickness of 1.0 I only get half of the rectangle drawn. It works fine with a thickness of 2.0.

Code to reproduce:

use macroquad::prelude::*;

#[macroquad::main("Macroquad PoCs")]
async fn main() {
    loop {
        clear_background(LIGHTGRAY);

        draw_rectangle_lines(100., 100., 200., 200., 1., BLUE);

        next_frame().await
    }
}

Sample image:

macroquad_half_rectangle

Is this a bug or does this have to do with some other setting?

/walter

gordug commented 3 months ago

Works in windows image Fails in Linux image

ElnuDev commented 3 months ago

I'm working on a pixel art game and this has been an absolute nightmare. I've tried messing around with different thicknesses and I can never get a perfect rectangle. The problem also extends to drawing simple lines as well, if you try to draw a rectangle out of lines very often the corners will be missing.

I really get the impression that the cause of this bug is weirdness with floating point numbers, maybe it would be appropriate to have a second set of drawing functions that take in integer coordinates?

After quite a bit of experimentation, I've come up with this which seems to render an entire rectangle with corners quite consistently. The main thing is to round all coordinates and then add a small epsilon to the coordinate of the top corner. Here's what I've come up with. It's really dirty, but it gets the job done. Your mileage may vary and this might require some adjustment depending on what you're doing.

let x = x.round();
let y = y.round();
let w = w.round();
let h = h.round();
const EPSILON: f32 = 0.5;
draw_rectangle_lines(x, y, w, h, 1.0, WHITE);
draw_line(x + EPSILON, y + EPSILON, x + w, y, 1.0, WHITE);
draw_line(x + w, y + EPSILON, x + w, y + h, 1.0, WHITE);
draw_line(x + w, y + h, x, y + h, 1.0, WHITE);
draw_line(x + EPSILON, y + h, x, y, 1.0, WHITE);

Since this seems to be a Linux-specific issue (haven't tested on Windows yet), you could wrap this in a #cfg[(target_os = "linux")] and then have the normal draw_rectangle_lines under a #cfg[(target_os = "windows")]. Hope this helps anyone who is having trouble with this same issue. Nope, it happens on Windows as well for me.

ozymandiaslone commented 3 months ago

idk if this is a good solution but I was able to fix this by changing line 55 in src/shapes.rs from let t = thickness / 2.;, to let t = f32::max(thickness / 2., 1.); just clamping the value of t to 1.0.