Skip to content

Commit

Permalink
Update gl_get_proc function
Browse files Browse the repository at this point in the history
- Import ctypes directly in display.c
- Don't use SDL_GetError when SDL_GL_GetProcAddress returns NULL
  • Loading branch information
yunline committed Jan 10, 2025
1 parent bd98dae commit e51dbca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
53 changes: 34 additions & 19 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 0 additions & 9 deletions src_py/_ffi.py

This file was deleted.

1 change: 0 additions & 1 deletion src_py/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ python_sources = files(
'_camera_opencv.py',
'_data_classes.py',
'_debug.py',
'_ffi.py',
'_sprite.py',
'camera.py',
'colordict.py',
Expand Down

0 comments on commit e51dbca

Please sign in to comment.