OpenGL & Vulkan windowsSDL 3.2

SDL_GL_SetAttribute

Requests one property of the OpenGL context and framebuffer (version, profile, depth bits…) BEFORE the window is created.

Signature

bool SDL_GL_SetAttribute(SDL_GLAttr attr, int value);

How it works

The SDL equivalent of glfwWindowHint. The framebuffer format is chosen when the window is created, so set the attributes first, then SDL_CreateWindow with SDL_WINDOW_OPENGL, then SDL_GL_CreateContext.

Parameters

attrSDL_GLAttr

Which property.

Common values

SDL_GL_CONTEXT_MAJOR_VERSION / _MINOR_VERSION
OpenGL version, e.g. 4 and 6.
SDL_GL_CONTEXT_PROFILE_MASK
SDL_GL_CONTEXT_PROFILE_CORE (modern only) or _COMPATIBILITY.
SDL_GL_CONTEXT_FLAGS
SDL_GL_CONTEXT_DEBUG_FLAG for a debug context; SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG for macOS.
SDL_GL_DEPTH_SIZE / SDL_GL_STENCIL_SIZE
Bits of depth and stencil, e.g. 24 and 8.
SDL_GL_MULTISAMPLEBUFFERS / SDL_GL_MULTISAMPLESAMPLES
1 and 4 for 4x MSAA.
SDL_GL_DOUBLEBUFFER
1 (default) for a back buffer.
valueint

The requested value.

Return value

true on success, false on failure.

Example

example.cpp
1SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
2SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
3SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
4SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
5SDL_Window* window = SDL_CreateWindow("GL", 1280, 720, SDL_WINDOW_OPENGL);
Official SDL wiki