Skip to content

Commit 2990746

Browse files
committed
added geometry/circle base
1 parent 23c282e commit 2990746

File tree

12 files changed

+734
-2
lines changed

12 files changed

+734
-2
lines changed

buildconfig/Setup.SDL2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ math src_c/math.c $(SDL) $(DEBUG)
7575
pixelcopy src_c/pixelcopy.c $(SDL) $(DEBUG)
7676
newbuffer src_c/newbuffer.c $(SDL) $(DEBUG)
7777
system src_c/system.c $(SDL) $(DEBUG)
78+
geometry src_c/geometry.c $(SDL) $(DEBUG)
7879
_window src_c/window.c $(SDL) $(DEBUG)

buildconfig/stubs/gen_stubs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"sysfont",
5151
"_debug",
5252
"system",
53+
"geometry",
5354
]
5455

5556
# pygame classes that are autoimported into main namespace are kept in this dict
@@ -68,6 +69,7 @@
6869
"mixer": ["Channel"],
6970
"time": ["Clock"],
7071
"joystick": ["Joystick"],
72+
"geometry": ["Circle"],
7173
}
7274

7375
# pygame modules from which __init__.py does the equivalent of

buildconfig/stubs/pygame/__init__.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,7 @@ from .constants import (
647647
WINDOWSIZECHANGED as WINDOWSIZECHANGED,
648648
WINDOWTAKEFOCUS as WINDOWTAKEFOCUS,
649649
)
650+
from .geometry import (
651+
Circle as Circle,
652+
CircleType as CircleType
653+
)

buildconfig/stubs/pygame/geometry.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import (
2+
Sequence,
3+
overload,
4+
)
5+
6+
class Circle:
7+
x: float
8+
y: float
9+
r: float
10+
11+
@overload
12+
def __init__(self, x: float, y: float, r: float) -> None: ...
13+
@overload
14+
def __init__(self, pos: Sequence[float], r: float) -> None: ...
15+
@overload
16+
def __init__(self, circle) -> None: ...
17+
def __copy__(self) -> "Circle": ...
18+
copy = __copy__
19+
20+
CircleType = Circle

src_c/_pygame.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ struct pgColorObject {
339339
#define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
340340
#define PYGAMEAPI_COLOR_NUMSLOTS 5
341341
#define PYGAMEAPI_MATH_NUMSLOTS 2
342-
#define PYGAMEAPI_BASE_NUMSLOTS 24
342+
#define PYGAMEAPI_BASE_NUMSLOTS 26
343343
#define PYGAMEAPI_EVENT_NUMSLOTS 6
344344
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
345+
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 1
345346

346347
#endif /* _PYGAME_INTERNAL_H */

src_c/base.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,91 @@ pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2)
575575
return 1;
576576
}
577577

578+
static inline int
579+
pg_DoubleFromObj(PyObject *obj, double *val)
580+
{
581+
if (PyFloat_Check(obj)) {
582+
*val = PyFloat_AS_DOUBLE(obj);
583+
return 1;
584+
}
585+
586+
*val = (double)PyLong_AsLong(obj);
587+
if (PyErr_Occurred()) {
588+
PyErr_Clear();
589+
return 0;
590+
}
591+
592+
return 1;
593+
}
594+
595+
/*Assumes obj is a Sequence, internal or conscious use only*/
596+
static inline int
597+
_pg_DoubleFromObjIndex(PyObject *obj, int index, double *val)
598+
{
599+
int result = 0;
600+
601+
PyObject *item = PySequence_ITEM(obj, index);
602+
if (!item) {
603+
PyErr_Clear();
604+
return 0;
605+
}
606+
result = pg_DoubleFromObj(item, val);
607+
Py_DECREF(item);
608+
609+
return result;
610+
}
611+
612+
static inline int
613+
pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2)
614+
{
615+
Py_ssize_t length;
616+
/*Faster path for tuples and lists*/
617+
if (pgSequenceFast_Check(obj)) {
618+
length = PySequence_Fast_GET_SIZE(obj);
619+
PyObject **f_arr = PySequence_Fast_ITEMS(obj);
620+
if (length == 2) {
621+
if (!pg_DoubleFromObj(f_arr[0], val1) ||
622+
!pg_DoubleFromObj(f_arr[1], val2)) {
623+
return 0;
624+
}
625+
}
626+
else if (length == 1) {
627+
/* Handle case of ((x, y), ) 'nested sequence' */
628+
return pg_TwoDoublesFromObj(f_arr[0], val1, val2);
629+
}
630+
else {
631+
return 0;
632+
}
633+
}
634+
else if (PySequence_Check(obj)) {
635+
length = PySequence_Length(obj);
636+
if (length == 2) {
637+
if (!_pg_DoubleFromObjIndex(obj, 0, val1)) {
638+
return 0;
639+
}
640+
if (!_pg_DoubleFromObjIndex(obj, 1, val2)) {
641+
return 0;
642+
}
643+
}
644+
else if (length == 1 && !PyUnicode_Check(obj)) {
645+
/* Handle case of ((x, y), ) 'nested sequence' */
646+
PyObject *tmp = PySequence_ITEM(obj, 0);
647+
int ret = pg_TwoDoublesFromObj(tmp, val1, val2);
648+
Py_DECREF(tmp);
649+
return ret;
650+
}
651+
else {
652+
PyErr_Clear();
653+
return 0;
654+
}
655+
}
656+
else {
657+
return 0;
658+
}
659+
660+
return 1;
661+
}
662+
578663
static int
579664
pg_UintFromObj(PyObject *obj, Uint32 *val)
580665
{
@@ -2170,8 +2255,10 @@ MODINIT_DEFINE(base)
21702255
c_api[21] = pg_GetDefaultWindowSurface;
21712256
c_api[22] = pg_SetDefaultWindowSurface;
21722257
c_api[23] = pg_EnvShouldBlendAlphaSDL2;
2258+
c_api[24] = pg_DoubleFromObj;
2259+
c_api[25] = pg_TwoDoublesFromObj;
21732260

2174-
#define FILLED_SLOTS 24
2261+
#define FILLED_SLOTS 26
21752262

21762263
#if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS
21772264
#error export slot count mismatch

0 commit comments

Comments
 (0)