pyGLFW

repository·master·Indexed 42 days ago

https://github.com/florianrhiem/pyglfw

Pythonic ctypes bindings for the GLFW library, used for creating windows, OpenGL contexts, and handling input/events. It provides a Python-friendly API that replaces C-style naming and data structures with Python sequences, namedtuples, and underscore-based naming conventions.

Tokens
1.1K
Snippets
2
Records
7
Agent score
4%

What's inside pyGLFW

  1. Understand pyGLFW API differences from C GLFW

    master

    pyGLFW is a ctypes wrapper that follows Pythonic conventions. Key differences from the original C API include:

    • Naming Convention: Function names use words_with_underscores instead of camelCase.
    • Namespacing: The GLFW_ and glfw prefixes are removed from function names (e.g., glfwCreateWindow becomes glfw.create_window). If you prefer the C-style naming, you can use from glfw.GLFW import *.
    • Data Structures: Structs are replaced with Python sequences and namedtuples.
    • Return Values: Functions like glfwGetMonitors return a Python list instead of a pointer and an object count.
    • Gamma Ramps: Uses floats between 0.0 and 1.0 instead of unsigned shorts. To revert to the original behavior, set glfw.NORMALIZE_GAMMA_RAMPS=False.
    • Image Handling: You can pass PIL/Pillow Image objects instead of providing a sequence for GLFWimage structs.
  2. Install pyGLFW

    master

    You can install the stable version of pyGLFW using pip:

    pip install glfw

    If you need to use wrappers for currently unreleased macros and functions from the development version of GLFW, install the preview version:

    pip install glfw[preview]

    Alternatively, you can enable preview mode by setting the PYGLFW_PREVIEW environment variable.

  3. Basic window and OpenGL context lifecycle

    master

    To create a basic windowed application, follow this lifecycle:

    1. Initialize: Call glfw.init().
    2. Create Window: Use glfw.create_window(width, height, title, monitor, share) to create a window and its OpenGL context.
    3. Make Context Current: Call glfw.make_context_current(window) to make the window's context the current one for the calling thread.
    4. Event Loop: Run a loop checking glfw.window_should_close(window). Inside the loop:
      • Perform rendering (e.g., via pyOpenGL).
      • Swap buffers with glfw.swap_buffers(window).
      • Process events with glfw.poll_events().
    5. Terminate: Call glfw.terminate() to clean up resources.
    import glfw
    
    def main():
        # Initialize the library
        if not glfw.init():
            return
    
        # Create a windowed mode window and its OpenGL context
        window = glfw.create_window(640, 480, "Hello World", None, None)
        if not window:
            glfw.terminate()
            return
    
        # Make the window's context current
        glfw.make_context_current(window)
    
        # Loop until the user closes the window
        while not glfw.window_should_close(window):
            # Render here, e.g. using pyOpenGL
    
            # Swap front and back buffers
            glfw.swap_buffers(window)
    
            # Poll for and process events
            glfw.poll_events()
    
        glfw.terminate()
    
    if __name__ == "__main__":
        main()
  4. Select GLFW library variant on Linux

    master
    On Linux, pyGLFW attempts to detect whether to use the Wayland or X11 variant of the GLFW library. You can manually force a specific variant by setting the PYGLFW_LIBRARY_VARIANT environment variable to either 'wayland' or 'x11'.
  5. Configure GLFW error reporting

    master

    By default, GLFW errors are reported as glfw.GLFWError warnings if no error callback is set. You can control this behavior using the glfw.ERROR_REPORTING setting:

    • Set it to 'warn' to issue warnings.
    • Set it to 'log' to log errors using the 'glfw' logger.
    • Set it to a dict to define specific behaviors for specific error codes.
    • Set it to False to disable error reporting entirely.