Window Visibility

Window flags after restart

Are window flags like SDL_WINDOW_HIDDEN preserved after restarting the program?

Abstract art representing computer programming

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.

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