Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement display.gl_get_proc #3285

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yunline
Copy link
Contributor

@yunline yunline commented Jan 8, 2025

Trys to fix #3284

Example:

import pygame
import ctypes

win = pygame.Window(opengl=True)

GL_VENDOR = 0x1F00
GL_RENDERER = 0x1F01
GL_VERSION = 0x1F02
GL_EXTENSIONS = 0x1F03

glGetString = pygame.display.gl_get_proc("glGetString")
glGetString.restype = ctypes.c_char_p
glGetString.argtypes = (ctypes.c_int,)
print(glGetString(GL_VENDOR))

# prints b'ATI Technologies Inc.' on my computer

WARNING
This PR introduces a new dependency: ctypes

This PR is experimental. If you have better idea, feel free to open another PR.

- SDL3 compat
- Use WINFUNCTYPE (stdcall) on win32
- Use ctypes._FuncPointer in stubs
@Starbuck5
Copy link
Member

Thoughts:

Maybe it would be simpler to import ctypes instead of a custom module into display.c, and then use PyObject_call stuff and ifdef WIN32 to do the same logic as the custom ffi.py module

Maybe we should give the pointer only, and let the user worry about ctypes, since they're going to need to worry about ctypes anyway to extract any value (as I understand it). Plus it makes it simpler code for this seemingly-niche use case.

@yunline yunline added opengl New API This pull request may need extra debate as it adds a new class or function to pygame labels Jan 9, 2025
- Import ctypes directly in display.c
- Don't use SDL_GetError when SDL_GL_GetProcAddress returns NULL

| :sl:`Get an OpenGL function by name`
| :sg:`gl_get_proc(proc_name) -> ctypes._FuncPointer`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an uncommon function it could be explained with a bit more detail, for example mentioning that it will try to import ctypes if it is not imported, the fact that it can fail and raise errors, and if it's relevant roughly how to use the return value with an embedded example.

@damusss
Copy link
Member

damusss commented Jan 17, 2025

[assuming the "changes" are addressed/discussed and that the code actually works]
It's true that it's uncommon but sdl still exports it and it makes sense to reside in display, given other GL functions are already there. plus it makes sense if one needs to do something simple with opengl and doesn't want to import something big like PyOpenGL, or if it is using something like moderngl and only has a missing function or two, it can use this function instead.

I think it's ok for it to return a ctypes object since it is imported only on first function call. if we didn't use ctypes the return value wouldn't be much of an abstraction, so it would be less useful, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New API This pull request may need extra debate as it adds a new class or function to pygame opengl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose SDL_GL_GetProcAddress
3 participants