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