Creating a Fullscreen Window with SDL2

How do I create a fullscreen window using SDL2 instead of a windowed one?

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.

Building SDL2 from a Subdirectory (CMake)

A step-by-step guide on setting up SDL2 and useful extensions in a project that uses CMake as its build system

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Troubleshooting CMake Errors with SDL2
I followed the lesson but I am getting CMake errors when trying to build my project with SDL2. How can I troubleshoot this?
Using SDL2 without CMake
Is it possible to use SDL2 in my C++ project without using CMake? How would I set that up?
Enabling VSync with SDL2
How can I enable vertical synchronization (VSync) in my SDL2 application to prevent screen tearing?
Playing Audio with SDL2
The lesson focused on graphics and input. How can I play audio in my SDL2 application?
Implementing a Basic Game Loop with SDL2
The lesson's example code uses a simple while loop for the main game loop. What does a more complete game loop look like in SDL2?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant