Window flags after restart

Are window flags like SDL_WINDOW_HIDDEN preserved after restarting the program?

No, SDL window flags like SDL_WINDOW_HIDDEN are not preserved when you restart your program. These flags are properties of the window object in memory, and when the program ends, the window and all its associated state are destroyed.

When the program is restarted, you must reapply any desired flags during the window creation process.

Why This Happens

SDL does not persist window state across runs because it's designed for real-time applications where state management is the responsibility of the programmer.

If you want to restore a specific window configuration, you need to save that information manually (e.g., in a file or database) and reapply it during initialization.

Example: Saving Window State

Here's an example where we save and restore the visibility state:

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

// Save state to a file
void SaveWindowState(bool hidden) {
  std::ofstream file("window_state.txt");
  file << (hidden ? 1 : 0) << '\n';
}

// Load state from a file
bool LoadWindowState() {
  std::ifstream file("window_state.txt");
  int state = 0;
  if (file >> state) { return state == 1; }
  return false;
}

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

  bool hidden = LoadWindowState();
  Uint32 flags = hidden
                   ? SDL_WINDOW_HIDDEN
                   : SDL_WINDOW_SHOWN;

  SDL_Window* window =
    SDL_CreateWindow("Restore Flags",
                     SDL_WINDOWPOS_CENTERED,
                     SDL_WINDOWPOS_CENTERED,
                     640, 480, flags);
  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) {
        SaveWindowState(
          SDL_GetWindowFlags(window) &
          SDL_WINDOW_HIDDEN);
        running = false;
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
[Window state restored based on previous session.]

This approach ensures that your application behaves consistently across restarts, even though SDL itself doesn't maintain such state.

Window Visibility

Learn how to control the visibility of SDL2 windows, including showing, hiding, minimizing, and more

Questions & Answers

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

Why hide a window?
Why would I want to hide a window in my program?
Check multiple SDL window flags
Can I check multiple window flags simultaneously?
SDL_FlashWindow() platform support
What platforms support the SDL_FlashWindow() function?
Do hidden windows process events?
Can a hidden window still process events or render content?
Toggle always-on-top with a key
How can I toggle the always-on-top behavior with a key press?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant