Handle window close cleanly and show exit popup selection
This commit is contained in:
@ -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();
|
||||
|
||||
@ -72,6 +72,7 @@ private:
|
||||
std::mutex tracksMutex;
|
||||
std::mutex pendingTracksMutex;
|
||||
std::atomic<bool> loadingComplete{false};
|
||||
std::atomic<bool> loadingAbort{false};
|
||||
std::atomic<int> loadedCount{0};
|
||||
|
||||
// SFX mixing support
|
||||
|
||||
Reference in New Issue
Block a user