Last active
November 12, 2025 21:16
-
-
Save 8Observer8/c6bb7fa2929355a00134a68dbbeb453b to your computer and use it in GitHub Desktop.
Background color using OpenGL, PySDL3, and Python
This file contains hidden or 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
| # pip install PyOpenGL PySDL3 | |
| import os | |
| from OpenGL.GL import * | |
| os.environ["SDL_MAIN_USE_CALLBACKS"] = "1" | |
| os.environ["SDL_RENDER_DRIVER"] = "opengl" | |
| import sdl3 | |
| glContext = None | |
| window = None | |
| @sdl3.SDL_AppInit_func | |
| def SDL_AppInit(appstate, argc, argv): | |
| global glContext | |
| global window | |
| if not sdl3.SDL_Init(sdl3.SDL_INIT_VIDEO): | |
| sdl3.SDL_Log("Couldn't initialize SDL: %s".encode() % sdl3.SDL_GetError()) | |
| return sdl3.SDL_APP_FAILURE | |
| window = sdl3.SDL_CreateWindow("OpenGL, PySDL, Python".encode(), | |
| 350, 350, sdl3.SDL_WINDOW_OPENGL) | |
| if not window: | |
| sdl3.SDL_Log("Couldn't create the window: %s".encode() % sdl3.SDL_GetError) | |
| return sdl3.SDL_APP_FAILURE | |
| glContext = sdl3.SDL_GL_CreateContext(window) | |
| if not glContext: | |
| sdl3.SDL_Log("Couldn't create the OpenGL context: %s".encode() % sdl3.SDL_GetError) | |
| return sdl3.SDL_APP_FAILURE | |
| sdl3.SDL_GL_SetSwapInterval(1) # Turn on vertical sync | |
| glClearColor(0.2, 0.2, 0.2, 1) | |
| return sdl3.SDL_APP_CONTINUE | |
| @sdl3.SDL_AppEvent_func | |
| def SDL_AppEvent(appstate, event): | |
| type = sdl3.SDL_DEREFERENCE(event).type | |
| if type == sdl3.SDL_EVENT_QUIT: | |
| return sdl3.SDL_APP_SUCCESS | |
| return sdl3.SDL_APP_CONTINUE | |
| @sdl3.SDL_AppIterate_func | |
| def SDL_AppIterate(appstate): | |
| glClear(GL_COLOR_BUFFER_BIT) | |
| sdl3.SDL_GL_SwapWindow(window) | |
| return sdl3.SDL_APP_CONTINUE | |
| @sdl3.SDL_AppQuit_func | |
| def SDL_AppQuit(appstate, result): | |
| global glContext | |
| sdl3.SDL_GL_DestroyContext(glContext) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment