To create a fullscreen window with SDL2, you can use the SDL_WINDOW_FULLSCREEN
flag when creating your window. Here's an example:
#include <SDL.h>
int main(int argc, char** argv) {
SDL_Window* window{SDL_CreateWindow(
"Fullscreen Example",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
0, 0,
SDL_WINDOW_FULLSCREEN
)};
// ...
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
When using SDL_WINDOW_FULLSCREEN
, you should set the window's width and height to 0, as SDL will automatically use the resolution of the fullscreen display.
You can also use SDL_WINDOW_FULLSCREEN_DESKTOP
instead, which will create a borderless window that covers the entire screen without changing the display resolution.
To toggle between windowed and fullscreen modes at runtime, you can use SDL_SetWindowFullscreen
:
Uint32 flags = SDL_GetWindowFlags(window);
if (flags & SDL_WINDOW_FULLSCREEN) {
SDL_SetWindowFullscreen(window, 0);
} else {
SDL_SetWindowFullscreen(
window, SDL_WINDOW_FULLSCREEN);
}
This will switch the window between fullscreen and windowed modes when called.
Answers to questions are automatically generated and may not have been reviewed.
A step-by-step guide on setting up SDL2 and useful extensions in a project that uses CMake as its build system