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.
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.]
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.
Learn how to control the visibility of SDL2 windows, including showing, hiding, minimizing, and more