Skip to content

Commit

Permalink
implement windowed gl provider
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcsdombi committed Dec 19, 2022
1 parent 537f649 commit 5cc0839
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
73 changes: 73 additions & 0 deletions glcontext/windowed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <Python.h>

#if defined(_WIN32) || defined(_WIN64)

#include <Windows.h>

PyObject * meth_load_opengl_function(PyObject * self, PyObject * arg) {
if (!PyUnicode_CheckExact(arg)) {
return NULL;
}
HMODULE module = GetModuleHandle("opengl32");
if (!module) {
return NULL;
}
const char * name = PyUnicode_AsUTF8(arg);
void * proc = (void *)GetProcAddress(module, name);
if (!proc) {
proc = wglGetProcAddress(name);
}
return PyLong_FromVoidPtr(proc);
}

#elif defined(__APPLE__)

#include <OpenGL/OpenGL.h>
#include <ApplicationServices/ApplicationServices.h>

#import <mach-o/dyld.h>
#import <stdlib.h>
#import <string.h>

#include <OpenGL/gl.h>
#include <OpenGL/glext.h>

PyObject * meth_load_opengl_function(PyObject * self, PyObject * arg) {
if (!PyUnicode_CheckExact(arg)) {
return NULL;
}
PyObject * prefix = PyUnicode_FromString("_");
PyObject * prefixed = PyNumber_Add(prefix, arg);
NSSymbol symbol = NULL;
const char * method = PyUnicode_AsUTF8(prefixed);
if (NSIsSymbolNameDefined(method)) {
symbol = NSLookupAndBindSymbol(method);
}
Py_DECREF(prefixed);
Py_DECREF(prefix);
return PyLong_FromVoidPtr(symbol ? NSAddressOfSymbol(symbol) : NULL);
}

#else

PyObject * meth_load_opengl_function(PyObject * self, PyObject * arg) {
if (!PyUnicode_CheckExact(arg)) {
return NULL;
}
const char * name = PyUnicode_AsUTF8(arg);
return PyLong_FromVoidPtr(glXGetProcAddress((unsigned char *)name));
}

#endif

PyMethodDef module_methods[] = {
{"load_opengl_function", (PyCFunction)meth_load_opengl_function, METH_O},
{},
};

PyModuleDef module_def = {PyModuleDef_HEAD_INIT, "windowed", NULL, -1, module_methods};

extern "C" PyObject * PyInit_windowed() {
PyObject * module = PyModule_Create(&module_def);
return module;
}
29 changes: 26 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@
libraries=['EGL'],
)

if target == 'windows':
windowed = Extension(
name='glcontext.windowed',
sources=['glcontext/windowed.cpp'],
libraries=['opengl32'],
)

if target == 'linux':
windowed = Extension(
name='glcontext.windowed',
sources=['glcontext/windowed.cpp'],
extra_compile_args=['-fpermissive'],
libraries=['GL'],
)

if target == 'darwin':
windowed = Extension(
name='glcontext.windowed',
sources=['glcontext/windowed.cpp'],
extra_compile_args=['-fpermissive', '-Wno-deprecated-declarations'],
extra_link_args=['-framework', 'OpenGL', '-Wno-deprecated'],
)

darwin = Extension(
name='glcontext.darwin',
sources=['glcontext/darwin.cpp'],
Expand All @@ -58,9 +81,9 @@
)

ext_modules = {
'windows': [wgl],
'linux': [x11, egl, headless],
'darwin': [darwin],
'windows': [wgl, windowed],
'linux': [x11, egl, headless, windowed],
'darwin': [darwin, windowed],
}

setup(
Expand Down

0 comments on commit 5cc0839

Please sign in to comment.