Skip to content

Commit 120675a

Browse files
damusssMyreMylar
andauthored
Allow erasing pixels in pygame.Surface.scroll and add repeat functionality (#2855)
* Implementation, stubs and docs * Remove spaces added by clang-format * Fix variable could not be initialized * Rewrite tests, fix code, add more exceptions * Fix format 1 * Modify test * Generate docs * Allow non erasing pixels * Fix doc * Fix bug with erase * Remove useless memset * Separate in 2 functions for clarity * Use SDL function to secure the clip * Update docs * build docs * FLAG SYSTEM (I can revert if you don't like it) * FLAG_SYSTEM fix stubs * Update Docs * Update versionchanged Co-authored-by: Dan Lawrence <danintheshed@gmail.com> * FIX THE MISSING PIXEL BUG * Simplify conditional statement * fix format * Split in 2 functions, update versionchanged * Fix docs and remove redundant C code --------- Co-authored-by: Damus666 <97639432+Damus666@users.noreply.github.com> Co-authored-by: Dan Lawrence <danintheshed@gmail.com>
1 parent a5002cf commit 120675a

File tree

10 files changed

+302
-132
lines changed

10 files changed

+302
-132
lines changed

buildconfig/stubs/pygame/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ from .constants import (
605605
SCRAP_PPM as SCRAP_PPM,
606606
SCRAP_SELECTION as SCRAP_SELECTION,
607607
SCRAP_TEXT as SCRAP_TEXT,
608+
SCROLL_ERASE as SCROLL_ERASE,
609+
SCROLL_REPEAT as SCROLL_REPEAT,
608610
SHOWN as SHOWN,
609611
SRCALPHA as SRCALPHA,
610612
SRCCOLORKEY as SRCCOLORKEY,

buildconfig/stubs/pygame/constants.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ SCRAP_PBM: str
528528
SCRAP_PPM: str
529529
SCRAP_SELECTION: int
530530
SCRAP_TEXT: str
531+
SCROLL_ERASE: int
532+
SCROLL_REPEAT: int
531533
SHOWN: int
532534
SRCALPHA: int
533535
SRCCOLORKEY: int

buildconfig/stubs/pygame/locals.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ SCRAP_PBM: str
530530
SCRAP_PPM: str
531531
SCRAP_SELECTION: int
532532
SCRAP_TEXT: str
533+
SCROLL_ERASE: int
534+
SCROLL_REPEAT: int
533535
SHOWN: int
534536
SRCALPHA: int
535537
SRCCOLORKEY: int

buildconfig/stubs/pygame/surface.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class Surface:
103103
rect: Optional[RectLike] = None,
104104
special_flags: int = 0,
105105
) -> Rect: ...
106-
def scroll(self, dx: int = 0, dy: int = 0, /) -> None: ...
106+
def scroll(
107+
self, dx: int = 0, dy: int = 0, scroll_flag: int = 0, /
108+
) -> None: ...
107109
@overload
108110
def set_colorkey(self, color: ColorLike, flags: int = 0, /) -> None: ...
109111
@overload

docs/reST/ref/surface.rst

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,30 @@
316316
317317
.. method:: scroll
318318

319-
| :sl:`shift the surface image in place`
320-
| :sg:`scroll(dx=0, dy=0, /) -> None`
319+
| :sl:`shift the Surface pixels in place`
320+
| :sg:`scroll(dx=0, dy=0, scroll_flag=0, /) -> None`
321321
322-
Move the image by dx pixels right and dy pixels down. dx and dy may be
323-
negative for left and up scrolls respectively. Areas of the surface that
324-
are not overwritten retain their original pixel values. Scrolling is
325-
contained by the Surface clip area. It is safe to have dx and dy values
326-
that exceed the surface size.
322+
Move the Surface by dx pixels right and dy pixels down. dx and dy may be
323+
negative for left and up scrolls respectively.
324+
325+
Scrolling is contained by the Surface clip area. It is safe to have dx
326+
and dy values that exceed the surface size.
327+
328+
The scroll flag can be:
329+
* ``0`` (default): the pixels are shifted but previous pixels are
330+
not modified.
331+
332+
* ``pygame.SCROLL_ERASE``: the space created by the shifting pixels
333+
is filled with black or transparency.
334+
335+
* ``pygame.SCROLL_REPEAT``: the pixels that disappear out of the
336+
surface or clip bounds are brought back on the opposite side
337+
resulting in an infinitely scrolling and repeating surface.
327338

328339
.. versionaddedold:: 1.9
329340

341+
.. versionchanged:: 2.5.3 Add repeating scroll and allow erasing pixels
342+
330343
.. ## Surface.scroll ##
331344
332345
.. method:: set_colorkey

src_c/_pygame.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ typedef enum {
430430
PGS_PREALLOC = 0x01000000
431431
} PygameSurfaceFlags;
432432

433+
typedef enum {
434+
PGS_SCROLL_DEFAULT = 0x00000000,
435+
PGS_SCROLL_REPEAT = 0x00000001,
436+
PGS_SCROLL_ERASE = 0x00000004
437+
} PygameScrollSurfaceFlags;
438+
433439
#define RAISE(x, y) (PyErr_SetString((x), (y)), NULL)
434440
#define RAISERETURN(x, y, r) \
435441
PyErr_SetString((x), (y)); \

src_c/constants.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ MODINIT_DEFINE(constants)
116116
DEC_CONSTSF(SHOWN);
117117
DEC_CONSTSF(HIDDEN);
118118

119+
DEC_CONSTSF(SCROLL_ERASE);
120+
DEC_CONSTSF(SCROLL_REPEAT);
121+
119122
DEC_CONSTSF(SCALED);
120123

121124
DEC_CONST(GL_RED_SIZE);

src_c/doc/surface_doc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define DOC_SURFACE_CONVERTALPHA "convert_alpha() -> Surface\nchange the pixel format of a surface including per pixel alphas"
88
#define DOC_SURFACE_COPY "copy() -> Surface\ncreate a new copy of a Surface"
99
#define DOC_SURFACE_FILL "fill(color, rect=None, special_flags=0) -> Rect\nfill Surface with a solid color"
10-
#define DOC_SURFACE_SCROLL "scroll(dx=0, dy=0, /) -> None\nshift the surface image in place"
10+
#define DOC_SURFACE_SCROLL "scroll(dx=0, dy=0, scroll_flag=0, /) -> None\nshift the Surface pixels in place"
1111
#define DOC_SURFACE_SETCOLORKEY "set_colorkey(color, flags=0, /) -> None\nset_colorkey(None) -> None\nset the transparent colorkey"
1212
#define DOC_SURFACE_GETCOLORKEY "get_colorkey() -> RGBA or None\nget the current transparent colorkey"
1313
#define DOC_SURFACE_SETALPHA "set_alpha(value, flags=0, /) -> None\nset_alpha(None) -> None\nset the alpha value for the full Surface"

0 commit comments

Comments
 (0)