Window Visibility

Do hidden windows process events?

Can a hidden window still process events or render content?

Abstract art representing computer programming

Yes, a hidden window can still process events, but rendering content to it will have no visible effect while the window remains hidden. In SDL2, hidden windows retain their event handling functionality, so you can continue to process user input or system events as usual.

However, since the window isn't being drawn to the screen, rendering operations won't be visible until you explicitly show the window using SDL_ShowWindow().

This behavior can be useful in applications where you need to prepare content in the background without displaying it immediately. For instance, you might preload textures, initialize UI elements, or handle logic updates for a hidden window before making it visible.

Event Handling for Hidden Windows

The event handling loop works the same way for hidden windows as it does for visible ones. Here's an 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(
    "Hidden Window",
    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;
  }

  SDL_Event event;
  bool running = true;
  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_KEYDOWN) {
        std::cout << "Key pressed: "
          << event.key.keysym.sym << '\n'; 
        SDL_ShowWindow(window); 
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
[Key presses will be logged. Window becomes visible when a key is pressed.]

Rendering Content

You can render to a hidden window, but the results won’t be displayed until the window is shown:

SDL_HideWindow(window);
// Prepare the next frame
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_ShowWindow(window);

This approach ensures that the user only sees a fully prepared frame when the window appears.

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