Skip to content

Commit aff289d

Browse files
authored
Merge pull request #2071 from durkisneer1/durk-getFRect
Create Surface.get_frect method
2 parents 447e0d6 + 386bd0b commit aff289d

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

buildconfig/stubs/pygame/surface.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from typing import Any, List, Optional, Sequence, Tuple, Union, overload, Iterab
22

33
from pygame.bufferproxy import BufferProxy
44
from pygame.color import Color
5-
from pygame.rect import Rect
5+
from pygame.rect import Rect, FRect
66

77
from ._common import ColorValue, Coordinate, Literal, RectValue, RGBAOutput
88

@@ -139,6 +139,7 @@ class Surface:
139139
def get_width(self) -> int: ...
140140
def get_height(self) -> int: ...
141141
def get_rect(self, **kwargs: Any) -> Rect: ...
142+
def get_frect(self, **kwargs: Any) -> FRect: ...
142143
def get_bitsize(self) -> int: ...
143144
def get_bytesize(self) -> int: ...
144145
def get_flags(self) -> int: ...

docs/reST/ref/surface.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,21 @@
709709

710710
.. ## Surface.get_rect ##
711711
712+
.. method:: get_frect
713+
714+
| :sl:`get the rectangular area of the Surface`
715+
| :sg:`get_frect(\**kwargs) -> FRect`
716+
717+
This is the same as :meth:`Surface.get_rect` but returns an FRect. FRect is similar
718+
to Rect, except it stores float values instead.
719+
720+
You can pass keyword argument values to this function. These named values
721+
will be applied to the attributes of the FRect before it is returned. An
722+
example would be ``mysurf.get_frect(center=(100.5, 100.5))`` to create a
723+
rectangle for the Surface centered at a given position.
724+
725+
.. ## Surface.get_frect ##
726+
712727
.. method:: get_bitsize
713728

714729
| :sl:`get the bit depth of the Surface pixel format`

src_c/doc/surface_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define DOC_SURFACE_GETWIDTH "get_width() -> width\nget the width of the Surface"
3838
#define DOC_SURFACE_GETHEIGHT "get_height() -> height\nget the height of the Surface"
3939
#define DOC_SURFACE_GETRECT "get_rect(**kwargs) -> Rect\nget the rectangular area of the Surface"
40+
#define DOC_SURFACE_GETFRECT "get_frect(**kwargs) -> FRect\nget the rectangular area of the Surface"
4041
#define DOC_SURFACE_GETBITSIZE "get_bitsize() -> int\nget the bit depth of the Surface pixel format"
4142
#define DOC_SURFACE_GETBYTESIZE "get_bytesize() -> int\nget the bytes used per Surface pixel"
4243
#define DOC_SURFACE_GETFLAGS "get_flags() -> int\nget the additional flags used for the Surface"

src_c/surface.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ surf_get_pitch(PyObject *self, PyObject *args);
196196
static PyObject *
197197
surf_get_rect(PyObject *self, PyObject *args, PyObject *kwargs);
198198
static PyObject *
199+
surf_get_frect(PyObject *self, PyObject *args, PyObject *kwargs);
200+
static PyObject *
199201
surf_get_width(PyObject *self, PyObject *args);
200202
static PyObject *
201203
surf_get_shifts(PyObject *self, PyObject *args);
@@ -356,6 +358,8 @@ static struct PyMethodDef surface_methods[] = {
356358
{"get_height", surf_get_height, METH_NOARGS, DOC_SURFACE_GETHEIGHT},
357359
{"get_rect", (PyCFunction)surf_get_rect, METH_VARARGS | METH_KEYWORDS,
358360
DOC_SURFACE_GETRECT},
361+
{"get_frect", (PyCFunction)surf_get_frect, METH_VARARGS | METH_KEYWORDS,
362+
DOC_SURFACE_GETFRECT},
359363
{"get_pitch", surf_get_pitch, METH_NOARGS, DOC_SURFACE_GETPITCH},
360364
{"get_bitsize", surf_get_bitsize, METH_NOARGS, DOC_SURFACE_GETBITSIZE},
361365
{"get_bytesize", surf_get_bytesize, METH_NOARGS, DOC_SURFACE_GETBYTESIZE},
@@ -2557,6 +2561,34 @@ surf_get_rect(PyObject *self, PyObject *args, PyObject *kwargs)
25572561
return rect;
25582562
}
25592563

2564+
static PyObject *
2565+
surf_get_frect(PyObject *self, PyObject *args, PyObject *kwargs)
2566+
{
2567+
PyObject *rect;
2568+
SDL_Surface *surf = pgSurface_AsSurface(self);
2569+
2570+
if (PyTuple_GET_SIZE(args) > 0) {
2571+
return RAISE(PyExc_TypeError,
2572+
"get_frect only accepts keyword arguments");
2573+
}
2574+
2575+
SURF_INIT_CHECK(surf)
2576+
2577+
rect = pgFRect_New4(0.f, 0.f, (float)surf->w, (float)surf->h);
2578+
if (rect && kwargs) {
2579+
PyObject *key, *value;
2580+
Py_ssize_t pos = 0;
2581+
2582+
while (PyDict_Next(kwargs, &pos, &key, &value)) {
2583+
if ((PyObject_SetAttr(rect, key, value) == -1)) {
2584+
Py_DECREF(rect);
2585+
return NULL;
2586+
}
2587+
}
2588+
}
2589+
return rect;
2590+
}
2591+
25602592
static PyObject *
25612593
surf_get_bitsize(PyObject *self, PyObject *_null)
25622594
{

test/surface_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,18 @@ def test_get_rect(self):
594594

595595
########################################################################
596596

597+
def test_get_frect(self):
598+
"""Ensure a surface's frect can be retrieved."""
599+
size = (16.0, 16.0)
600+
surf = pygame.Surface(size)
601+
frect = surf.get_frect()
602+
603+
self.assertEqual(frect.topleft, (0.0, 0.0))
604+
self.assertEqual(frect.size, size)
605+
self.assertIsInstance(frect, pygame.FRect)
606+
607+
########################################################################
608+
597609
def test_get_width__size_and_height(self):
598610
"""Ensure a surface's size, width and height can be retrieved."""
599611
for w in range(0, 255, 32):

0 commit comments

Comments
 (0)