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

Surface properties #2813

Merged
merged 12 commits into from
May 23, 2024
6 changes: 6 additions & 0 deletions buildconfig/stubs/pygame/surface.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ _ViewKind = Literal[

class Surface:
_pixels_address: int
@property
def width(self) -> int: ...
@property
def height(self) -> int: ...
@property
def size(self) -> Tuple[int, int]: ...
@overload
def __init__(
self,
Expand Down
9 changes: 9 additions & 0 deletions docs/reST/ref/surface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
entire Surface. If it is changed, all drawing operations will only effect
the smaller area.

Surfaces contain the following read-only attributes:
Starbuck5 marked this conversation as resolved.
Show resolved Hide resolved

::

width, height
size

.. versionadded:: 2.5.0
oddbookworm marked this conversation as resolved.
Show resolved Hide resolved

.. method:: blit

| :sl:`draw another surface onto this one`
Expand Down
3 changes: 3 additions & 0 deletions src_c/surface.c
Starbuck5 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ _PgSurface_SrcAlpha(SDL_Surface *surf);
static PyGetSetDef surface_getsets[] = {
{"_pixels_address", (getter)surf_get_pixels_address, NULL,
"pixel buffer address (readonly)", NULL},
{"width", (getter)surf_get_width, NULL, NULL, NULL},
{"height", (getter)surf_get_height, NULL, NULL, NULL},
{"size", (getter)surf_get_size, NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL}};

static struct PyMethodDef surface_methods[] = {
Expand Down
12 changes: 12 additions & 0 deletions test/surface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,18 @@ def test_get_width__size_and_height(self):
self.assertEqual(s.get_height(), h)
self.assertEqual(s.get_size(), (w, h))

def test_attributes(self):
"""Test width, height, and size attributes of surface"""
s = pygame.Surface((100, 50))
self.assertEqual(s.width, 100)
self.assertEqual(s.height, 50)
self.assertEqual(s.size, (100, 50))

attrs = ["width", "height", "size"]
for attr in attrs:
with self.assertRaises(AttributeError):
setattr(s, attr, 200)

def test_get_view(self):
"""Ensure a buffer view of the surface's pixels can be retrieved."""
# Check that BufferProxys are returned when array depth is supported,
Expand Down
Loading