OpenGL & Vulkan windowsSDL 3.2

SDL_GL_CreateContext

Creates an OpenGL context for an OpenGL window and makes it current.

Signature

SDL_GLContext SDL_GL_CreateContext(SDL_Window *window);

How it works

The window must have been created with SDL_WINDOW_OPENGL. The context comes back already current, so the next step is loading the GL functions with glad and SDL_GL_GetProcAddress.

The context follows the attributes set with SDL_GL_SetAttribute; if the version you asked for is not available, this returns NULL.

Parameters

windowSDL_Window *

A window created with SDL_WINDOW_OPENGL.

Return value

The context, or NULL on failure.

Example

example.cpp
1SDL_GLContext gl = SDL_GL_CreateContext(window);
2if (!gl) { SDL_Log("GL context: %s", SDL_GetError()); return 1; }
3gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
Official SDL wiki