Window Visibility

Why hide a window?

Why would I want to hide a window in my program?

Abstract art representing computer programming

Hiding a window can be useful in a variety of situations. One common scenario is when you're working on applications that require temporary user distraction or focus management.

For example, a game launcher might hide the main application window while a settings dialog or a login screen is active, ensuring the user completes those actions before returning to the main window.

Similarly, background processes or utilities might use hidden windows to initialize resources without requiring immediate user interaction.

Another case is for applications where performance matters, and you want to delay rendering or event processing for windows that aren't visible. In SDL2, a hidden window won't be displayed on the screen, but you can still keep it in memory and manage its resources.

This allows for a seamless experience when you show the window again. Consider this example:

#include <SDL.h>
#include <iostream>

int main() {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    std::cout << "SDL_Init Error: "
      << SDL_GetError() << '\n';
    return 1;
  }

  SDL_Window *window = SDL_CreateWindow(
    "Example",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    640, 480,
    SDL_WINDOW_HIDDEN
  );
  if (!window) {
    std::cout << "SDL_CreateWindow Error: "
      << SDL_GetError() << '\n';
    SDL_Quit();
    return 1;
  }

  // Simulate some work
  SDL_Delay(2000);

  SDL_ShowWindow(window);  
  SDL_Delay(2000);

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
[No visible window for 2 seconds, then shown for 2 seconds]

This example creates a hidden window and only shows it after initialization. Using SDL_HideWindow() and SDL_ShowWindow(), you can toggle visibility dynamically, which is handy for use cases like splash screens or in-app tutorials.

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