diff --git a/buildconfig/Setup.Emscripten.SDL2.in b/buildconfig/Setup.Emscripten.SDL2.in index 9c465102f1..74913d3f49 100644 --- a/buildconfig/Setup.Emscripten.SDL2.in +++ b/buildconfig/Setup.Emscripten.SDL2.in @@ -33,14 +33,13 @@ math src_c/math.c $(SDL) $(DEBUG) GFX = src_c/SDL_gfx/SDL_gfxPrimitives.c -static src_c/static.c $(SDL) $(FREETYPE) $(FONT) $(MIXER) $(IMAGE) $(PNG) $(JPEG) $(DEBUG) +static src_c/base.c $(SDL) $(FREETYPE) $(FONT) $(MIXER) $(IMAGE) $(PNG) $(JPEG) $(DEBUG) -# these should not be altered they already are in static.c merging file above +# these should not be altered they already are in base.c merging file above time src_c/void.c _freetype src_c/void.c imageext src_c/void.c image src_c/void.c -base src_c/void.c bufferproxy src_c/void.c color src_c/void.c controller src_c/void.c diff --git a/src_c/base.c b/src_c/base.c index 4d3ac2a906..61fcd69dc2 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -19,61 +19,24 @@ Pete Shinners pete@shinners.org */ -#define NO_PYGAME_C_API +#if defined(BUILD_STATIC) +#define CONTROLLER_NOPYX +#define PYGAMEAPI_RECT_INTERNAL +#define PYGAMEAPI_EVENT_INTERNAL +#define PYGAMEAPI_JOYSTICK_INTERNAL #define PYGAMEAPI_BASE_INTERNAL - -#include "pygame.h" - -#include -#include "doc/pygame_doc.h" -#include "pgarrinter.h" -#include "pgcompat.h" - -/* This file controls all the initialization of - * the module and the various SDL subsystems - */ - -/*platform specific init stuff*/ - -#ifdef MS_WIN32 /*python gives us MS_WIN32*/ -#define WIN32_LEAN_AND_MEAN -#define VC_EXTRALEAN -#include -extern int -SDL_RegisterApp(const char *, Uint32, void *); -#endif - -#if defined(macintosh) -#if (!defined(__MWERKS__) && !TARGET_API_MAC_CARBON) -QDGlobals pg_qd; -#endif -#endif - -#if SDL_BYTEORDER == SDL_LIL_ENDIAN -#define PAI_MY_ENDIAN '<' -#define PAI_OTHER_ENDIAN '>' -#define BUF_OTHER_ENDIAN '>' -#else -#define PAI_MY_ENDIAN '>' -#define PAI_OTHER_ENDIAN '<' -#define BUF_OTHER_ENDIAN '<' +#define PYGAMEAPI_SURFACE_INTERNAL +#define PYGAMEAPI_BUFFERPROXY_INTERNAL +#define PYGAMEAPI_WINDOW_INTERNAL +#define PYGAMEAPI_RENDER_INTERNAL #endif -#define BUF_MY_ENDIAN '=' -/* Extended array struct */ -typedef struct pg_capsule_interface_s { - PyArrayInterface inter; - Py_intptr_t imem[1]; -} pgCapsuleInterface; +#include "base.h" -/* Py_buffer internal data for an array interface/struct */ -typedef struct pg_view_internals_s { - char format[4]; /* make 4 byte word sized */ - Py_ssize_t imem[1]; -} pgViewInternals; +PG_PixelFormatEnum pg_default_convert_format = 0; /* Custom exceptions */ -static PyObject *pgExc_BufferError = NULL; +PyObject *pgExc_BufferError = NULL; /* Only one instance of the state per process. */ static PyObject *pg_quit_functions = NULL; @@ -83,101 +46,8 @@ SDL_Window *pg_default_window = NULL; pgSurfaceObject *pg_default_screen = NULL; static int pg_env_blend_alpha_SDL2 = 0; -static void -pg_install_parachute(void); -static void -pg_uninstall_parachute(void); -static void -pg_atexit_quit(void); -static int -pgGetArrayStruct(PyObject *, PyObject **, PyArrayInterface **); -static PyObject * -pgArrayStruct_AsDict(PyArrayInterface *); -static PyObject * -pgBuffer_AsArrayInterface(Py_buffer *); -static PyObject * -pgBuffer_AsArrayStruct(Py_buffer *); -static int -_pg_buffer_is_byteswapped(Py_buffer *); -static void -pgBuffer_Release(pg_buffer *); -static int -pgObject_GetBuffer(PyObject *, pg_buffer *, int); -static inline PyObject * -pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, - char *); -static int -pgGetArrayInterface(PyObject **, PyObject *); -static int -pgArrayStruct_AsBuffer(pg_buffer *, PyObject *, PyArrayInterface *, int); -static int -_pg_arraystruct_as_buffer(Py_buffer *, PyObject *, PyArrayInterface *, int); -static int -_pg_arraystruct_to_format(char *, PyArrayInterface *, int); -static int -pgDict_AsBuffer(pg_buffer *, PyObject *, int); -static int -_pg_shape_check(PyObject *); -static int -_pg_typestr_check(PyObject *); -static int -_pg_strides_check(PyObject *); -static int -_pg_data_check(PyObject *); -static int -_pg_is_int_tuple(PyObject *); -static int -_pg_values_as_buffer(Py_buffer *, int, PyObject *, PyObject *, PyObject *, - PyObject *); -static int -_pg_int_tuple_as_ssize_arr(PyObject *, Py_ssize_t *); -static int -_pg_typestr_as_format(PyObject *, char *, Py_ssize_t *); -static PyObject * -pg_view_get_typestr_obj(Py_buffer *); -static PyObject * -pg_view_get_shape_obj(Py_buffer *); -static PyObject * -pg_view_get_strides_obj(Py_buffer *); -static PyObject * -pg_view_get_data_obj(Py_buffer *); -static char -_pg_as_arrayinter_typekind(Py_buffer *); -static char -_pg_as_arrayinter_byteorder(Py_buffer *); -static int -_pg_as_arrayinter_flags(Py_buffer *); -static pgCapsuleInterface * -_pg_new_capsuleinterface(Py_buffer *); -static void -_pg_capsule_PyMem_Free(PyObject *); -static PyObject * -_pg_shape_as_tuple(PyArrayInterface *); -static PyObject * -_pg_typekind_as_str(PyArrayInterface *); -static PyObject * -_pg_strides_as_tuple(PyArrayInterface *); -static PyObject * -_pg_data_as_tuple(PyArrayInterface *); -static PyObject * -pg_get_array_interface(PyObject *, PyObject *); -static void -_pg_release_buffer_array(Py_buffer *); -static void -_pg_release_buffer_generic(Py_buffer *); -static SDL_Window * -pg_GetDefaultWindow(void); -static void -pg_SetDefaultWindow(SDL_Window *); -static pgSurfaceObject * -pg_GetDefaultWindowSurface(void); -static void -pg_SetDefaultWindowSurface(pgSurfaceObject *); -static int -pg_EnvShouldBlendAlphaSDL2(void); - /* compare compiled to linked, raise python error on incompatibility */ -static int +int pg_CheckSDLVersions(void) { #if SDL_VERSION_ATLEAST(3, 0, 0) @@ -246,7 +116,7 @@ pg_RegisterQuit(void (*func)(void)) } } -static PyObject * +PyObject * pg_register_quit(PyObject *self, PyObject *value) { if (!pg_quit_functions) { @@ -263,7 +133,7 @@ pg_register_quit(PyObject *self, PyObject *value) } /* init pygame modules, returns 1 if successful, 0 if fail, with PyErr set*/ -static int +int pg_mod_autoinit(const char *modname) { PyObject *module, *funcobj, *temp; @@ -296,7 +166,7 @@ pg_mod_autoinit(const char *modname) } /* try to quit pygame modules, errors silenced */ -static void +void pg_mod_autoquit(const char *modname) { PyObject *module, *funcobj, *temp; @@ -333,7 +203,7 @@ pg_mod_autoquit(const char *modname) Py_XDECREF(funcobj); } -static PyObject * +PyObject * pg_init(PyObject *self, PyObject *_null) { int i = 0, success = 0, fail = 0; @@ -374,7 +244,7 @@ pg_init(PyObject *self, PyObject *_null) return pg_tuple_couple_from_values_int(success, fail); } -static void +void pg_atexit_quit(void) { /* Maybe it is safe to call SDL_quit more than once after an SDL_Init, @@ -387,7 +257,7 @@ pg_atexit_quit(void) } } -static PyObject * +PyObject * pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs) { int linked = 1; /* Default is linked version. */ @@ -417,13 +287,13 @@ pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs) PG_FIND_VNUM_MICRO(version)); } -static PyObject * +PyObject * pg_get_sdl_byteorder(PyObject *self, PyObject *_null) { return PyLong_FromLong(SDL_BYTEORDER); } -static void +void _pg_quit(void) { Py_ssize_t num, i; @@ -491,21 +361,21 @@ _pg_quit(void) Py_END_ALLOW_THREADS; } -static PyObject * +PyObject * pg_quit(PyObject *self, PyObject *_null) { _pg_quit(); Py_RETURN_NONE; } -static PyObject * +PyObject * pg_base_get_init(PyObject *self, PyObject *_null) { return PyBool_FromLong(pg_is_init); } /* internal C API utility functions */ -static int +int pg_IntFromObj(PyObject *obj, int *val) { if (PyFloat_Check(obj)) { @@ -524,7 +394,7 @@ pg_IntFromObj(PyObject *obj, int *val) return 1; } -static int +int pg_IntFromObjIndex(PyObject *obj, int _index, int *val) { int result = 0; @@ -539,7 +409,7 @@ pg_IntFromObjIndex(PyObject *obj, int _index, int *val) return result; } -static int +int pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2) { // First, lets check the size. This returns -1 if invalid and may set an @@ -602,7 +472,7 @@ pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2) return 1; } -static int +int pg_FloatFromObj(PyObject *obj, float *val) { if (PyFloat_Check(obj)) { @@ -618,7 +488,7 @@ pg_FloatFromObj(PyObject *obj, float *val) return 1; } -static int +int pg_FloatFromObjIndex(PyObject *obj, int _index, float *val) { int result = 0; @@ -633,7 +503,7 @@ pg_FloatFromObjIndex(PyObject *obj, int _index, float *val) return result; } -static int +int pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2) { // First, lets check the size. This returns -1 if invalid and may set an @@ -696,106 +566,7 @@ pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2) return 1; } -static inline int -pg_DoubleFromObj(PyObject *obj, double *val) -{ - if (PyFloat_Check(obj)) { - *val = PyFloat_AS_DOUBLE(obj); - return 1; - } - - *val = (double)PyLong_AsLong(obj); - if (PyErr_Occurred()) { - PyErr_Clear(); - return 0; - } - - return 1; -} - -/*Assumes obj is a Sequence, internal or conscious use only*/ -static inline int -_pg_DoubleFromObjIndex(PyObject *obj, int index, double *val) -{ - int result = 0; - - PyObject *item = PySequence_ITEM(obj, index); - if (!item) { - PyErr_Clear(); - return 0; - } - result = pg_DoubleFromObj(item, val); - Py_DECREF(item); - - return result; -} - -static inline int -pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2) -{ - Py_ssize_t length; - /*Faster path for tuples and lists*/ - if (pgSequenceFast_Check(obj)) { - length = PySequence_Fast_GET_SIZE(obj); - PyObject **f_arr = PySequence_Fast_ITEMS(obj); - if (length == 2) { - if (!pg_DoubleFromObj(f_arr[0], val1) || - !pg_DoubleFromObj(f_arr[1], val2)) { - return 0; - } - } - else if (length == 1) { - /* Handle case of ((x, y), ) 'nested sequence' */ - return pg_TwoDoublesFromObj(f_arr[0], val1, val2); - } - else { - return 0; - } - } - else if (PySequence_Check(obj)) { - length = PySequence_Length(obj); - if (length == 2) { - if (!_pg_DoubleFromObjIndex(obj, 0, val1)) { - return 0; - } - if (!_pg_DoubleFromObjIndex(obj, 1, val2)) { - return 0; - } - } - else if (length == 1 && !PyUnicode_Check(obj)) { - /* Handle case of ((x, y), ) 'nested sequence' */ - PyObject *tmp = PySequence_ITEM(obj, 0); - int ret = pg_TwoDoublesFromObj(tmp, val1, val2); - Py_DECREF(tmp); - return ret; - } - else { - PyErr_Clear(); - return 0; - } - } - else { - return 0; - } - - return 1; -} - -static inline int -pg_TwoDoublesFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs, - double *val1, double *val2) -{ - if (nargs == 1 && pg_TwoDoublesFromObj(args[0], val1, val2)) { - return 1; - } - else if (nargs == 2 && pg_DoubleFromObj(args[0], val1) && - pg_DoubleFromObj(args[1], val2)) { - return 1; - } - return 0; -} - -static int +int pg_UintFromObj(PyObject *obj, Uint32 *val) { if (PyNumber_Check(obj)) { @@ -816,7 +587,7 @@ pg_UintFromObj(PyObject *obj, Uint32 *val) return 0; } -static int +int pg_UintFromObjIndex(PyObject *obj, int _index, Uint32 *val) { int result = 0; @@ -832,7 +603,7 @@ pg_UintFromObjIndex(PyObject *obj, int _index, Uint32 *val) } /* You probably want to use the pg_RGBAFromObjEx function instead of this. */ -static int +int pg_RGBAFromObj(PyObject *obj, Uint8 *RGBA) { Py_ssize_t length; @@ -872,13 +643,13 @@ pg_RGBAFromObj(PyObject *obj, Uint8 *RGBA) return 1; } -static PyObject * +PyObject * pg_get_error(PyObject *self, PyObject *_null) { return PyUnicode_FromString(SDL_GetError()); } -static PyObject * +PyObject * pg_set_error(PyObject *s, PyObject *args) { char *errstring = NULL; @@ -900,7 +671,7 @@ pg_set_error(PyObject *s, PyObject *args) /*array interface*/ -static int +int pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) { PyObject *cobj = PyObject_GetAttrString(obj, "__array_struct__"); @@ -929,7 +700,7 @@ pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) return 0; } -static PyObject * +PyObject * pgArrayStruct_AsDict(PyArrayInterface *inter_p) { PyObject *dictobj = Py_BuildValue("{sisNsNsNsN}", "version", (int)3, @@ -957,7 +728,7 @@ pgArrayStruct_AsDict(PyArrayInterface *inter_p) return dictobj; } -static PyObject * +PyObject * pgBuffer_AsArrayInterface(Py_buffer *view_p) { return Py_BuildValue("{sisNsNsNsN}", "version", (int)3, "typestr", @@ -967,7 +738,7 @@ pgBuffer_AsArrayInterface(Py_buffer *view_p) pg_view_get_data_obj(view_p)); } -static PyObject * +PyObject * pgBuffer_AsArrayStruct(Py_buffer *view_p) { void *cinter_p = _pg_new_capsuleinterface(view_p); @@ -984,7 +755,7 @@ pgBuffer_AsArrayStruct(Py_buffer *view_p) return capsule; } -static pgCapsuleInterface * +pgCapsuleInterface * _pg_new_capsuleinterface(Py_buffer *view_p) { int ndim = view_p->ndim; @@ -1021,13 +792,13 @@ _pg_new_capsuleinterface(Py_buffer *view_p) return cinter_p; } -static void +void _pg_capsule_PyMem_Free(PyObject *capsule) { PyMem_Free(PyCapsule_GetPointer(capsule, 0)); } -static int +int _pg_as_arrayinter_flags(Py_buffer *view_p) { int inter_flags = PAI_ALIGNED; /* atomic int types always aligned */ @@ -1045,7 +816,7 @@ _pg_as_arrayinter_flags(Py_buffer *view_p) return inter_flags; } -static PyObject * +PyObject * pg_view_get_typestr_obj(Py_buffer *view) { return PyUnicode_FromFormat("%c%c%i", _pg_as_arrayinter_byteorder(view), @@ -1053,7 +824,7 @@ pg_view_get_typestr_obj(Py_buffer *view) (int)view->itemsize); } -static PyObject * +PyObject * pg_view_get_shape_obj(Py_buffer *view) { PyObject *shapeobj = PyTuple_New(view->ndim); @@ -1074,7 +845,7 @@ pg_view_get_shape_obj(Py_buffer *view) return shapeobj; } -static PyObject * +PyObject * pg_view_get_strides_obj(Py_buffer *view) { PyObject *shapeobj = PyTuple_New(view->ndim); @@ -1095,14 +866,14 @@ pg_view_get_strides_obj(Py_buffer *view) return shapeobj; } -static PyObject * +PyObject * pg_view_get_data_obj(Py_buffer *view) { return Py_BuildValue("NN", PyLong_FromVoidPtr(view->buf), PyBool_FromLong((long)view->readonly)); } -static char +char _pg_as_arrayinter_typekind(Py_buffer *view) { char type = view->format ? view->format[0] : 'B'; @@ -1142,7 +913,7 @@ _pg_as_arrayinter_typekind(Py_buffer *view) return typekind; } -static char +char _pg_as_arrayinter_byteorder(Py_buffer *view) { char format_0 = view->format ? view->format[0] : 'B'; @@ -1174,7 +945,7 @@ _pg_as_arrayinter_byteorder(Py_buffer *view) return byteorder; } -static PyObject * +PyObject * _pg_shape_as_tuple(PyArrayInterface *inter_p) { PyObject *shapeobj = PyTuple_New((Py_ssize_t)inter_p->nd); @@ -1195,7 +966,7 @@ _pg_shape_as_tuple(PyArrayInterface *inter_p) return shapeobj; } -static PyObject * +PyObject * _pg_typekind_as_str(PyArrayInterface *inter_p) { return PyUnicode_FromFormat( @@ -1207,7 +978,7 @@ _pg_typekind_as_str(PyArrayInterface *inter_p) inter_p->typekind, inter_p->itemsize); } -static PyObject * +PyObject * _pg_strides_as_tuple(PyArrayInterface *inter_p) { PyObject *stridesobj = PyTuple_New((Py_ssize_t)inter_p->nd); @@ -1228,7 +999,7 @@ _pg_strides_as_tuple(PyArrayInterface *inter_p) return stridesobj; } -static PyObject * +PyObject * _pg_data_as_tuple(PyArrayInterface *inter_p) { long readonly = (inter_p->flags & PAI_WRITEABLE) == 0; @@ -1237,7 +1008,7 @@ _pg_data_as_tuple(PyArrayInterface *inter_p) PyBool_FromLong(readonly)); } -static PyObject * +PyObject * pg_get_array_interface(PyObject *self, PyObject *arg) { PyObject *cobj; @@ -1252,7 +1023,7 @@ pg_get_array_interface(PyObject *self, PyObject *arg) return dictobj; } -static int +int pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) { Py_buffer *view_p = (Py_buffer *)pg_view_p; @@ -1385,33 +1156,7 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) return 0; } -static inline PyObject * -pgObject_getRectHelper(PyObject *rect, PyObject *const *args, Py_ssize_t nargs, - PyObject *kwnames, char *type) -{ - if (nargs > 0) { - Py_DECREF(rect); - return PyErr_Format(PyExc_TypeError, - "get_%s only accepts keyword arguments", type); - } - - if (rect && kwnames) { - Py_ssize_t i, sequence_len; - PyObject **sequence_items; - sequence_items = PySequence_Fast_ITEMS(kwnames); - sequence_len = PyTuple_GET_SIZE(kwnames); - - for (i = 0; i < sequence_len; ++i) { - if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) { - Py_DECREF(rect); - return NULL; - } - } - } - return rect; -} - -static void +void pgBuffer_Release(pg_buffer *pg_view_p) { assert(pg_view_p && pg_view_p->release_buffer); @@ -1423,7 +1168,7 @@ pgBuffer_Release(pg_buffer *pg_view_p) PyErr_Restore(type, value, traceback); } -static void +void _pg_release_buffer_generic(Py_buffer *view_p) { if (view_p->obj) { @@ -1432,7 +1177,7 @@ _pg_release_buffer_generic(Py_buffer *view_p) } } -static void +void _pg_release_buffer_array(Py_buffer *view_p) { /* This is deliberately made safe for use on an uninitialized *view_p */ @@ -1443,7 +1188,7 @@ _pg_release_buffer_array(Py_buffer *view_p) _pg_release_buffer_generic(view_p); } -static int +int _pg_buffer_is_byteswapped(Py_buffer *view) { if (view->format) { @@ -1468,7 +1213,7 @@ _pg_buffer_is_byteswapped(Py_buffer *view) return 0; } -static int +int pgGetArrayInterface(PyObject **dict, PyObject *obj) { PyObject *inter = PyObject_GetAttrString(obj, "__array_interface__"); @@ -1491,7 +1236,7 @@ pgGetArrayInterface(PyObject **dict, PyObject *obj) return 0; } -static int +int pgArrayStruct_AsBuffer(pg_buffer *pg_view_p, PyObject *cobj, PyArrayInterface *inter_p, int flags) { @@ -1504,7 +1249,7 @@ pgArrayStruct_AsBuffer(pg_buffer *pg_view_p, PyObject *cobj, return 0; } -static int +int _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, PyArrayInterface *inter_p, int flags) { @@ -1599,7 +1344,7 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, return 0; } -static int +int _pg_arraystruct_to_format(char *format, PyArrayInterface *inter_p, int max_format_len) { @@ -1724,7 +1469,7 @@ _pg_arraystruct_to_format(char *format, PyArrayInterface *inter_p, return 0; } -static int +int pgDict_AsBuffer(pg_buffer *pg_view_p, PyObject *dict, int flags) { PyObject *shape = PyDict_GetItemString(dict, "shape"); @@ -1753,7 +1498,7 @@ pgDict_AsBuffer(pg_buffer *pg_view_p, PyObject *dict, int flags) return 0; } -static int +int _pg_shape_check(PyObject *op) { if (!op) { @@ -1773,7 +1518,7 @@ _pg_shape_check(PyObject *op) return 0; } -static int +int _pg_typestr_check(PyObject *op) { if (!op) { @@ -1803,7 +1548,7 @@ _pg_typestr_check(PyObject *op) return 0; } -static int +int _pg_data_check(PyObject *op) { PyObject *item; @@ -1830,7 +1575,7 @@ _pg_data_check(PyObject *op) return 0; } -static int +int _pg_strides_check(PyObject *op) { if (op && !_pg_is_int_tuple(op) /* Conditional && */) { @@ -1841,7 +1586,7 @@ _pg_strides_check(PyObject *op) return 0; } -static int +int _pg_is_int_tuple(PyObject *op) { Py_ssize_t i; @@ -1861,7 +1606,7 @@ _pg_is_int_tuple(PyObject *op) return 1; } -static int +int _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, PyObject *shape, PyObject *data, PyObject *strides) { @@ -1976,7 +1721,7 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, return 0; } -static int +int _pg_int_tuple_as_ssize_arr(PyObject *tp, Py_ssize_t *arr) { Py_ssize_t i; @@ -1991,7 +1736,7 @@ _pg_int_tuple_as_ssize_arr(PyObject *tp, Py_ssize_t *arr) return 0; } -static int +int _pg_typestr_as_format(PyObject *sp, char *format, Py_ssize_t *itemsize_p) { const char *typestr; @@ -2167,13 +1912,13 @@ _pg_typestr_as_format(PyObject *sp, char *format, Py_ssize_t *itemsize_p) } /*Default window(display)*/ -static SDL_Window * +SDL_Window * pg_GetDefaultWindow(void) { return pg_default_window; } -static void +void pg_SetDefaultWindow(SDL_Window *win) { /*Allows a window to be replaced by itself*/ @@ -2186,14 +1931,14 @@ pg_SetDefaultWindow(SDL_Window *win) pg_default_window = win; } -static pgSurfaceObject * +pgSurfaceObject * pg_GetDefaultWindowSurface(void) { /* return a borrowed reference*/ return pg_default_screen; } -static void +void pg_SetDefaultWindowSurface(pgSurfaceObject *screen) { /*a screen surface can be replaced with itself*/ @@ -2205,9 +1950,7 @@ pg_SetDefaultWindowSurface(pgSurfaceObject *screen) pg_default_screen = screen; } -PG_PixelFormatEnum pg_default_convert_format = 0; - -static PG_PixelFormatEnum +PG_PixelFormatEnum pg_GetDefaultConvertFormat(void) { if (pg_default_screen) { @@ -2216,20 +1959,20 @@ pg_GetDefaultConvertFormat(void) return pg_default_convert_format; } -static void +void pg_SetDefaultConvertFormat(PG_PixelFormatEnum format) { pg_default_convert_format = format; } -static int +int pg_EnvShouldBlendAlphaSDL2(void) { return pg_env_blend_alpha_SDL2; } /*error signal handlers(replacing SDL parachute)*/ -static void +void pygame_parachute(int sig) { #ifdef HAVE_SIGNAL_H @@ -2282,7 +2025,7 @@ static int fatal_signals[] = { }; static int parachute_installed = 0; -static void +void pg_install_parachute(void) { #ifdef HAVE_SIGNAL_H @@ -2306,7 +2049,7 @@ pg_install_parachute(void) return; } -static void +void pg_uninstall_parachute(void) { #ifdef HAVE_SIGNAL_H @@ -2346,13 +2089,6 @@ static PyMethodDef _base_methods[] = { "return an array struct interface as an interface dictionary"}, {NULL, NULL, 0, NULL}}; -#if defined(BUILD_STATIC) && defined(NO_PYGAME_C_API) -// in case of wasm+dynamic loading it could be a trampoline in the globals -// generated at runtime. -// when building static make global accessible symbol directly. -static PyObject *pgExc_SDLError; -#endif - MODINIT_DEFINE(base) { PyObject *module, *apiobj, *atexit; @@ -2504,3 +2240,520 @@ MODINIT_DEFINE(base) Py_XDECREF(module); return NULL; } + +#if defined(__EMSCRIPTEN__) || defined(__wasi__) +#define NO_PYGAME_C_API + +#define CONTROLLER_NOPYX + +#define PYGAMEAPI_RECT_INTERNAL +#define PYGAMEAPI_EVENT_INTERNAL +#define PYGAMEAPI_JOYSTICK_INTERNAL +#define PYGAMEAPI_BASE_INTERNAL +#define PYGAMEAPI_SURFACE_INTERNAL +#define PYGAMEAPI_BUFFERPROXY_INTERNAL +#define PYGAMEAPI_WINDOW_INTERNAL +#define PYGAMEAPI_RENDER_INTERNAL + +#if 0 +#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1) +#define pgSurface_NewNoOwn(surface) \ + (pgSurfaceObject *)pgSurface_New2((surface), 0) +#endif + +#include + +#undef WITH_THREAD + +#if defined(BUILD_STATIC) +#undef import_pygame_base +#undef import_pygame_rect +#undef import_pygame_surface +#undef import_pygame_geometry +#undef import_pygame_color +#undef import_pygame_bufferproxy +#undef import_pygame_rwobject +#undef import_pygame_event + +void +import_pygame_base(void) +{ +} + +void +import_pygame_rect(void) +{ +} + +void +import_pygame_surface(void) +{ +} + +#ifdef import_pygame_window +#undef import_pygame_window +#endif + +void +import_pygame_window(void) +{ +} + +void +import_pygame_geometry(void) +{ +} + +void +import_pygame_color(void) +{ +} + +void +import_pygame_font(void) +{ +} + +void +import_pygame_freetype(void) +{ +} + +void +import_pygame_bufferproxy(void) +{ +} + +void +import_pygame_rwobject(void) +{ +} + +void +import_pygame_event(void) +{ +} + +#ifdef import_pygame_joystick +#undef import_pygame_joystick +#endif + +void +import_pygame_joystick(void) +{ +} + +#undef import_pygame_imageext +#undef import_pygame_render +#undef import_pygame_pixelarray + +void +import_pygame_imageext(void) +{ +} + +void +import_pygame_render(void) +{ +} + +void +import_pygame_pixelarray(void) +{ +} + +PyMODINIT_FUNC +PyInit_base(void); +PyMODINIT_FUNC +PyInit_color(void); +PyMODINIT_FUNC +PyInit_constants(void); +PyMODINIT_FUNC +PyInit_version(void); +PyMODINIT_FUNC +PyInit_rect(void); +PyMODINIT_FUNC +PyInit_geometry(void); +PyMODINIT_FUNC +PyInit_surflock(void); +PyMODINIT_FUNC +PyInit_rwobject(void); +PyMODINIT_FUNC +PyInit_bufferproxy(void); + +PyMODINIT_FUNC +PyInit_surface(void); +PyMODINIT_FUNC +PyInit_display(void); +PyMODINIT_FUNC +PyInit__freetype(void); +PyMODINIT_FUNC +PyInit_font(void); + +PyMODINIT_FUNC +PyInit_draw(void); +PyMODINIT_FUNC +PyInit_mouse(void); +PyMODINIT_FUNC +PyInit_key(void); +PyMODINIT_FUNC +PyInit_event(void); + +PyMODINIT_FUNC +PyInit_joystick(void); + +PyMODINIT_FUNC +PyInit_imageext(void); + +PyMODINIT_FUNC +PyInit_image(void); + +PyMODINIT_FUNC +PyInit_mask(void); + +PyMODINIT_FUNC +PyInit_mixer_music(void); + +PyMODINIT_FUNC +PyInit_pg_mixer(void); + +PyMODINIT_FUNC +PyInit_pg_math(void); + +PyMODINIT_FUNC +PyInit_pg_time(void); + +PyMODINIT_FUNC +PyInit_sdl2(void); + +PyMODINIT_FUNC +PyInit_mixer(void); + +PyMODINIT_FUNC +PyInit_system(void); + +#if defined(CONTROLLER_NOPYX) +PyMODINIT_FUNC +PyInit_controller(void); +#else +PyMODINIT_FUNC +PyInit_controller_old(void); +#endif + +PyMODINIT_FUNC +PyInit_transform(void); + +PyMODINIT_FUNC +PyInit_video(void); + +PyMODINIT_FUNC +PyInit__sprite(void); + +PyMODINIT_FUNC +PyInit_pixelcopy(void); + +PyMODINIT_FUNC +PyInit_newbuffer(void); + +PyMODINIT_FUNC +PyInit_gfxdraw(void); + +PyMODINIT_FUNC +PyInit_audio(void); + +PyMODINIT_FUNC +PyInit_pixelarray(void); + +PyMODINIT_FUNC +PyInit_window(void); + +PyMODINIT_FUNC +PyInit__render(void); + +// pygame_static module + +void +load_submodule(const char *parent, PyObject *mod, const char *alias) +{ + char fqn[1024]; + if (!mod) { + snprintf(fqn, sizeof(fqn), "ERROR: PyInit_%s failed for %s.%s", alias, + parent, alias); + puts(fqn); + PyErr_Print(); + PyErr_Clear(); + } + else { + snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias); + PyObject *modules = PyImport_GetModuleDict(); + + PyObject *pmod = PyDict_GetItemString(modules, parent); + if (!pmod) { + snprintf(fqn, sizeof(fqn), "ERROR: Parent %s not found for %s.%s", + parent, parent, alias); + puts(fqn); + } + else { + PyDict_SetItemString(modules, fqn, mod); + PyDict_SetItemString(PyModule_GetDict(mod), "__name__", + PyUnicode_FromString(fqn)); + PyModule_AddObjectRef(pmod, alias, mod); + Py_XDECREF(mod); + } + } +} + +void +load_submodule_mphase(const char *parent, PyObject *mdef, PyObject *spec, + const char *alias) +{ + char fqn[1024]; + snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias); + + PyObject *modules = PyImport_GetModuleDict(); + + Py_DECREF(PyObject_GetAttrString(spec, "name")); + PyObject_SetAttrString(spec, "name", PyUnicode_FromString(alias)); + PyObject *pmod = PyDict_GetItemString(modules, parent); + PyObject *mod = PyModule_FromDefAndSpec((PyModuleDef *)mdef, spec); + PyDict_SetItemString(PyModule_GetDict(mod), "__package__", + PyUnicode_FromString(parent)); + // TODO SET PACKAGE + PyModule_ExecDef(mod, (PyModuleDef *)mdef); + + if (pmod) { + PyDict_SetItemString(modules, fqn, mod); + PyDict_SetItemString(PyModule_GetDict(mod), "__name__", + PyUnicode_FromString(fqn)); + PyModule_AddObjectRef(pmod, alias, mod); + Py_XDECREF(mod); + } + if (!pmod || PyErr_Occurred()) { + snprintf(fqn, sizeof(fqn), "Error after init in : %s.%s\n", parent, + alias); + fputs(fqn, stderr); + PyErr_Print(); + PyErr_Clear(); + } +} + +static PyObject * +mod_pygame_import_cython(PyObject *self, PyObject *spec) +{ + load_submodule_mphase("pygame._sdl2", PyInit_sdl2(), spec, "sdl2"); + + load_submodule_mphase("pygame._sdl2", PyInit_mixer(), spec, "mixer"); +#if defined(CONTROLLER_NOPYX) + load_submodule("pygame._sdl2", PyInit_controller(), "controller"); +#else + load_submodule_mphase("pygame._sdl2", PyInit_controller_old(), spec, + "controller_old"); +#endif + load_submodule_mphase("pygame._sdl2", PyInit_audio(), spec, "audio"); + load_submodule_mphase("pygame._sdl2", PyInit_video(), spec, "video"); + + Py_RETURN_NONE; +} + +static PyMethodDef mod_pygame_static_methods[] = { + {"import_cython", (PyCFunction)mod_pygame_import_cython, METH_O, + "pygame._sdl2.sdl2"}, + {NULL, NULL, 0, NULL}}; + +static struct PyModuleDef mod_pygame_static = {PyModuleDef_HEAD_INIT, + "pygame_static", NULL, -1, + mod_pygame_static_methods}; + +PyMODINIT_FUNC +PyInit_pygame_static() +{ + // cannot fail here, and font_initialized is already set to 1 in font.c . + TTF_Init(); + + // for correct input in wasm worker + SDL_SetHint("SDL_EMSCRIPTEN_KEYBOARD_ELEMENT", "1"); + + // base module is including current file + // all globals are accessible from here. + load_submodule("pygame", PyInit_base(), "base"); + + load_submodule("pygame", PyInit_constants(), "constants"); + // + load_submodule("pygame", PyInit_pg_math(), "math"); + + // base, pygame.colordict + load_submodule("pygame", PyInit_color(), "color"); + + // base + load_submodule("pygame", PyInit_rect(), "rect"); + + // base, rect + load_submodule("pygame", PyInit_geometry(), "geometry"); + + load_submodule("pygame", PyInit_bufferproxy(), "bufferproxy"); + load_submodule("pygame", PyInit_surflock(), "surflock"); + + // base, color, rect, bufferproxy, surflock + load_submodule("pygame", PyInit_surface(), "surface"); + + load_submodule("pygame", PyInit_rwobject(), "rwobject"); + + // base, color, rect, bufferproxy, surflock, surface, rwobject + load_submodule("pygame", PyInit_imageext(), "imageext"); + // base, color, rect, bufferproxy, surflock, surface, rwobject + load_submodule("pygame", PyInit_image(), "image"); + + load_submodule("pygame", PyInit__freetype(), "_freetype"); + load_submodule("pygame", PyInit_font(), "font"); + load_submodule("pygame", PyInit_pixelcopy(), "pixelcopy"); + load_submodule("pygame", PyInit_newbuffer(), "newbuffer"); + + // base + load_submodule("pygame", PyInit_joystick(), "joystick"); + // base, joystick + load_submodule("pygame", PyInit_event(), "event"); + + // base, rect, event + load_submodule("pygame", PyInit_key(), "key"); + // base, event + load_submodule("pygame", PyInit_pg_time(), "time"); + + load_submodule("pygame", PyInit_transform(), "transform"); + load_submodule("pygame", PyInit_draw(), "draw"); + + load_submodule("pygame", PyInit_mask(), "mask"); + load_submodule("pygame", PyInit_mouse(), "mouse"); + + load_submodule("pygame", PyInit_pg_mixer(), "mixer"); + load_submodule("pygame.mixer", PyInit_mixer_music(), "music"); + + // base, color, rect, bufferproxy, surflock, surface + load_submodule("pygame", PyInit_window(), "window"); + + // base, color, rect, surflock, surface, window + load_submodule("pygame", PyInit_display(), "display"); + load_submodule("pygame", PyInit__render(), "_render"); + + load_submodule("pygame", PyInit_pixelarray(), "pixelarray"); + + // base, color, rect, bufferproxy, surflock, surface + load_submodule("pygame", PyInit_gfxdraw(), "gfxdraw"); + + load_submodule("pygame", PyInit_system(), "system"); + + return PyModule_Create(&mod_pygame_static); +} + +#endif // defined(BUILD_STATIC) + +#include "rect.c" +#include "pgcompat_rect.c" + +#undef pgSurface_Lock +#undef pgSurface_Unlock +#undef pgSurface_LockBy +#undef pgSurface_UnlockBy +#undef pgSurface_Prep +#undef pgSurface_Unprep + +#include "surflock.c" + +#undef pgColor_New +#undef pgColor_NewLength +#undef pg_RGBAFromObjEx +#undef pg_MappedColorFromObj +#undef pgColor_Type + +#include "color.c" + +#undef pgBufferProxy_New + +#include "bufferproxy.c" + +#undef pgSurface_Blit +#undef pgSurface_New +#undef pgSurface_Type +#undef pgSurface_SetSurface + +#include "surface.c" +#include "simd_blitters_avx2.c" +#include "simd_blitters_sse2.c" + +#include "window.c" + +#undef pgVidInfo_Type +#undef pgVidInfo_New + +#include "display.c" + +#include "draw.c" + +#undef pg_EncodeString +#undef pg_EncodeFilePath +#undef pgRWops_IsFileObject +#undef pgRWops_GetFileExtension +#undef pgRWops_FromFileObject +#undef pgRWops_FromObject + +#include "rwobject.c" + +#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1) +#include "render.c" +#include "image.c" + +#include "imageext.c" + +#include "mask.c" + +#undef pg_EnableKeyRepeat +#undef pg_GetKeyRepeat +#undef pgEvent_FillUserEvent +#undef pgEvent_Type +#undef pgEvent_New + +#include "joystick.c" + +#include "event.c" + +#include "mouse.c" + +#include "key.c" + +#include "time.c" + +#include "system.c" +#include "geometry.c" + +#include "_freetype.c" +#include "freetype/ft_wrap.c" +#include "freetype/ft_render.c" +#include "freetype/ft_render_cb.c" +#include "freetype/ft_cache.c" +#include "freetype/ft_layout.c" +#include "freetype/ft_unicode.c" + +#include "font.c" + +#include "mixer.c" + +#include "music.c" + +#include "gfxdraw.c" + +#include "alphablit.c" + +#include "surface_fill.c" +#include "pixelarray.c" +#include "pixelcopy.c" +#include "newbuffer.c" + +#include "_sdl2/controller.c" +#include "_sdl2/controller_old.c" +#include "_sdl2/touch.c" +#include "transform.c" +// that remove some warnings +#undef MAX +#undef MIN +#include "scale2x.c" + +#endif diff --git a/src_c/base.h b/src_c/base.h new file mode 100644 index 0000000000..8fb0970eac --- /dev/null +++ b/src_c/base.h @@ -0,0 +1,356 @@ +#ifndef BASE_H +#define BASE_H + +#define NO_PYGAME_C_API +#define PYGAMEAPI_BASE_INTERNAL + +#include "pygame.h" + +#include +#include "doc/pygame_doc.h" +#include "pgarrinter.h" +#include "pgcompat.h" + +/* This file controls all the initialization of + * the module and the various SDL subsystems + */ + +/*platform specific init stuff*/ + +#ifdef MS_WIN32 /*python gives us MS_WIN32*/ +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRALEAN +#include +extern int +SDL_RegisterApp(const char *, Uint32, void *); +#endif + +#if defined(macintosh) +#if (!defined(__MWERKS__) && !TARGET_API_MAC_CARBON) +QDGlobals pg_qd; +#endif +#endif + +#if SDL_BYTEORDER == SDL_LIL_ENDIAN +#define PAI_MY_ENDIAN '<' +#define PAI_OTHER_ENDIAN '>' +#define BUF_OTHER_ENDIAN '>' +#else +#define PAI_MY_ENDIAN '>' +#define PAI_OTHER_ENDIAN '<' +#define BUF_OTHER_ENDIAN '<' +#endif +#define BUF_MY_ENDIAN '=' + +/* Extended array struct */ +typedef struct pg_capsule_interface_s { + PyArrayInterface inter; + Py_intptr_t imem[1]; +} pgCapsuleInterface; + +/* Py_buffer internal data for an array interface/struct */ +typedef struct pg_view_internals_s { + char format[4]; /* make 4 byte word sized */ + Py_ssize_t imem[1]; +} pgViewInternals; + +extern PG_PixelFormatEnum pg_default_convert_format; +extern PyObject *pgExc_BufferError; +extern SDL_Window *pg_default_window; +extern pgSurfaceObject *pg_default_screen; + +void +pg_install_parachute(void); +void +pg_uninstall_parachute(void); +void +pg_atexit_quit(void); +int +pgGetArrayStruct(PyObject *, PyObject **, PyArrayInterface **); +PyObject * +pgArrayStruct_AsDict(PyArrayInterface *); +PyObject * +pgBuffer_AsArrayInterface(Py_buffer *); +PyObject * +pgBuffer_AsArrayStruct(Py_buffer *); +int +_pg_buffer_is_byteswapped(Py_buffer *); +void +pgBuffer_Release(pg_buffer *); +int +pgObject_GetBuffer(PyObject *, pg_buffer *, int); +static inline PyObject * +pgObject_getRectHelper(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, + char *); +int +pgGetArrayInterface(PyObject **, PyObject *); +int +pgArrayStruct_AsBuffer(pg_buffer *, PyObject *, PyArrayInterface *, int); +int +_pg_arraystruct_as_buffer(Py_buffer *, PyObject *, PyArrayInterface *, int); +int +_pg_arraystruct_to_format(char *, PyArrayInterface *, int); +int +pgDict_AsBuffer(pg_buffer *, PyObject *, int); +int +_pg_shape_check(PyObject *); +int +_pg_typestr_check(PyObject *); +int +_pg_strides_check(PyObject *); +int +_pg_data_check(PyObject *); +int +_pg_is_int_tuple(PyObject *); +int +_pg_values_as_buffer(Py_buffer *, int, PyObject *, PyObject *, PyObject *, + PyObject *); +int +_pg_int_tuple_as_ssize_arr(PyObject *, Py_ssize_t *); +int +_pg_typestr_as_format(PyObject *, char *, Py_ssize_t *); +PyObject * +pg_view_get_typestr_obj(Py_buffer *); +PyObject * +pg_view_get_shape_obj(Py_buffer *); +PyObject * +pg_view_get_strides_obj(Py_buffer *); +PyObject * +pg_view_get_data_obj(Py_buffer *); +char +_pg_as_arrayinter_typekind(Py_buffer *); +char +_pg_as_arrayinter_byteorder(Py_buffer *); +int +_pg_as_arrayinter_flags(Py_buffer *); +pgCapsuleInterface * +_pg_new_capsuleinterface(Py_buffer *); +void +_pg_capsule_PyMem_Free(PyObject *); +PyObject * +_pg_shape_as_tuple(PyArrayInterface *); +PyObject * +_pg_typekind_as_str(PyArrayInterface *); +PyObject * +_pg_strides_as_tuple(PyArrayInterface *); +PyObject * +_pg_data_as_tuple(PyArrayInterface *); +PyObject * +pg_get_array_interface(PyObject *, PyObject *); +void +_pg_release_buffer_array(Py_buffer *); +void +_pg_release_buffer_generic(Py_buffer *); +SDL_Window * +pg_GetDefaultWindow(void); +void +pg_SetDefaultWindow(SDL_Window *); +pgSurfaceObject * +pg_GetDefaultWindowSurface(void); +void +pg_SetDefaultWindowSurface(pgSurfaceObject *); +int +pg_EnvShouldBlendAlphaSDL2(void); +int +pg_CheckSDLVersions(void); +void +pg_RegisterQuit(void (*func)(void)); +PyObject * +pg_register_quit(PyObject *self, PyObject *value); +int +pg_mod_autoinit(const char *modname); +void +pg_mod_autoquit(const char *modname); +PyObject * +pg_init(PyObject *self, PyObject *_null); +PyObject * +pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs); +PyObject * +pg_get_sdl_byteorder(PyObject *self, PyObject *_null); +void +_pg_quit(void); +PyObject * +pg_quit(PyObject *self, PyObject *_null); +PyObject * +pg_base_get_init(PyObject *self, PyObject *_null); +/* internal C API utility functions */ +int +pg_IntFromObj(PyObject *obj, int *val); +int +pg_IntFromObjIndex(PyObject *obj, int _index, int *val); +int +pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2); +int +pg_FloatFromObj(PyObject *obj, float *val); +int +pg_FloatFromObjIndex(PyObject *obj, int _index, float *val); +int +pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2); +static inline int +pg_DoubleFromObj(PyObject *obj, double *val); +/*Assumes obj is a Sequence, internal or conscious use only*/ +static inline int +_pg_DoubleFromObjIndex(PyObject *obj, int index, double *val); +static inline int +pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2); +static inline int +pg_TwoDoublesFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs, + double *val1, double *val2); +int +pg_UintFromObj(PyObject *obj, Uint32 *val); +int +pg_UintFromObjIndex(PyObject *obj, int _index, Uint32 *val); +/* You probably want to use the pg_RGBAFromObjEx function instead of this. */ +int +pg_RGBAFromObj(PyObject *obj, Uint8 *RGBA); +PyObject * +pg_get_error(PyObject *self, PyObject *_null); +PyObject * +pg_set_error(PyObject *s, PyObject *args); + +/*error signal handlers(replacing SDL parachute)*/ +void +pygame_parachute(int sig); + +void +pg_SetDefaultConvertFormat(PG_PixelFormatEnum format); + +PG_PixelFormatEnum +pg_GetDefaultConvertFormat(void); + +#if defined(BUILD_STATIC) && defined(NO_PYGAME_C_API) +// in case of wasm+dynamic loading it could be a trampoline in the globals +// generated at runtime. +// when building static make global accessible symbol directly. +extern PyObject *pgExc_SDLError; +#endif + +MODINIT_DEFINE(base); + +/*=======static inline function definitions=======*/ +static inline PyObject * +pgObject_getRectHelper(PyObject *rect, PyObject *const *args, Py_ssize_t nargs, + PyObject *kwnames, char *type) +{ + if (nargs > 0) { + Py_DECREF(rect); + return PyErr_Format(PyExc_TypeError, + "get_%s only accepts keyword arguments", type); + } + + if (rect && kwnames) { + Py_ssize_t i, sequence_len; + PyObject **sequence_items; + sequence_items = PySequence_Fast_ITEMS(kwnames); + sequence_len = PyTuple_GET_SIZE(kwnames); + + for (i = 0; i < sequence_len; ++i) { + if ((PyObject_SetAttr(rect, sequence_items[i], args[i]) == -1)) { + Py_DECREF(rect); + return NULL; + } + } + } + return rect; +} + +static inline int +pg_DoubleFromObj(PyObject *obj, double *val) +{ + if (PyFloat_Check(obj)) { + *val = PyFloat_AS_DOUBLE(obj); + return 1; + } + + *val = (double)PyLong_AsLong(obj); + if (PyErr_Occurred()) { + PyErr_Clear(); + return 0; + } + + return 1; +} + +/*Assumes obj is a Sequence, internal or conscious use only*/ +static inline int +_pg_DoubleFromObjIndex(PyObject *obj, int index, double *val) +{ + int result = 0; + + PyObject *item = PySequence_ITEM(obj, index); + if (!item) { + PyErr_Clear(); + return 0; + } + result = pg_DoubleFromObj(item, val); + Py_DECREF(item); + + return result; +} + +static inline int +pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2) +{ + Py_ssize_t length; + /*Faster path for tuples and lists*/ + if (pgSequenceFast_Check(obj)) { + length = PySequence_Fast_GET_SIZE(obj); + PyObject **f_arr = PySequence_Fast_ITEMS(obj); + if (length == 2) { + if (!pg_DoubleFromObj(f_arr[0], val1) || + !pg_DoubleFromObj(f_arr[1], val2)) { + return 0; + } + } + else if (length == 1) { + /* Handle case of ((x, y), ) 'nested sequence' */ + return pg_TwoDoublesFromObj(f_arr[0], val1, val2); + } + else { + return 0; + } + } + else if (PySequence_Check(obj)) { + length = PySequence_Length(obj); + if (length == 2) { + if (!_pg_DoubleFromObjIndex(obj, 0, val1)) { + return 0; + } + if (!_pg_DoubleFromObjIndex(obj, 1, val2)) { + return 0; + } + } + else if (length == 1 && !PyUnicode_Check(obj)) { + /* Handle case of ((x, y), ) 'nested sequence' */ + PyObject *tmp = PySequence_ITEM(obj, 0); + int ret = pg_TwoDoublesFromObj(tmp, val1, val2); + Py_DECREF(tmp); + return ret; + } + else { + PyErr_Clear(); + return 0; + } + } + else { + return 0; + } + + return 1; +} + +static inline int +pg_TwoDoublesFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs, + double *val1, double *val2) +{ + if (nargs == 1 && pg_TwoDoublesFromObj(args[0], val1, val2)) { + return 1; + } + else if (nargs == 2 && pg_DoubleFromObj(args[0], val1) && + pg_DoubleFromObj(args[1], val2)) { + return 1; + } + return 0; +} + +#endif // #ifndef BASE_H diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index c3bcc5ec55..4ae29da25a 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -208,6 +208,7 @@ typedef struct { #define pgRect_AsRect(x) (((pgRectObject *)x)->r) #define pgFRect_AsRect(x) (((pgFRectObject *)x)->r) + #ifndef PYGAMEAPI_RECT_INTERNAL #define pgRect_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(rect, 0)) diff --git a/src_c/static.c b/src_c/static.c deleted file mode 100644 index d40c30189c..0000000000 --- a/src_c/static.c +++ /dev/null @@ -1,509 +0,0 @@ -#define NO_PYGAME_C_API - -#define CONTROLLER_NOPYX - -#define PYGAMEAPI_RECT_INTERNAL -#define PYGAMEAPI_EVENT_INTERNAL -#define PYGAMEAPI_JOYSTICK_INTERNAL -#define PYGAMEAPI_BASE_INTERNAL -#define PYGAMEAPI_SURFACE_INTERNAL -#define PYGAMEAPI_BUFFERPROXY_INTERNAL -#define PYGAMEAPI_WINDOW_INTERNAL -#define PYGAMEAPI_RENDER_INTERNAL - -#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1) -#define pgSurface_NewNoOwn(surface) \ - (pgSurfaceObject *)pgSurface_New2((surface), 0) - -#include "pygame.h" -#include "Python.h" - -#include - -#if defined(__EMSCRIPTEN__) -#undef WITH_THREAD -#endif - -#if defined(BUILD_STATIC) -#undef import_pygame_base -#undef import_pygame_rect -#undef import_pygame_surface -#undef import_pygame_geometry -#undef import_pygame_color -#undef import_pygame_bufferproxy -#undef import_pygame_rwobject -#undef import_pygame_event - -void -import_pygame_base(void) -{ -} - -void -import_pygame_rect(void) -{ -} - -void -import_pygame_surface(void) -{ -} - -void -import_pygame_window(void) -{ -} - -void -import_pygame_geometry(void) -{ -} - -void -import_pygame_color(void) -{ -} - -void -import_pygame_font(void) -{ -} - -void -import_pygame_freetype(void) -{ -} - -void -import_pygame_bufferproxy(void) -{ -} - -void -import_pygame_rwobject(void) -{ -} - -void -import_pygame_event(void) -{ -} - -void -import_pygame_joystick(void) -{ -} - -#undef import_pygame_imageext -#undef import_pygame_render -#undef import_pygame_pixelarray - -void -import_pygame_imageext(void) -{ -} - -void -import_pygame_render(void) -{ -} - -void -import_pygame_pixelarray(void) -{ -} - -PyMODINIT_FUNC -PyInit_base(void); -PyMODINIT_FUNC -PyInit_color(void); -PyMODINIT_FUNC -PyInit_constants(void); -PyMODINIT_FUNC -PyInit_version(void); -PyMODINIT_FUNC -PyInit_rect(void); -PyMODINIT_FUNC -PyInit_geometry(void); -PyMODINIT_FUNC -PyInit_surflock(void); -PyMODINIT_FUNC -PyInit_rwobject(void); -PyMODINIT_FUNC -PyInit_bufferproxy(void); - -PyMODINIT_FUNC -PyInit_surface(void); -PyMODINIT_FUNC -PyInit_display(void); -PyMODINIT_FUNC -PyInit__freetype(void); -PyMODINIT_FUNC -PyInit_font(void); - -PyMODINIT_FUNC -PyInit_draw(void); -PyMODINIT_FUNC -PyInit_mouse(void); -PyMODINIT_FUNC -PyInit_key(void); -PyMODINIT_FUNC -PyInit_event(void); - -PyMODINIT_FUNC -PyInit_joystick(void); - -PyMODINIT_FUNC -PyInit_imageext(void); - -PyMODINIT_FUNC -PyInit_image(void); - -PyMODINIT_FUNC -PyInit_mask(void); - -PyMODINIT_FUNC -PyInit_mixer_music(void); - -PyMODINIT_FUNC -PyInit_pg_mixer(void); - -PyMODINIT_FUNC -PyInit_pg_math(void); - -PyMODINIT_FUNC -PyInit_pg_time(void); - -PyMODINIT_FUNC -PyInit_sdl2(void); - -PyMODINIT_FUNC -PyInit_mixer(void); - -PyMODINIT_FUNC -PyInit_system(void); - -#if defined(CONTROLLER_NOPYX) -PyMODINIT_FUNC -PyInit_controller(void); -#else -PyMODINIT_FUNC -PyInit_controller_old(void); -#endif - -PyMODINIT_FUNC -PyInit_transform(void); - -PyMODINIT_FUNC -PyInit_video(void); - -PyMODINIT_FUNC -PyInit__sprite(void); - -PyMODINIT_FUNC -PyInit_pixelcopy(void); - -PyMODINIT_FUNC -PyInit_newbuffer(void); - -PyMODINIT_FUNC -PyInit_gfxdraw(void); - -PyMODINIT_FUNC -PyInit_audio(void); - -PyMODINIT_FUNC -PyInit_pixelarray(void); - -PyMODINIT_FUNC -PyInit_window(void); - -PyMODINIT_FUNC -PyInit__render(void); - -// pygame_static module - -void -load_submodule(const char *parent, PyObject *mod, const char *alias) -{ - char fqn[1024]; - if (!mod) { - snprintf(fqn, sizeof(fqn), "ERROR: PyInit_%s failed for %s.%s", alias, - parent, alias); - puts(fqn); - PyErr_Print(); - PyErr_Clear(); - } - else { - snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias); - PyObject *modules = PyImport_GetModuleDict(); - - PyObject *pmod = PyDict_GetItemString(modules, parent); - if (!pmod) { - snprintf(fqn, sizeof(fqn), "ERROR: Parent %s not found for %s.%s", - parent, parent, alias); - puts(fqn); - } - else { - PyDict_SetItemString(modules, fqn, mod); - PyDict_SetItemString(PyModule_GetDict(mod), "__name__", - PyUnicode_FromString(fqn)); - PyModule_AddObjectRef(pmod, alias, mod); - Py_XDECREF(mod); - } - } -} - -void -load_submodule_mphase(const char *parent, PyObject *mdef, PyObject *spec, - const char *alias) -{ - char fqn[1024]; - snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias); - - PyObject *modules = PyImport_GetModuleDict(); - - Py_DECREF(PyObject_GetAttrString(spec, "name")); - PyObject_SetAttrString(spec, "name", PyUnicode_FromString(alias)); - PyObject *pmod = PyDict_GetItemString(modules, parent); - PyObject *mod = PyModule_FromDefAndSpec((PyModuleDef *)mdef, spec); - PyDict_SetItemString(PyModule_GetDict(mod), "__package__", - PyUnicode_FromString(parent)); - // TODO SET PACKAGE - PyModule_ExecDef(mod, (PyModuleDef *)mdef); - - if (pmod) { - PyDict_SetItemString(modules, fqn, mod); - PyDict_SetItemString(PyModule_GetDict(mod), "__name__", - PyUnicode_FromString(fqn)); - PyModule_AddObjectRef(pmod, alias, mod); - Py_XDECREF(mod); - } - if (!pmod || PyErr_Occurred()) { - snprintf(fqn, sizeof(fqn), "Error after init in : %s.%s\n", parent, - alias); - fputs(fqn, stderr); - PyErr_Print(); - PyErr_Clear(); - } -} - -static PyObject * -mod_pygame_import_cython(PyObject *self, PyObject *spec) -{ - load_submodule_mphase("pygame._sdl2", PyInit_sdl2(), spec, "sdl2"); - - load_submodule_mphase("pygame._sdl2", PyInit_mixer(), spec, "mixer"); -#if defined(CONTROLLER_NOPYX) - load_submodule("pygame._sdl2", PyInit_controller(), "controller"); -#else - load_submodule_mphase("pygame._sdl2", PyInit_controller_old(), spec, - "controller_old"); -#endif - load_submodule_mphase("pygame._sdl2", PyInit_audio(), spec, "audio"); - load_submodule_mphase("pygame._sdl2", PyInit_video(), spec, "video"); - - Py_RETURN_NONE; -} - -static PyMethodDef mod_pygame_static_methods[] = { - {"import_cython", (PyCFunction)mod_pygame_import_cython, METH_O, - "pygame._sdl2.sdl2"}, - {NULL, NULL, 0, NULL}}; - -static struct PyModuleDef mod_pygame_static = {PyModuleDef_HEAD_INIT, - "pygame_static", NULL, -1, - mod_pygame_static_methods}; - -PyMODINIT_FUNC -PyInit_pygame_static() -{ - // cannot fail here, and font_initialized is already set to 1 in font.c . - TTF_Init(); - - // for correct input in wasm worker - SDL_SetHint("SDL_EMSCRIPTEN_KEYBOARD_ELEMENT", "1"); - - load_submodule("pygame", PyInit_base(), "base"); - - // - load_submodule("pygame", PyInit_constants(), "constants"); - // - load_submodule("pygame", PyInit_pg_math(), "math"); - - // base, pygame.colordict - load_submodule("pygame", PyInit_color(), "color"); - - // base - load_submodule("pygame", PyInit_rect(), "rect"); - - // base, rect - load_submodule("pygame", PyInit_geometry(), "geometry"); - - load_submodule("pygame", PyInit_bufferproxy(), "bufferproxy"); - load_submodule("pygame", PyInit_surflock(), "surflock"); - - // base, color, rect, bufferproxy, surflock - load_submodule("pygame", PyInit_surface(), "surface"); - - load_submodule("pygame", PyInit_rwobject(), "rwobject"); - - // base, color, rect, bufferproxy, surflock, surface, rwobject - load_submodule("pygame", PyInit_imageext(), "imageext"); - // base, color, rect, bufferproxy, surflock, surface, rwobject - load_submodule("pygame", PyInit_image(), "image"); - - load_submodule("pygame", PyInit__freetype(), "_freetype"); - load_submodule("pygame", PyInit_font(), "font"); - load_submodule("pygame", PyInit_pixelcopy(), "pixelcopy"); - load_submodule("pygame", PyInit_newbuffer(), "newbuffer"); - - // base - load_submodule("pygame", PyInit_joystick(), "joystick"); - // base, joystick - load_submodule("pygame", PyInit_event(), "event"); - - // base, rect, event - load_submodule("pygame", PyInit_key(), "key"); - // base, event - load_submodule("pygame", PyInit_pg_time(), "time"); - - load_submodule("pygame", PyInit_transform(), "transform"); - load_submodule("pygame", PyInit_draw(), "draw"); - - load_submodule("pygame", PyInit_mask(), "mask"); - load_submodule("pygame", PyInit_mouse(), "mouse"); - - load_submodule("pygame", PyInit_pg_mixer(), "mixer"); - load_submodule("pygame.mixer", PyInit_mixer_music(), "music"); - - // base, color, rect, bufferproxy, surflock, surface - load_submodule("pygame", PyInit_window(), "window"); - - // base, color, rect, surflock, surface, window - load_submodule("pygame", PyInit_display(), "display"); - load_submodule("pygame", PyInit__render(), "_render"); - - load_submodule("pygame", PyInit_pixelarray(), "pixelarray"); - - // base, color, rect, bufferproxy, surflock, surface - load_submodule("pygame", PyInit_gfxdraw(), "gfxdraw"); - - load_submodule("pygame", PyInit_system(), "system"); - - return PyModule_Create(&mod_pygame_static); -} - -#endif // defined(BUILD_STATIC) - -#include "base.c" - -#include "rect.c" -#include "pgcompat_rect.c" - -#undef pgSurface_Lock -#undef pgSurface_Unlock -#undef pgSurface_LockBy -#undef pgSurface_UnlockBy -#undef pgSurface_Prep -#undef pgSurface_Unprep - -#include "surflock.c" - -#undef pgColor_New -#undef pgColor_NewLength -#undef pg_RGBAFromObjEx -#undef pg_MappedColorFromObj -#undef pgColor_Type - -#include "color.c" - -#undef pgBufferProxy_New - -#include "bufferproxy.c" - -#undef pgSurface_Blit -#undef pgSurface_New -#undef pgSurface_Type -#undef pgSurface_SetSurface - -#include "surface.c" -#include "simd_blitters_avx2.c" -#include "simd_blitters_sse2.c" - -#include "window.c" - -#undef pgVidInfo_Type -#undef pgVidInfo_New - -#include "display.c" - -#include "draw.c" - -#undef pg_EncodeString -#undef pg_EncodeFilePath -#undef pgRWops_IsFileObject -#undef pgRWops_GetFileExtension -#undef pgRWops_FromFileObject -#undef pgRWops_FromObject - -#include "rwobject.c" - -#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1) -#include "render.c" -#include "image.c" - -#include "imageext.c" - -#include "mask.c" - -#undef pg_EnableKeyRepeat -#undef pg_GetKeyRepeat -#undef pgEvent_FillUserEvent -#undef pgEvent_Type -#undef pgEvent_New - -#include "joystick.c" - -#include "event.c" - -#include "mouse.c" - -#include "key.c" - -#include "time.c" - -#include "system.c" -#include "geometry.c" - -#include "_freetype.c" -#include "freetype/ft_wrap.c" -#include "freetype/ft_render.c" -#include "freetype/ft_render_cb.c" -#include "freetype/ft_cache.c" -#include "freetype/ft_layout.c" -#include "freetype/ft_unicode.c" - -#include "font.c" - -#include "mixer.c" - -#include "music.c" - -#include "gfxdraw.c" - -#include "alphablit.c" - -#include "surface_fill.c" -#include "pixelarray.c" -#include "pixelcopy.c" -#include "newbuffer.c" - -#include "_sdl2/controller.c" -#include "_sdl2/controller_old.c" -#include "_sdl2/touch.c" -#include "transform.c" -// that remove some warnings -#undef MAX -#undef MIN -#include "scale2x.c"