drop usage of threads in sound system

master
Eduardo Bart 12 years ago
parent 525feaf7fb
commit ae67c6adbc

@ -22,7 +22,7 @@ ENDIF()
SET(Boost_USE_MULTITHREADED ON) SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_LIBS ON) SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost COMPONENTS system thread REQUIRED) FIND_PACKAGE(Boost COMPONENTS system REQUIRED)
IF(USE_OPENGL_ES2) IF(USE_OPENGL_ES2)
FIND_PACKAGE(OpenGLES2 REQUIRED) FIND_PACKAGE(OpenGLES2 REQUIRED)

@ -209,6 +209,8 @@ void Application::exit()
void Application::poll() void Application::poll()
{ {
g_sounds.poll();
// poll input events // poll input events
g_window.poll(); g_window.poll();
//g_particleManager.update(); //g_particleManager.update();

@ -33,8 +33,6 @@ SoundManager g_sounds;
void SoundManager::init() void SoundManager::init()
{ {
m_run = false;
m_device = alcOpenDevice(NULL); m_device = alcOpenDevice(NULL);
if(!m_device) { if(!m_device) {
logError("unable to open audio device"); logError("unable to open audio device");
@ -46,27 +44,20 @@ void SoundManager::init()
logError("unable to create audio context: ", alcGetString(m_device, alcGetError(m_device))); logError("unable to create audio context: ", alcGetString(m_device, alcGetError(m_device)));
return; return;
} }
alcMakeContextCurrent(m_context); alcMakeContextCurrent(m_context);
m_thread = std::thread(std::bind(&SoundManager::audioThread, &g_sounds));
while(!m_run)
g_clock.sleep(1);
m_musicEnabled = true; m_musicEnabled = true;
m_soundEnabled = true; m_soundEnabled = true;
/* /*
g_eventDispatcher.scheduleEvent([this] { g_eventDispatcher.scheduleEvent([this] {
play("/test.ogg"); play("/1.ogg");
}, 10); }, 10);
*/ */
} }
void SoundManager::terminate() void SoundManager::terminate()
{ {
m_run = false;
m_thread.join();
m_sources.clear(); m_sources.clear();
m_buffers.clear(); m_buffers.clear();
m_musicSource = nullptr; m_musicSource = nullptr;
@ -88,25 +79,14 @@ void SoundManager::terminate()
} }
} }
void SoundManager::audioThread() void SoundManager::poll()
{
m_run = true;
while(m_run) {
//TODO: use condition variable
g_clock.sleep(30);
update();
}
}
void SoundManager::update()
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
static ticks_t lastUpdate = 0; static ticks_t lastUpdate = 0;
ticks_t now = g_clock.asyncTicks(); ticks_t now = g_clock.ticks();
if(now - lastUpdate < 300) if(now - lastUpdate < POLL_DELAY)
return; return;
lastUpdate = now; lastUpdate = now;
for(auto it = m_sources.begin(); it != m_sources.end();) { for(auto it = m_sources.begin(); it != m_sources.end();) {
@ -133,8 +113,6 @@ void SoundManager::update()
void SoundManager::preload(const std::string& filename) void SoundManager::preload(const std::string& filename)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
auto it = m_buffers.find(filename); auto it = m_buffers.find(filename);
if(it != m_buffers.end()) if(it != m_buffers.end())
return; return;
@ -152,16 +130,12 @@ void SoundManager::preload(const std::string& filename)
void SoundManager::enableSound(bool enable) void SoundManager::enableSound(bool enable)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if(!isAudioEnabled()) if(!isAudioEnabled())
return; return;
} }
void SoundManager::play(const std::string& filename) void SoundManager::play(const std::string& filename)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if(!m_soundEnabled) if(!m_soundEnabled)
return; return;
@ -179,8 +153,6 @@ void SoundManager::play(const std::string& filename)
void SoundManager::enableMusic(bool enable) void SoundManager::enableMusic(bool enable)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if(!isAudioEnabled()) if(!isAudioEnabled())
return; return;
@ -194,8 +166,6 @@ void SoundManager::enableMusic(bool enable)
void SoundManager::playMusic(const std::string& filename, bool fade) void SoundManager::playMusic(const std::string& filename, bool fade)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if(m_currentMusic == filename && m_musicSource) if(m_currentMusic == filename && m_musicSource)
return; return;
m_currentMusic = filename; m_currentMusic = filename;
@ -211,7 +181,7 @@ void SoundManager::playMusic(const std::string& filename, bool fade)
void SoundManager::stopMusic(float fadetime) void SoundManager::stopMusic(float fadetime)
{ {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
} }
SoundSourcePtr SoundManager::createSoundSource(const std::string& filename) SoundSourcePtr SoundManager::createSoundSource(const std::string& filename)

@ -28,16 +28,15 @@
class SoundManager class SoundManager
{ {
enum { enum {
MAX_CACHE_SIZE = 10000000 MAX_CACHE_SIZE = 100000,
POLL_DELAY = 300
}; };
public: public:
void init(); void init();
void terminate(); void terminate();
void audioThread(); void poll();
void update();
void preload(const std::string& filename); void preload(const std::string& filename);
@ -62,9 +61,6 @@ private:
StreamSoundSourcePtr m_musicSource; StreamSoundSourcePtr m_musicSource;
ALCdevice *m_device; ALCdevice *m_device;
ALCcontext *m_context; ALCcontext *m_context;
std::thread m_thread;
std::atomic<bool> m_run;
std::recursive_mutex m_mutex;
Boolean<false> m_musicEnabled; Boolean<false> m_musicEnabled;
Boolean<false> m_soundEnabled; Boolean<false> m_soundEnabled;
std::string m_currentMusic; std::string m_currentMusic;

@ -1,12 +1,23 @@
#!/bin/bash #!/bin/bash
# generates a otclient zip package for win32 # generates otclient zip package for win32
# by edubart :) # by edubart :)
protocol=860 # TODO: the following options
# --gitroot <giturl> - clone a different url, the default is git://github.com/edubart/otclient.git
# --platform <platform> - compile for i486-mingw32, i586-msvc-mingw32 or linux
# --branch <branch> - use sources from a specific branch, the default is master
# --name <name> - change .exe and folder name, the default is otclient
# --protocol <proto> - game protocol, the default is 860
# --suffix <suffix> - override suffix, the default is the date
# --copy-tibiafiles <folder> - copy Tibia.spr and Tibia.dat from folder
# --github-upload <url> - upload the zip to github
# --scp-upload <url> - upload the zip to a shell via scp
gitroot="git://github.com/edubart/otclient.git" gitroot="git://github.com/edubart/otclient.git"
gitname="otclient" name="otclient"
mingw32="i486-mingw32" protocol=860
spr_folder="$HOME/projects/otclient/modules/game_tibiafiles" platform="i486-mingw32"
tibiafiles_folder="$HOME/projects/otclient/modules/game_tibiafiles"
upload_to="root@myserver.com:/var/www/downloads/" upload_to="root@myserver.com:/var/www/downloads/"
pkg_suffix="-snapshot-`date +%Y%m%d`-protocol${protocol}-win32" pkg_suffix="-snapshot-`date +%Y%m%d`-protocol${protocol}-win32"
use_spr=false use_spr=false
@ -14,12 +25,12 @@ upload=false
make_jobs=8 make_jobs=8
srcdir=`pwd` srcdir=`pwd`
if [ -d $gitname ]; then if [ -d $name ]; then
cd $gitname cd $name
git pull || exit git pull || exit
else else
git clone $gitroot || exit git clone $gitroot $name || exit
cd $gitname cd $name
fi fi
gitdir=`pwd` gitdir=`pwd`
@ -32,17 +43,17 @@ else
mkdir -p build mkdir -p build
cd build cd build
cmake -DCMAKE_TOOLCHAIN_FILE=$gitdir/src/framework/cmake/${mingw32}_toolchain.cmake \ cmake -DCMAKE_TOOLCHAIN_FILE=$gitdir/src/framework/cmake/${platform}_toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \ -DCMAKE_BUILD_TYPE=Release \
-DBUILD_REVISION=$revision \ -DBUILD_REVISION=$revision \
-DPROTOCOL=$protocol \ -DPROTOCOL=$protocol \
.. || exit .. || exit
fi fi
make -j${make_jobs} || exit make "-j${make_jobs}" || exit
pkgdir="$gitname$pkg_suffix" pkgdir="$name$pkg_suffix"
pkgzip="$pkgdir.zip" pkgzip="$pkgdir.zip"
cd $srcdir cd $srcdir
@ -51,8 +62,8 @@ mkdir $pkgdir
cd $pkgdir cd $pkgdir
cp -R $gitdir/modules . cp -R $gitdir/modules .
cp $gitdir/build/$gitname.exe $gitname.exe cp $gitdir/build/*.exe .
cp $gitdir/build/$gitname.map $gitname.map cp $gitdir/build/*.map .
cp $gitdir/LICENSE . cp $gitdir/LICENSE .
cp $gitdir/README.rdoc . cp $gitdir/README.rdoc .

Loading…
Cancel
Save