pyglet / pyglet

pyglet is a cross-platform windowing and multimedia library for Python, for developing games and other visually rich applications.
http://pyglet.org
BSD 3-Clause "New" or "Revised" License
1.9k stars 307 forks source link

Hint-friendly pyglet.gl.* funcs #1237

Open iperov opened 3 days ago

iperov commented 3 days ago

Describe the solution you'd like

pyglet.gl.gl* funcs have no popup hint of arguments

here is my code example to use annotations which will work as IDE hint and ctypes configurator

dlls_by_name = {}

def dll_import(dll_name):
    """
    decorator for import DLL func.
    always annotate return type even if it is None !
    """
    dll_name = str(dll_name)

    def decorator(func):
        dll_func = None
        def wrapper(*args):
            nonlocal dll_func

            if dll_func is None:
                dll = dlls_by_name.get(dll_name, None)
                if dll is None:
                    try:
                        dll = ctypes.cdll.LoadLibrary(find_library(dll_name))
                    except:
                        pass

                    if dll is None:
                        raise RuntimeError(f'Unable to load {dll_name} library.')

                    dlls_by_name[dll_name] = dll

                dll_func = getattr(dll, func.__name__)
                anno = list(get_type_hints(func).values())
                anno = [ None if x is type(None) else x for x in anno ]

                dll_func.argtypes = anno[:-1]
                dll_func.restype = anno[-1]

            return dll_func(*args)

        return wrapper
    return decorator

@dll_import('OpenCL')
def clGetPlatformIDs (num_entries : cl_uint, platforms : POINTER(cl_platform_id), num_platforms : POINTER(cl_uint) ) -> cl_result: ...

Code_2024-11-27_12-36-11

Could probably be improved.

benmoran56 commented 2 days ago

Thanks for opening the ticket. The GL bindings are automatically generated from the spec by this tool: https://github.com/pyglet/pyglet/blob/master/tools/gengl.py

Could your idea be implemented directly at this stage? It seems like it would be the appropriate place for it.

iperov commented 2 days ago

I don't know the structure of pyglet. I only suggested how it might look like in the end.