SDL_WINDOWEVENT_CLOSE vs SDL_QUIT

What's the difference between SDL_WINDOWEVENT_CLOSE and SDL_QUIT?

While SDL_WINDOWEVENT_CLOSE and SDL_QUIT are both related to closing windows, they serve different purposes.

SDL_WINDOWEVENT_CLOSE

This event is specific to an individual SDL window. It occurs when the user attempts to close a window (e.g., by clicking the close button). The event contains the windowID, allowing you to determine which window the action applies to.

if (event.type == SDL_WINDOWEVENT &&
    event.window.event == SDL_WINDOWEVENT_CLOSE) {
  std::cout << "User attempted to close window ID: "
            << event.window.windowID << '\n';
}

SDL_QUIT

This event is more general and signals the application should terminate. It typically occurs when:

  • The last window is closed.
  • The user sends a termination signal (e.g., pressing Ctrl+C in the console).
if (event.type == SDL_QUIT) {
  std::cout << "Application quit signal received\n";
}

Key Differences

  • SDL_WINDOWEVENT_CLOSE is window-specific, while SDL_QUIT affects the entire application.
  • If you close all SDL windows, SDL usually triggers SDL_QUIT.

Understanding this distinction helps in multi-window applications where closing one window doesn't necessarily mean quitting the app.

Window Events and Window IDs

Discover how to monitor and respond to window state changes in SDL applications

Questions & Answers

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

Purpose of SDL Window Events
Why do we monitor SDL window events rather than continuously checking window properties?
How Window IDs Work
What are SDL window IDs, and how are they useful?
Changing Window IDs
Can I change a window's ID after it's created?
SDL_WINDOWEVENT_ENTER Triggering Issues
Why does SDL_WINDOWEVENT_ENTER not always trigger when expected?
Handling External Library Windows
How can I handle events for windows created by external libraries?
Handling Invalid Window IDs
What happens if I pass an invalid windowID to SDL_GetWindowFromID()?
Simulating SDL_WINDOWEVENT
How can I simulate a SDL_WINDOWEVENT for testing purposes?
Or Ask your Own Question
Purchase the course to ask your own questions