Skip to content

Commit 8a0744d

Browse files
ScriptLineStudiosoddbookwormStarbuck5
authored
Surface properties (#2813)
* test commit * attributes * forgot stubs * changes as per request * python setup.py docs removed this? * removed w and h * forgot stub removal * Fixed docs for review * Make arrow spacing more consistent in surf docs * removed unnecessary section in docs --------- Co-authored-by: Andrew Coffey <[email protected]> Co-authored-by: Starbuck5 <[email protected]>
1 parent 4c5b9d5 commit 8a0744d

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

buildconfig/stubs/pygame/surface.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ _ViewKind = Literal[
4242

4343
class Surface:
4444
_pixels_address: int
45+
@property
46+
def width(self) -> int: ...
47+
@property
48+
def height(self) -> int: ...
49+
@property
50+
def size(self) -> Tuple[int, int]: ...
4551
@overload
4652
def __init__(
4753
self,

docs/reST/ref/surface.rst

+30-3
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@
719719
| :sl:`get the dimensions of the Surface`
720720
| :sg:`get_size() -> (width, height)`
721721

722-
Return the width and height of the Surface in pixels.
722+
Return the width and height of the Surface in pixels. Can also be accessed with :attr:`size`
723723

724724
.. ## Surface.get_size ##
725725

@@ -728,7 +728,7 @@
728728
| :sl:`get the width of the Surface`
729729
| :sg:`get_width() -> width`
730730

731-
Return the width of the Surface in pixels.
731+
Return the width of the Surface in pixels. Can also be accessed with :attr:`width`
732732

733733
.. ## Surface.get_width ##
734734

@@ -737,7 +737,7 @@
737737
| :sl:`get the height of the Surface`
738738
| :sg:`get_height() -> height`
739739

740-
Return the height of the Surface in pixels.
740+
Return the height of the Surface in pixels. Can also be accessed with :attr:`height`
741741

742742
.. ## Surface.get_height ##
743743

@@ -1030,6 +1030,33 @@
10301030

10311031
.. ## Surface.premul_alpha ##
10321032

1033+
.. attribute:: width
1034+
1035+
| :sl:`Surface width in pixels (read-only)`
1036+
| :sg:`width -> int`
1037+
1038+
Read-only attribute. Same as :meth:`get_width()`
1039+
1040+
.. versionadded:: 2.5.0
1041+
1042+
.. attribute:: height
1043+
1044+
| :sl:`Surface height in pixels (read-only)`
1045+
| :sg:`height -> int`
1046+
1047+
Read-only attribute. Same as :meth:`get_height()`
1048+
1049+
.. versionadded:: 2.5.0
1050+
1051+
.. attribute:: size
1052+
1053+
| :sl:`Surface size in pixels (read-only)`
1054+
| :sg:`height -> tuple[int, int]`
1055+
1056+
Read-only attribute. Same as :meth:`get_size()`
1057+
1058+
.. versionadded:: 2.5.0
1059+
10331060
.. ## pygame.Surface ##
10341061

10351062

src_c/doc/surface_doc.h

+3
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@
5252
#define DOC_SURFACE_GETBUFFER "get_buffer() -> BufferProxy\nacquires a buffer object for the pixels of the Surface."
5353
#define DOC_SURFACE_PIXELSADDRESS "_pixels_address -> int\npixel buffer address"
5454
#define DOC_SURFACE_PREMULALPHA "premul_alpha() -> Surface\nreturns a copy of the surface with the RGB channels pre-multiplied by the alpha channel."
55+
#define DOC_SURFACE_WIDTH "width -> int\nSurface width in pixels (read-only)"
56+
#define DOC_SURFACE_HEIGHT "height -> int\nSurface height in pixels (read-only)"
57+
#define DOC_SURFACE_SIZE "height -> tuple[int, int]\nSurface size in pixels (read-only)"

src_c/surface.c

+3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ _PgSurface_SrcAlpha(SDL_Surface *surf);
266266
static PyGetSetDef surface_getsets[] = {
267267
{"_pixels_address", (getter)surf_get_pixels_address, NULL,
268268
"pixel buffer address (readonly)", NULL},
269+
{"width", (getter)surf_get_width, NULL, DOC_SURFACE_WIDTH, NULL},
270+
{"height", (getter)surf_get_height, NULL, DOC_SURFACE_HEIGHT, NULL},
271+
{"size", (getter)surf_get_size, NULL, DOC_SURFACE_SIZE, NULL},
269272
{NULL, NULL, NULL, NULL, NULL}};
270273

271274
static struct PyMethodDef surface_methods[] = {

test/surface_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,18 @@ def test_get_width__size_and_height(self):
674674
self.assertEqual(s.get_height(), h)
675675
self.assertEqual(s.get_size(), (w, h))
676676

677+
def test_attributes(self):
678+
"""Test width, height, and size attributes of surface"""
679+
s = pygame.Surface((100, 50))
680+
self.assertEqual(s.width, 100)
681+
self.assertEqual(s.height, 50)
682+
self.assertEqual(s.size, (100, 50))
683+
684+
attrs = ["width", "height", "size"]
685+
for attr in attrs:
686+
with self.assertRaises(AttributeError):
687+
setattr(s, attr, 200)
688+
677689
def test_get_view(self):
678690
"""Ensure a buffer view of the surface's pixels can be retrieved."""
679691
# Check that BufferProxys are returned when array depth is supported,

0 commit comments

Comments
 (0)