Skip to content
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

Allow erasing pixels in pygame.Surface.scroll and add repeat functionality #2855

Merged
merged 32 commits into from
Oct 18, 2024

Conversation

damusss
Copy link
Contributor

@damusss damusss commented May 14, 2024

The shifting was incorrect, not setting the space created by the shifted pixels to black. Now it does that [EDIT: now it does that with the erase flag only], but most importantly I think I implemented this feature idea: #2159 implementing a way to allow for repeated shifting. The video proof is here: https://discord.com/channels/772505616680878080/772940667231928360/1240055647584911443

I would love feedback about the C code as I did quite a lot of pointer operations.
Why?

  • The erasing functionality is probably expected to happen so it's nice to have it
  • The repeat functionality gives to this function some reason to exist
  • It's cool

Question: how can I add a test for it? Edit: I modified the test, tell me if it's incomplete

You can use the following code with my request to test locally:

import pygame
pygame.init()
win = pygame.Window(size=(700, 700))
win.get_surface()
clock = pygame.Clock()

surf = pygame.image.load("B:/py/contributing/test.png").convert_alpha() # your image
surf = surf.subsurface(surf.get_bounding_rect())

main = pygame.Surface((600, 600))
main.blit(surf, surf.get_rect(center = (main.get_width()/2, main.get_height()/2)))
#main.set_clip(pygame.Rect(50, 50, 400, 400))

FLAG = pygame.SCROLL_REPEAT
SPEED = 4

while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            exit()
            
    win.get_surface().fill("black")
    screen = win.get_surface()    
    
    keys = pygame.key.get_pressed()
    dir = [0, 0]
    if keys[pygame.K_LEFT]:
        dir[0] -= SPEED
    if keys[pygame.K_RIGHT]:
        dir[0] += SPEED
    if keys[pygame.K_UP]:
        dir[1] -= SPEED
    if keys[pygame.K_DOWN]:
        dir[1] += SPEED
    main.scroll(dir[0], dir[1], FLAG)
    
    screen.blit(main, main.get_rect(center=(win.size[0]/2, win.size[1]/2)))
    
    win.flip()
    dt = clock.tick(60)/1000
    win.title = str(clock.get_fps())
    

@damusss damusss requested a review from a team as a code owner May 14, 2024 22:16
@damusss damusss marked this pull request as draft May 14, 2024 22:18
@damusss damusss marked this pull request as ready for review May 15, 2024 19:43
@damusss
Copy link
Contributor Author

damusss commented May 19, 2024

Could the 2 faulty tests be rerun?

@MyreMylar
Copy link
Member

MyreMylar commented May 25, 2024

The shifting was incorrect, not setting the space created by the shifted pixels to black.

Why do you think this is incorrect? The docs currently say:

Move the image by dx pixels right and dy pixels down. dx and dy may be negative for left and up scrolls respectively. Areas of the surface that are not overwritten retain their original pixel values.

Which looks to me like what the current behaviour is.

e.g.

Current main branch scroll a 200 pixel wide surface right by 100 pixels:

image

After this PR:

image

I think we should retain the current behaviour as the default, and if we want the original pixels not overwritten by scrolled pixels cleared to a colour (and why specifically black?) that should be an option/parameter. Otherwise we will probably accidentally break somebody's code.


As an addendum - do we think this function is likely to be a performance sensitive one like blit, or one used less frequently? I'm wondering whether it should support keyword arguments or not, now it has 3 params (possibly 4 after the above).

Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

See comment above.

@damusss
Copy link
Contributor Author

damusss commented May 25, 2024

@MyreMylar yeah that was a misunderstanding on my side. I don't think 4 arguments are too much. If they are, what do you suggest? Another function for repeat? only positional?

@damusss damusss requested a review from a team as a code owner May 25, 2024 20:20
@damusss damusss changed the title Remove artifacts on pygame.Surface.scroll and add repeat argument Allow erasing pixels in pygame.Surface.scroll and add repeat functionality May 25, 2024
@bilhox
Copy link
Contributor

bilhox commented Jun 7, 2024

Looking at how you implemented it, I believe the best option would be to use the same system used for flags on surfaces and window. Like that we can a fair amount of arguments for this function.

@damusss
Copy link
Contributor Author

damusss commented Jun 7, 2024

Looking at how you implemented it, I believe the best option would be to use the same system used for flags on surfaces and window. Like that we can a fair amount of arguments for this function.

That would require 2 new constants pygame.SCROLL_REPEAT and pygame.SCROLL_ERASE i believe. the new arguments would go from 2 to 1, i guess

@bilhox
Copy link
Contributor

bilhox commented Jun 7, 2024

Looking at how you implemented it, I believe the best option would be to use the same system used for flags on surfaces and window. Like that we can a fair amount of arguments for this function.

That would require 2 new constants pygame.SCROLL_REPEAT and pygame.SCROLL_ERASE i believe. the new arguments would go from 2 to 1, i guess

I prefer this approach.

@yunline yunline added the Surface pygame.Surface label Jun 11, 2024
src_c/surface.c Outdated Show resolved Hide resolved
@bilhox
Copy link
Contributor

bilhox commented Jun 21, 2024

I think there were separate functions for the actual process of scrolling and the function definition, what about doing this to keep some kind of readability ?

@bilhox
Copy link
Contributor

bilhox commented Jun 22, 2024

After a little digging with @damusss about some issues, here are the potential problem we are facing out currently:

  • erase mode doesn't work when we have an alpha channel.
  • There are missing pixels to erase sometimes, see for example this video (at 00:04, see at bottom right) :
surface_scroll_erase.mp4

docs/reST/ref/surface.rst Outdated Show resolved Hide resolved
Co-authored-by: Dan Lawrence <[email protected]>
@MyreMylar
Copy link
Member

All looks good to me now, the three modes work as I expect and add some nice functionality to this function which I expect is not often used in it's current form.

Perhaps it will see a resurgence? 👍

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame dont merge labels Oct 8, 2024
@damusss damusss removed the dont merge label Oct 8, 2024
Copy link
Contributor

@bilhox bilhox left a comment

Choose a reason for hiding this comment

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

except what I mentionned, nice, it works correctly now !

src_c/surface.c Outdated Show resolved Hide resolved
@bilhox bilhox added this to the 2.5.3 milestone Oct 17, 2024
Scrolling is contained by the Surface clip area. It is safe to have dx
and dy values that exceed the surface size.

The scroll flag can be ``0`` (normal behavior) or:
Copy link
Contributor

Choose a reason for hiding this comment

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

you removed the old text, so what is the "normal behaviour" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You really don't need to request changes when some requested changes are already in action you know that right 😭

does this satisfy you?

Copy link
Contributor

@bilhox bilhox left a comment

Choose a reason for hiding this comment

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

LGTM !

@damusss
Copy link
Contributor Author

damusss commented Oct 17, 2024

btw I really suggest squash and merge, 32 commits are not worth this change!!

@bilhox
Copy link
Contributor

bilhox commented Oct 17, 2024

except myremylar, we're only 2 who contributed to this PR, so I'm waiting tomorrow before merging it.

@bilhox bilhox merged commit 120675a into pygame-community:main Oct 18, 2024
28 checks passed
@damusss damusss deleted the surface-scroll-repeat branch October 18, 2024 13:21
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 Surface pygame.Surface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants