Handle window close cleanly and show exit popup selection

This commit is contained in:
2025-11-22 11:43:20 +01:00
parent a257c5cd79
commit 838b5b1836
6 changed files with 39 additions and 15 deletions

View File

@ -217,6 +217,7 @@ void Audio::startBackgroundLoading() {
return;
}
}
loadingAbort = false;
loadingComplete = false;
loadedCount = 0;
loadingThread = std::thread(&Audio::backgroundLoadingThread, this);
@ -235,12 +236,18 @@ void Audio::backgroundLoadingThread() {
#endif
while (true) {
if (loadingAbort.load()) {
break;
}
std::string path;
{
std::lock_guard<std::mutex> lock(pendingTracksMutex);
if (pendingTracks.empty()) break;
path = std::move(pendingTracks.front());
pendingTracks.erase(pendingTracks.begin());
if (loadingAbort.load()) {
break;
}
}
AudioTrack t;
t.path = path;
@ -255,6 +262,10 @@ void Audio::backgroundLoadingThread() {
#endif
// Thread-safe addition to tracks
if (loadingAbort.load()) {
break;
}
{
std::lock_guard<std::mutex> lock(tracksMutex);
tracks.push_back(std::move(t));
@ -262,8 +273,12 @@ void Audio::backgroundLoadingThread() {
loadedCount++;
// Small delay to prevent overwhelming the system
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// Small delay to prevent overwhelming the system (unless abort requested)
if (!loadingAbort.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} else {
break;
}
}
#ifdef _WIN32
@ -328,9 +343,15 @@ void Audio::playGameMusic() {
void Audio::shutdown(){
// Stop background loading thread first
loadingAbort = true;
{
std::lock_guard<std::mutex> lock(pendingTracksMutex);
pendingTracks.clear();
}
if (loadingThread.joinable()) {
loadingThread.join();
}
loadingComplete = true;
if(audioStream){ SDL_DestroyAudioStream(audioStream); audioStream=nullptr; }
tracks.clear();