-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix a few issues with draw.rect() #2406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
See the PR for an actual explanation.
Woh, this is epic! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ty so much <3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
8000
LGTM 👍
Cheers :)
good to see that my benchmark helped to fix and verify that it works again as intended. |
@dr0id indeed! btw, @Starbuck5 would you mind pasting the code for the script with rounded corners added? |
Yeah, this is the script: Just changed a line or two from the original.
|
Thanks :) |
Fixes #2359, Fixes #2245
In pygame 2, there is an issue where rects draw like this:

Or even not draw at all, if the rect has negative width or height.
I tracked this back to 2.0.0.dev8, the first version where this issue appears. (The same version as rounded rects were added).
The reason for this was this bit of code in draw.c (https://github.com/pygame/pygame/blob/main/src_c/draw.c#L908:L910):
This bit of code really messes up the draw width of rectangles, usually evident with thin rectangles or ones with negative dimensions. (Especially with negative dimensions, since there isn't any
abs()
on it).I tracked down the commit that added this in #1503, and I found out that this is actually for the rounded_rect function, which was merged into the normal rect draw function, usable through keyword arguments. However when the two functions were merged this code chunk got added so that it affected all rects, not just ones that were going to be drawn rounded.
So I fixed the reported issues by moving the above chunk of code into the conditional for drawing rounded rects.
But then I thought I have such a nice rect benchmark by dr0id - I should set it up to draw rounded rects.

Turns out the rounded rect drawing was failing with negative width or negative height dimensions, so I set up some code to normalize the rects in the rounded rect draw.
The rounded rect drawing also fails on very thin rectangles. (See the normalized ones that are showing up as huge squares, like <rect(190, 433, 50, 1)>. I fixed this by only drawing a rect using the rounded rect code when it has a border_radius (or any variant) and it has a thickness on the x and y axis of 2 or more. (That threshold may seem arbitrary: but it's legit. Rects with an x/y dimension that equals 2 draw properly, those with dimensions that equal 0 or 1 draw improperly.)
Result:
