Add draw.aarect() to draw anti-aliased and rounded rectangles#3701
Add draw.aarect() to draw anti-aliased and rounded rectangles#3701Fantastair wants to merge 4 commits intopygame-community:mainfrom
Conversation
Update draw_module_example to include aarect example
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including You can disable this status message by setting the
📝 WalkthroughWalkthroughAdds a new public drawing function, pygame.draw.aarect, implemented in C (Xiaolin Wu anti-aliasing), with pyi stub, documentation examples, and tests; binds the C implementation to the draw module and exposes it in the test suite. Changes
Sequence Diagram(s)sequenceDiagram
participant Py as Python caller
participant DrawMod as draw module binding
participant CImpl as C implementation (aarect)
participant SDL as SDL_Surface
Py->>DrawMod: pygame.draw.aarect(surface, color, rect, ...)
DrawMod->>CImpl: call aarect(...) (args parsed)
CImpl->>CImpl: validate args & compute radii/area
CImpl->>SDL: lock surface
CImpl->>CImpl: if rounded -> draw_round_rect_xiaolinwu(...)
CImpl->>SDL: blit/modify pixels within drawn area
CImpl->>SDL: unlock surface
CImpl-->>DrawMod: return drawn Rect
DrawMod-->>Py: return Rect
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src_c/draw.c`:
- Around line 4259-4268: Run the formatter to fix clang-format issues affecting
draw_round_rect_xiaolinwu and surrounding declarations: execute "python3 dev.py
format" (or "python3 dev.py all" to catch all files), review the changes to the
draw_round_rect_xiaolinwu function signature and local variable declarations
(pts, q_top, q_left, q_bottom, q_right, f) to ensure only style changes were
applied, then stage and commit the formatted changes.
In `@test/draw_test.py`:
- Around line 5173-5180: The tests assign to an unused variable bounds_rect
inside assertRaises blocks causing F841 lint errors; remove the assignments so
the calls to self.draw_aarect(...) are executed only for their exception check
(e.g., change "bounds_rect = self.draw_aarect(...)" to "self.draw_aarect(...)"
in the assertRaises blocks). Apply the same change for all occurrences mentioned
(other blocks around lines referencing draw_aarect and bounds_rect).
| with self.assertRaises(TypeError): | ||
| bounds_rect = self.draw_aarect(surface, pygame.Color("white")) | ||
|
|
||
| with self.assertRaises(TypeError): | ||
| bounds_rect = self.draw_aarect(surface) | ||
|
|
||
| with self.assertRaises(TypeError): | ||
| bounds_rect = self.draw_aarect() |
There was a problem hiding this comment.
Fix F841 lint errors from unused bounds_rect assignments.
Ruff/Flake8 flag these as unused locals; this can fail CI. Drop the assignments inside the assertRaises blocks.
✅ Suggested fix (apply similarly to the other blocks)
- with self.assertRaises(TypeError):
- bounds_rect = self.draw_aarect(surface, pygame.Color("white"))
+ with self.assertRaises(TypeError):
+ self.draw_aarect(surface, pygame.Color("white"))Also applies to: 5196-5201, 5241-5249, 5358-5360, 5383-5385, 5473-5477, 5518-5522
🧰 Tools
🪛 Flake8 (7.3.0)
[error] 5180-5180: local variable 'bounds_rect' is assigned to but never used
(F841)
🪛 Ruff (0.14.14)
[error] 5180-5180: Local variable bounds_rect is assigned to but never used
Remove assignment to unused variable bounds_rect
(F841)
🤖 Prompt for AI Agents
In `@test/draw_test.py` around lines 5173 - 5180, The tests assign to an unused
variable bounds_rect inside assertRaises blocks causing F841 lint errors; remove
the assignments so the calls to self.draw_aarect(...) are executed only for
their exception check (e.g., change "bounds_rect = self.draw_aarect(...)" to
"self.draw_aarect(...)" in the assertRaises blocks). Apply the same change for
all occurrences mentioned (other blocks around lines referencing draw_aarect and
bounds_rect).
Implemented the enhancement mentioned in #3700.
Added the following to
src_c/draw.c:draw_round_rect_xiaolinwu()function, used to draw anti-aliased rounded rectangles (corresponding to the functionality ofdraw_round_rect(), but internally usesdraw_circle_xiaolinwu()to draw the rounded corners).aarect()function serves as the Python API interface. Its parameters and functionality are identical to thedraw.rect()function, except it draws anti-aliased rounded rectangles. Internally, it callsdraw_round_rect_xiaolinwu().Added example code related to
aarect()indocs/reST/ref/code_examples/draw_module_example.py.Added function annotations for
aarect()inbuildconfig/stubs/pygame/draw.pyi.Added
aarect()-related unit tests totest/draw_test.py. (Incidentally, I noticed thetest_color_validationsubtest in theDrawModuleTestclass lacked coverage fordraw.rect(), so I added it as well.)