From 184e6702d6e3b6fa379d412df80d90aedd6137b8 Mon Sep 17 00:00:00 2001 From: itzpr3d4t0r Date: Mon, 19 Aug 2024 12:36:59 +0200 Subject: [PATCH 1/2] optimized rect clipline --- src_c/rect.c | 2 ++ src_c/rect_impl.h | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src_c/rect.c b/src_c/rect.c index 0cdafa263e..48d793cc4e 100644 --- a/src_c/rect.c +++ b/src_c/rect.c @@ -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 @@ -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 diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 15bd155308..58dbccc29d 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -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 @@ -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 @@ -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 @@ -2964,6 +2989,7 @@ RectExport_iterator(RectObject *self) #undef RectImport_TypeObject #undef RectImport_PrimitiveFromObj #undef RectImport_PyBuildValueFormat +#undef TupleFromTwoPrimitives #undef RectImport_ObjectName #undef PrimitiveType From 85154fb9153add35cdb020cfbc7364270f1318a1 Mon Sep 17 00:00:00 2001 From: itzpr3d4t0r Date: Mon, 19 Aug 2024 17:16:50 +0200 Subject: [PATCH 2/2] lil fix --- src_c/rect_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 58dbccc29d..5fc1dfcfd0 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -2989,7 +2989,7 @@ RectExport_iterator(RectObject *self) #undef RectImport_TypeObject #undef RectImport_PrimitiveFromObj #undef RectImport_PyBuildValueFormat -#undef TupleFromTwoPrimitives +#undef RectImport_TupleFromTwoPrimitives #undef RectImport_ObjectName #undef PrimitiveType