diff options
| author | manuel <manuel@mausz.at> | 2011-05-11 17:38:29 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-05-11 17:38:29 +0200 |
| commit | ca29fc0babe8fc985a9e4656f80fc7faec4ac8a5 (patch) | |
| tree | fb48f74ffcddcd8b260ebf78062623427aeda862 /pacman-c++/common/audio.cpp | |
| parent | 535c342a2f28e0a1e90010b2f0ff4018eeeb200a (diff) | |
| download | foop-ca29fc0babe8fc985a9e4656f80fc7faec4ac8a5.tar.gz foop-ca29fc0babe8fc985a9e4656f80fc7faec4ac8a5.tar.bz2 foop-ca29fc0babe8fc985a9e4656f80fc7faec4ac8a5.zip | |
- fix audio plugin and make that a real interface
- that fixes a duplicate statis audiomanager (1x pacman, 1x audio plugin) on windows
- display won/lost dialog upon gameend
Diffstat (limited to 'pacman-c++/common/audio.cpp')
| -rw-r--r-- | pacman-c++/common/audio.cpp | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/pacman-c++/common/audio.cpp b/pacman-c++/common/audio.cpp index d29303d..eddbace 100644 --- a/pacman-c++/common/audio.cpp +++ b/pacman-c++/common/audio.cpp | |||
| @@ -23,7 +23,7 @@ AudioManager::AudioManager() | |||
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | preload(); | 25 | preload(); |
| 26 | if (!tryLoadPhononPlugin()) | 26 | if (!tryLoadAudioPlugin("*phononplayer*")) |
| 27 | { | 27 | { |
| 28 | qWarning() << "Unable to load audio plugin. Audio disabled.."; | 28 | qWarning() << "Unable to load audio plugin. Audio disabled.."; |
| 29 | m_players.append(new NoopAudioPlayer(this)); | 29 | m_players.append(new NoopAudioPlayer(this)); |
| @@ -31,6 +31,7 @@ AudioManager::AudioManager() | |||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | AudioPlayer *firstplayer = m_factory->create(this); | 33 | AudioPlayer *firstplayer = m_factory->create(this); |
| 34 | firstplayer->setWorking(m_working); | ||
| 34 | firstplayer->test(m_sounds[Sound::EatingFruit], Sound::length[Sound::EatingFruit] * 2); | 35 | firstplayer->test(m_sounds[Sound::EatingFruit], Sound::length[Sound::EatingFruit] * 2); |
| 35 | m_working = firstplayer->isWorking(); | 36 | m_working = firstplayer->isWorking(); |
| 36 | m_players.append(firstplayer); | 37 | m_players.append(firstplayer); |
| @@ -118,6 +119,33 @@ void AudioManager::unregisterAudioPlayer(AudioPlayer *player) | |||
| 118 | m_players.removeAll(player); | 119 | m_players.removeAll(player); |
| 119 | } | 120 | } |
| 120 | 121 | ||
| 122 | |||
| 123 | void AudioManager::unregisterAudioPlayer_helper(QObject *player) | ||
| 124 | { | ||
| 125 | unregisterAudioPlayer(static_cast<AudioPlayer *>(player)); | ||
| 126 | } | ||
| 127 | |||
| 128 | void AudioManager::play(Sound::Type sound, AudioPlayer *player) | ||
| 129 | { | ||
| 130 | if (player == NULL) | ||
| 131 | player = audioPlayer(); | ||
| 132 | player->play(this->sound(sound), length(sound)); | ||
| 133 | } | ||
| 134 | |||
| 135 | void AudioManager::setSource(Sound::Type sound, AudioPlayer *player) | ||
| 136 | { | ||
| 137 | if (player == NULL) | ||
| 138 | player = audioPlayer(); | ||
| 139 | player->setSource(this->sound(sound), length(sound)); | ||
| 140 | } | ||
| 141 | |||
| 142 | void AudioManager::enqueue(Sound::Type sound, AudioPlayer *player) | ||
| 143 | { | ||
| 144 | if (player == NULL) | ||
| 145 | player = audioPlayer(); | ||
| 146 | player->enqueue(this->sound(sound), length(sound)); | ||
| 147 | } | ||
| 148 | |||
| 121 | QFile *AudioManager::sound(Sound::Type sound) | 149 | QFile *AudioManager::sound(Sound::Type sound) |
| 122 | { | 150 | { |
| 123 | if (!isWorking()) | 151 | if (!isWorking()) |
| @@ -125,9 +153,9 @@ QFile *AudioManager::sound(Sound::Type sound) | |||
| 125 | return m_sounds.at(sound); | 153 | return m_sounds.at(sound); |
| 126 | } | 154 | } |
| 127 | 155 | ||
| 128 | void AudioManager::unregisterAudioPlayer_helper(QObject *player) | 156 | unsigned int AudioManager::length(Sound::Type sound) |
| 129 | { | 157 | { |
| 130 | unregisterAudioPlayer(static_cast<AudioPlayer *>(player)); | 158 | return Sound::length[sound]; |
| 131 | } | 159 | } |
| 132 | 160 | ||
| 133 | void AudioManager::preload() | 161 | void AudioManager::preload() |
| @@ -137,13 +165,14 @@ void AudioManager::preload() | |||
| 137 | Q_ASSERT(sounds.count() > 0); | 165 | Q_ASSERT(sounds.count() > 0); |
| 138 | for(unsigned i = 1; i <= sounds.count(); ++i) | 166 | for(unsigned i = 1; i <= sounds.count(); ++i) |
| 139 | m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), this)); | 167 | m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), this)); |
| 168 | Q_ASSERT(m_sounds.count() != (sizeof(Sound::length) / sizeof(unsigned int))); | ||
| 140 | } | 169 | } |
| 141 | 170 | ||
| 142 | bool AudioManager::tryLoadPhononPlugin() | 171 | bool AudioManager::tryLoadAudioPlugin(const QString &libraryname) |
| 143 | { | 172 | { |
| 144 | QDir dir = qApp->applicationDirPath(); | 173 | QDir dir = qApp->applicationDirPath(); |
| 145 | QStringList filters; | 174 | QStringList filters; |
| 146 | filters << "*phononplayer*"; | 175 | filters << libraryname; |
| 147 | foreach (QString file, dir.entryList(filters, QDir::Files)) | 176 | foreach (QString file, dir.entryList(filters, QDir::Files)) |
| 148 | { | 177 | { |
| 149 | file = dir.absoluteFilePath(file); | 178 | file = dir.absoluteFilePath(file); |
| @@ -163,25 +192,19 @@ bool AudioManager::tryLoadPhononPlugin() | |||
| 163 | return false; | 192 | return false; |
| 164 | } | 193 | } |
| 165 | 194 | ||
| 166 | AudioPlayer *AudioManager::createAudioPlayer() | 195 | AudioPlayer *AudioManager::createAudioPlayer(QObject *parent) |
| 167 | { | 196 | { |
| 168 | if (m_factory == NULL) | 197 | if (m_factory == NULL) |
| 169 | return NULL; | 198 | return NULL; |
| 170 | 199 | ||
| 171 | AudioPlayer *player = m_factory->create(this); | 200 | AudioPlayer *player = m_factory->create(parent != NULL ? parent : this); |
| 201 | player->setWorking(m_working); | ||
| 172 | registerAudioPlayer(player); | 202 | registerAudioPlayer(player); |
| 173 | return player; | 203 | return player; |
| 174 | } | 204 | } |
| 175 | 205 | ||
| 176 | /* --------------------------------------------------------------- */ | 206 | /* --------------------------------------------------------------- */ |
| 177 | 207 | ||
| 178 | AudioPlayer *NoopAudioPlayerFactory::create(QObject *parent) | ||
| 179 | { | ||
| 180 | return new FakeAudioPlayer(parent); | ||
| 181 | } | ||
| 182 | |||
| 183 | /* --------------------------------------------------------------- */ | ||
| 184 | |||
| 185 | NoopAudioPlayer::NoopAudioPlayer(QObject *parent) | 208 | NoopAudioPlayer::NoopAudioPlayer(QObject *parent) |
| 186 | : AudioPlayer(parent), m_working(false), m_muted(false), m_playing(false), m_paused(false) | 209 | : AudioPlayer(parent), m_working(false), m_muted(false), m_playing(false), m_paused(false) |
| 187 | {} | 210 | {} |
| @@ -209,19 +232,6 @@ void NoopAudioPlayer::play() | |||
| 209 | m_playing = false; | 232 | m_playing = false; |
| 210 | } | 233 | } |
| 211 | 234 | ||
| 212 | void NoopAudioPlayer::play(Sound::Type /* sound */) | ||
| 213 | { | ||
| 214 | play(); | ||
| 215 | } | ||
| 216 | |||
| 217 | bool NoopAudioPlayer::isPlaying() | ||
| 218 | { | ||
| 219 | return m_playing; | ||
| 220 | } | ||
| 221 | |||
| 222 | void NoopAudioPlayer::enqueue(Sound::Type /* sound */) | ||
| 223 | {} | ||
| 224 | |||
| 225 | void NoopAudioPlayer::pause() | 235 | void NoopAudioPlayer::pause() |
| 226 | { | 236 | { |
| 227 | m_paused = true; | 237 | m_paused = true; |
| @@ -258,7 +268,32 @@ void NoopAudioPlayer::prefinishMarkReached_ex(qint32 mark) | |||
| 258 | emit prefinishMarkReached(mark); | 268 | emit prefinishMarkReached(mark); |
| 259 | } | 269 | } |
| 260 | 270 | ||
| 261 | void NoopAudioPlayer::test(QFile * /* testsound */, qint32 /* length */) | 271 | void NoopAudioPlayer::seek(qint64 /* time */) |
| 272 | {} | ||
| 273 | |||
| 274 | void NoopAudioPlayer::setWorking(bool working) | ||
| 275 | { | ||
| 276 | m_working = working; | ||
| 277 | } | ||
| 278 | |||
| 279 | void NoopAudioPlayer::test(QFile * /* sound */, unsigned int /* length */) | ||
| 280 | {} | ||
| 281 | |||
| 282 | void NoopAudioPlayer::play(QFile *sound, unsigned int length) | ||
| 283 | { | ||
| 284 | setSource(sound, length); | ||
| 285 | play(); | ||
| 286 | } | ||
| 287 | |||
| 288 | void NoopAudioPlayer::setSource(QFile * /* sound */, unsigned int /* length */) | ||
| 289 | {} | ||
| 290 | |||
| 291 | bool NoopAudioPlayer::isPlaying() | ||
| 292 | { | ||
| 293 | return m_playing; | ||
| 294 | } | ||
| 295 | |||
| 296 | void NoopAudioPlayer::enqueue(QFile * /* sound */, unsigned int /* length */) | ||
| 262 | {} | 297 | {} |
| 263 | 298 | ||
| 264 | /* --------------------------------------------------------------- */ | 299 | /* --------------------------------------------------------------- */ |
| @@ -270,13 +305,13 @@ FakeAudioPlayer::FakeAudioPlayer(QObject *parent) | |||
| 270 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(finished_ex())); | 305 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(finished_ex())); |
| 271 | } | 306 | } |
| 272 | 307 | ||
| 273 | void FakeAudioPlayer::play(Sound::Type sound) | 308 | void FakeAudioPlayer::play(QFile * /* sound */, unsigned int length) |
| 274 | { | 309 | { |
| 275 | m_playing = true; | 310 | m_playing = true; |
| 276 | m_paused = false; | 311 | m_paused = false; |
| 277 | 312 | ||
| 278 | m_timer.setSingleShot(true); | 313 | m_timer.setSingleShot(true); |
| 279 | unsigned int interval = Sound::length[sound]; | 314 | unsigned int interval = length; |
| 280 | /* add a small delay server side only */ | 315 | /* add a small delay server side only */ |
| 281 | if (Constants::server) | 316 | if (Constants::server) |
| 282 | interval += Constants::tick; | 317 | interval += Constants::tick; |
| @@ -299,17 +334,15 @@ GaplessAudioPlayer::GaplessAudioPlayer(Sound::Type sound, qint32 mark, QObject * | |||
| 299 | if (!m_working) | 334 | if (!m_working) |
| 300 | return; | 335 | return; |
| 301 | 336 | ||
| 302 | m_player1 = AudioManager::self()->createAudioPlayer(); | 337 | m_player1 = AudioManager::self()->createAudioPlayer(this); |
| 303 | m_player2 = AudioManager::self()->createAudioPlayer(); | 338 | m_player2 = AudioManager::self()->createAudioPlayer(this); |
| 304 | 339 | ||
| 340 | /* this doesn't work reliable on all platforms (e.g. windows) */ | ||
| 305 | m_player2->setPrefinishMark(mark); | 341 | m_player2->setPrefinishMark(mark); |
| 306 | m_player1->setPrefinishMark(mark); | 342 | m_player1->setPrefinishMark(mark); |
| 307 | 343 | ||
| 308 | connect(m_player1, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer2())); | 344 | connect(m_player1, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer2())); |
| 309 | connect(m_player2, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer1())); | 345 | connect(m_player2, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer1())); |
| 310 | |||
| 311 | AudioManager::self()->registerAudioPlayer(m_player1); | ||
| 312 | AudioManager::self()->registerAudioPlayer(m_player2); | ||
| 313 | } | 346 | } |
| 314 | 347 | ||
| 315 | bool GaplessAudioPlayer::isWorking() const | 348 | bool GaplessAudioPlayer::isWorking() const |
| @@ -350,10 +383,12 @@ void GaplessAudioPlayer::pause() | |||
| 350 | 383 | ||
| 351 | void GaplessAudioPlayer::startPlayer1() | 384 | void GaplessAudioPlayer::startPlayer1() |
| 352 | { | 385 | { |
| 353 | m_player1->play(m_sound); | 386 | AudioManager::self()->play(m_sound, m_player1); |
| 387 | m_player2->pause(); | ||
| 354 | } | 388 | } |
| 355 | 389 | ||
| 356 | void GaplessAudioPlayer::startPlayer2() | 390 | void GaplessAudioPlayer::startPlayer2() |
| 357 | { | 391 | { |
| 358 | m_player2->play(m_sound); | 392 | AudioManager::self()->play(m_sound, m_player2); |
| 393 | m_player1->pause(); | ||
| 359 | } | 394 | } |
