From 4e068591bd5456aaa47f751a017d373fdc8de2dd Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sat, 18 May 2019 23:07:38 +0200 Subject: [PATCH] Use enum class to describe actual state --- rwengine/src/audio/SoundBuffer.cpp | 6 +++--- rwengine/src/audio/SoundBuffer.hpp | 7 ++++++- rwengine/src/audio/SoundBufferStreamed.cpp | 12 ++++++------ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/rwengine/src/audio/SoundBuffer.cpp b/rwengine/src/audio/SoundBuffer.cpp index 04494323..40d0aea8 100644 --- a/rwengine/src/audio/SoundBuffer.cpp +++ b/rwengine/src/audio/SoundBuffer.cpp @@ -51,16 +51,16 @@ bool SoundBuffer::isStopped() const { } void SoundBuffer::play() { - running = true; + state = State::Playing; alCheck(alSourcePlay(source)); } void SoundBuffer::pause() { - running = false; + state = State::Stopped; alCheck(alSourcePause(source)); } void SoundBuffer::stop() { - running = false; + state = State::Stopped; alCheck(alSourceStop(source)); } diff --git a/rwengine/src/audio/SoundBuffer.hpp b/rwengine/src/audio/SoundBuffer.hpp index 379162da..6bdb8abe 100644 --- a/rwengine/src/audio/SoundBuffer.hpp +++ b/rwengine/src/audio/SoundBuffer.hpp @@ -27,8 +27,13 @@ struct SoundBuffer { void setGain(float gain); void setMaxDistance(float maxDist); + enum class State { + Stopped, + Playing + }; + ALuint source; - bool running = false; + State state = State::Stopped; private: ALuint buffer; }; diff --git a/rwengine/src/audio/SoundBufferStreamed.cpp b/rwengine/src/audio/SoundBufferStreamed.cpp index 70823110..0a3413d8 100644 --- a/rwengine/src/audio/SoundBufferStreamed.cpp +++ b/rwengine/src/audio/SoundBufferStreamed.cpp @@ -59,8 +59,8 @@ void SoundBufferStreamed::play() { alSourcePlay(source); // Maybe another thread is running, we should tell him to stop - if (running) { - running = false; + if (state == State::Playing) { + state = State::Stopped; } } // Use preloaded data (and give another thread time to stop) @@ -72,11 +72,11 @@ void SoundBufferStreamed::play() { } void SoundBufferStreamed::updateBuffers() { - running = true; + state = State::Playing; while (true) { { std::lock_guard lock(soundSource->mutex); - if (!running) { + if (state == State::Stopped) { return; } @@ -131,11 +131,11 @@ void SoundBufferStreamed::updateBuffers() { void SoundBufferStreamed::pause() { std::lock_guard lock(soundSource->mutex); - running = false; + state = State::Stopped; alCheck(alSourcePause(source)); } void SoundBufferStreamed::stop() { std::lock_guard lock(soundSource->mutex); - running = false; + state = State::Stopped; alCheck(alSourceStop(source)); }