File: windowed.cpp

package info (click to toggle)
python-moderngl-glcontext 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: cpp: 1,373; python: 212; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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

#include <GL/gl.h>
#include <GL/glx.h>

PyObject * meth_load_opengl_function(PyObject * self, PyObject * arg) {
    if (!PyUnicode_CheckExact(arg)) {
        return NULL;
    }
    const char * name = PyUnicode_AsUTF8(arg);
    return PyLong_FromVoidPtr((void *)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;
}