-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
537f649
commit 5cc0839
Showing
2 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters