Skip to content

Commit

Permalink
Merge pull request #3067 from itzpr3d4t0r/rect-clipline-opt
Browse files Browse the repository at this point in the history
Optimized `Rect.clipline()`
  • Loading branch information
ankith26 authored Aug 20, 2024
2 parents 4dab9d7 + 85154fb commit 141a75d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src_c/rect.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
#define RectImport_TypeObject pgRect_Type
#define RectImport_IntersectRectAndLine SDL_IntersectRectAndLine
#define RectImport_PyBuildValueFormat "i"
#define RectImport_TupleFromTwoPrimitives pg_tuple_couple_from_values_int
#define RectImport_ObjectName "pygame.rect.Rect"
#define RectImport_PythonNumberCheck PyLong_Check
#define RectImport_PythonNumberAsPrimitiveType PyLong_AsLong
Expand Down Expand Up @@ -268,6 +269,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
#define RectImport_IntersectRectAndLine PG_IntersectFRectAndLine
#define RectImport_TypeObject pgFRect_Type
#define RectImport_PyBuildValueFormat "f"
#define RectImport_TupleFromTwoPrimitives pg_tuple_couple_from_values_double
#define RectImport_ObjectName "pygame.rect.FRect"
#define RectImport_PythonNumberCheck PyFloat_Check
#define RectImport_PythonNumberAsPrimitiveType PyFloat_AsDouble
Expand Down
30 changes: 28 additions & 2 deletions src_c/rect_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@
#ifndef RectImport_PyBuildValueFormat
#error RectImport_PyBuildValueFormat needs to be defined
#endif
#ifndef RectImport_TupleFromTwoPrimitives
#error RectImport_TupleFromTwoPrimitives needs to be defined
#endif
// #endregion

// #region RectOptional
Expand Down Expand Up @@ -399,6 +402,7 @@
#define fourPrimivitesFromObj RectImport_fourPrimiviteFromObj
#define PrimitiveFromObj RectImport_PrimitiveFromObj
#define TypeFMT RectImport_PyBuildValueFormat
#define TupleFromTwoPrimitives RectImport_TupleFromTwoPrimitives
#define ObjectName RectImport_ObjectName
#define PythonNumberCheck RectImport_PythonNumberCheck
#define PythonNumberAsPrimitiveType RectImport_PythonNumberAsPrimitiveType
Expand Down Expand Up @@ -1915,8 +1919,29 @@ RectExport_clipline(RectObject *self, PyObject *const *args, Py_ssize_t nargs)
}

Py_XDECREF(rect_copy);
return Py_BuildValue("((" TypeFMT "" TypeFMT ")(" TypeFMT "" TypeFMT "))",
x1, y1, x2, y2);

PyObject *subtup1, *subtup2;
subtup1 = TupleFromTwoPrimitives(x1, y1);
if (!subtup1)
return NULL;

subtup2 = TupleFromTwoPrimitives(x2, y2);
if (!subtup2) {
Py_DECREF(subtup1);
return NULL;
}

PyObject *tup = PyTuple_New(2);
if (!tup) {
Py_DECREF(subtup1);
Py_DECREF(subtup2);
return NULL;
}

PyTuple_SET_ITEM(tup, 0, subtup1);
PyTuple_SET_ITEM(tup, 1, subtup2);

return tup;
}

static int
Expand Down Expand Up @@ -2964,6 +2989,7 @@ RectExport_iterator(RectObject *self)
#undef RectImport_TypeObject
#undef RectImport_PrimitiveFromObj
#undef RectImport_PyBuildValueFormat
#undef RectImport_TupleFromTwoPrimitives
#undef RectImport_ObjectName

#undef PrimitiveType
Expand Down

0 comments on commit 141a75d

Please sign in to comment.