Skip to content

Add special_flags to Group.draw() #3321

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

Merged

Conversation

MarcellPerger1
Copy link
Contributor

@MarcellPerger1 MarcellPerger1 commented Feb 1, 2025

Add special_flags to Group.draw(), fixes #3306.
I implemented this in the same way as in pygame/pygame#3722 for compatibility with pygame/pygame:

  • I added bgsurf as a (sometimes unused) 2nd argument to to all the Group.draw() methods so that special_flags is consistently the 3rd argument across all subclasses.
  • For LayeredUpdates.draw(), special_flags (if provided as non-None) overrides the blendmode of sprites.

In the docs, I haven't added the versionchanged because I don't know if the next version (or version where this will be merged) will be 2.5.3 or 2.6.0.

I haven't used fblits in this PR as I thought it might be better to do that in separate PR (let me know if I should do it in this PR)

I haven't added the `versionchanged` because I don't know if the next version will be 2.5.3 or 2.6.0
@MarcellPerger1 MarcellPerger1 requested a review from a team as a code owner February 1, 2025 11:10
@Starbuck5 Starbuck5 requested a review from dr0id February 1, 2025 20:26
@Starbuck5
Copy link
Member

In the docs, I haven't added the versionchanged because I don't know if the next version (or version where this will be merged) will be 2.5.3 or 2.6.0.

2.5.4, as of today :)

@@ -551,10 +555,11 @@ Sprites are not thread safe. So lock them yourself if using threads.
.. method:: draw

| :sl:`draw all sprites in the right order onto the passed surface.`
| :sg:`draw(surface, bgd=None) -> Rect_list`
| :sg:`draw(surface, bgsurf=None, special_flags=0) -> Rect_list`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

special_flags should default to None.

@aatle
Copy link
Contributor

aatle commented Feb 2, 2025

The feature is nice, but the implementation looks strange (it can't be changed though, for pygame compatibility).
Rather than adding a confusing unused bgsurf argument for all sprite groups to keep consistency with a single, rarely used LayeredDirty subclass, it would have been better to make special_flags keyword-only. Every code example given has used it as a keyword argument.
I also don't see why has bgd been renamed to bgsurf. This is technically a breaking change for LayeredDirty (though it probably won't have much impact).

@MarcellPerger1
Copy link
Contributor Author

I also don't see why has bgd been renamed to bgsurf. This is technically a breaking change for LayeredDirty (though it probably won't have much impact).

The only reason I changed it here is for compatibility with pygame, although it might be better just to keep it as bgd for compatibility with previous versions of pygame-ce (personally, I think compatibility with previous versions of pygame-ce is more important than compatibility with pygame).
I can change it back to bgd if you think that would be better.

@Starbuck5
Copy link
Member

(personally, I think compatibility with previous versions of pygame-ce is more important than compatibility with pygame).

I agree.

@MarcellPerger1
Copy link
Contributor Author

I've changed bgsurf back to bgd. Do I need to do anything else to help get this merged?

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with sprite stuff, but this PR LGTM at a glance.

Thanks for contributing to pygame-ce! 🥳

@Starbuck5
Copy link
Member

@aatle if this looks okay to you I would be okay with merging.

for spr in _sprites:
flags = spr.blendmode if _special_flags is None else _special_flags
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible for _special_flags to be None because it is already checked for in draw().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible for it to be None here because when the code checks for None in draw(), the result is assigned to a different variable (flags) and isn't assigned back to the special_flags variable.
I implemented it in this way because if special_flags isn't specified, it defaults back to 0 when drawing the background but defaults to spr.blendmode when drawing each sprite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
It is a bit confusing with all of the flags. Perhaps rename the background flags from flags to bgd_flags?

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me.
Thanks!

@Starbuck5 Starbuck5 merged commit 812a1a4 into pygame-community:main Apr 12, 2025
25 checks passed
@Starbuck5 Starbuck5 added New API This pull request may need extra debate as it adds a new class or function to pygame sprite pygame.sprite labels Apr 12, 2025
@Starbuck5 Starbuck5 added this to the 2.5.4 milestone Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New API This pull request may need extra debate as it adds a new class or function to pygame sprite pygame.sprite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow passing special_flags to Group.draw
4 participants