diff --git a/src_c/display.c b/src_c/display.c index f983ab2b2b..1d863867c4 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -2938,7 +2938,7 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) return NULL; } -static PyObject *pg_gl_proc_from_address = NULL; +static PyObject *ctypes_functype = NULL; static PyObject * pg_gl_get_proc(PyObject *self, PyObject *arg) @@ -2958,17 +2958,45 @@ pg_gl_get_proc(PyObject *self, PyObject *arg) #endif proc_addr = SDL_GL_GetProcAddress(proc_name); if (!proc_addr) { - return RAISE(pgExc_SDLError, SDL_GetError()); + PyErr_Format(pgExc_SDLError, "Unable to get OpenGL function '%s'", + proc_name); + return NULL; } PyObject *proc_addr_obj = PyLong_FromVoidPtr(proc_addr); if (!proc_addr_obj) { return NULL; } - if (!pg_gl_proc_from_address) { - return RAISE(PyExc_TypeError, "'_proc_from_address' object is NULL"); + + // load ctypes_functype if it's NULL + if (!ctypes_functype) { + PyObject *ctypes_module = PyImport_ImportModule("ctypes"); + if (!ctypes_module) { + return NULL; + } + + PyObject *ctypes_functype_factory; +#if defined(_WIN32) + // gl proc need to be called with WINFUNCTYPE (stdcall) on win32 + ctypes_functype_factory = + PyObject_GetAttrString(ctypes_module, "WINFUNCTYPE"); +#else + ctypes_functype_factory = + PyObject_GetAttrString(ctypes_module, "CFUNCTYPE"); +#endif + Py_DECREF(ctypes_module); + if (!ctypes_functype_factory) { + return NULL; + } + + ctypes_functype = + PyObject_CallOneArg(ctypes_functype_factory, Py_None); + Py_DECREF(ctypes_functype_factory); + if (!ctypes_functype) { + return NULL; + } } - PyObject *retv = - PyObject_CallFunction(pg_gl_proc_from_address, "(O)", proc_addr_obj); + + PyObject *retv = PyObject_CallOneArg(ctypes_functype, proc_addr_obj); Py_DECREF(proc_addr_obj); return retv; } @@ -3101,19 +3129,6 @@ MODINIT_DEFINE(display) return NULL; } - /* load _ffi module for display.get_gl_proc function */ - PyObject *pg_ffi_module = PyImport_ImportModule("pygame._ffi"); - if (!pg_ffi_module) { - return NULL; - } - - pg_gl_proc_from_address = - PyObject_GetAttrString(pg_ffi_module, "_gl_proc_from_address"); - if (!pg_gl_proc_from_address) { - return NULL; - } - Py_DECREF(pg_ffi_module); - /* create the module */ module = PyModule_Create(&_module); if (module == NULL) { diff --git a/src_py/_ffi.py b/src_py/_ffi.py deleted file mode 100644 index b40cdaea31..0000000000 --- a/src_py/_ffi.py +++ /dev/null @@ -1,9 +0,0 @@ -import ctypes -import sys - - -def _gl_proc_from_address(addr): - if sys.platform == "win32": - # use __stdcall on win32 - return ctypes.WINFUNCTYPE(None)(addr) - return ctypes.CFUNCTYPE(None)(addr) diff --git a/src_py/meson.build b/src_py/meson.build index 5eba01ced9..541c54cd69 100644 --- a/src_py/meson.build +++ b/src_py/meson.build @@ -4,7 +4,6 @@ python_sources = files( '_camera_opencv.py', '_data_classes.py', '_debug.py', - '_ffi.py', '_sprite.py', 'camera.py', 'colordict.py',