Building SDL2 from a Subdirectory (CMake)

Creating a Fullscreen Window with SDL2

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

Vector illustration representing computer programming

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.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 62 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved