aboutsummaryrefslogtreecommitdiff
path: root/engines/toon
diff options
context:
space:
mode:
Diffstat (limited to 'engines/toon')
-rw-r--r--engines/toon/audio.cpp124
-rw-r--r--engines/toon/audio.h32
-rw-r--r--engines/toon/script_func.cpp8
-rw-r--r--engines/toon/toon.cpp7
4 files changed, 151 insertions, 20 deletions
diff --git a/engines/toon/audio.cpp b/engines/toon/audio.cpp
index 302e45bb5f..c3ab2b2fd6 100644
--- a/engines/toon/audio.cpp
+++ b/engines/toon/audio.cpp
@@ -49,9 +49,18 @@ AudioManager::AudioManager(ToonEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixe
for (int32 i = 0; i < 4; i++)
_audioPacks[i] = 0;
- voiceMuted = false;
- musicMuted = false;
- sfxMuted = false;
+ for (int32 i = 0; i < 4; i++) {
+ _ambientSFXs[i]._delay = 0;
+ _ambientSFXs[i]._enabled = false;
+ _ambientSFXs[i]._id = -1;
+ _ambientSFXs[i]._channel = -1;
+ _ambientSFXs[i]._lastTimer = 0;
+ _ambientSFXs[i]._volume = 255;
+ }
+
+ _voiceMuted = false;
+ _musicMuted = false;
+ _sfxMuted = false;
}
AudioManager::~AudioManager(void) {
@@ -62,18 +71,18 @@ AudioManager::~AudioManager(void) {
void AudioManager::muteMusic(bool muted) {
setMusicVolume(muted ? 0 : 255);
- musicMuted = muted;
+ _musicMuted = muted;
}
void AudioManager::muteVoice(bool muted) {
if(voiceStillPlaying() && _channels[2]) {
_channels[2]->setVolume(muted ? 0 : 255);
}
- voiceMuted = muted;
+ _voiceMuted = muted;
}
void AudioManager::muteSfx(bool muted) {
- sfxMuted = muted;
+ _sfxMuted = muted;
}
void AudioManager::removeInstance(AudioStreamInstance *inst) {
@@ -125,7 +134,7 @@ void AudioManager::playMusic(Common::String dir, Common::String music) {
//if (!_channels[_currentMusicChannel])
// delete _channels[_currentMusicChannel];
_channels[_currentMusicChannel] = new AudioStreamInstance(this, _mixer, srs, true);
- _channels[_currentMusicChannel]->setVolume(musicMuted ? 0 : 255);
+ _channels[_currentMusicChannel]->setVolume(_musicMuted ? 0 : 255);
_channels[_currentMusicChannel]->play(true, Audio::Mixer::kMusicSoundType);
}
@@ -152,11 +161,11 @@ void AudioManager::playVoice(int32 id, bool genericVoice) {
_channels[2] = new AudioStreamInstance(this, _mixer, stream);
_channels[2]->play(false, Audio::Mixer::kSpeechSoundType);
- _channels[2]->setVolume(voiceMuted ? 0 : 255);
+ _channels[2]->setVolume(_voiceMuted ? 0 : 255);
}
-void AudioManager::playSFX(int32 id, int volume , bool genericSFX) {
+int32 AudioManager::playSFX(int32 id, int volume , bool genericSFX) {
debugC(4, kDebugAudio, "playSFX(%d, %d)", id, (genericSFX) ? 1 : 0);
// find a free SFX channel
@@ -168,16 +177,18 @@ void AudioManager::playSFX(int32 id, int volume , bool genericSFX) {
stream = _audioPacks[3]->getStream(id, true);
if (stream->size() == 0)
- return;
+ return -1;
for (int32 i = 3; i < 16; i++) {
if (!_channels[i]) {
_channels[i] = new AudioStreamInstance(this, _mixer, stream);
_channels[i]->play(false, Audio::Mixer::kSFXSoundType);
- _channels[i]->setVolume(sfxMuted ? 0 : volume);
- break;
+ _channels[i]->setVolume(_sfxMuted ? 0 : volume);
+ return i;
}
}
+
+ return -1;
}
void AudioManager::stopCurrentVoice() {
@@ -517,5 +528,94 @@ Common::SeekableReadStream *AudioStreamPackage::getStream(int32 id, bool ownMemo
}
}
+void AudioManager::startAmbientSFX(int32 id, int32 delay, int32 mode, int32 volume)
+{
+ int32 found = -1;
+ for (int32 i = 0; i < 4; i++) {
+ if (!_ambientSFXs[i]._enabled) {
+ found = i;
+ break;
+ }
+ }
+
+ if (found < 0)
+ return;
+
+ _ambientSFXs[found]._lastTimer = _vm->getOldMilli() - 1;
+ _ambientSFXs[found]._delay = delay;
+ _ambientSFXs[found]._enabled = true;
+ _ambientSFXs[found]._mode = mode;
+ _ambientSFXs[found]._volume = volume;
+ _ambientSFXs[found]._id = id;
+ updateAmbientSFX();
+
+}
+
+void AudioManager::setAmbientSFXVolume(int32 id, int volume) {
+ for (int32 i = 0; i < 4; i++) {
+ AudioAmbientSFX* ambient = &_ambientSFXs[i];
+ if (ambient->_id == id && ambient->_enabled) {
+ ambient->_volume = volume;
+ if (ambient->_channel >= 0 && _channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()) {
+ _channels[ambient->_channel]->setVolume(volume);
+ }
+ break;
+ }
+ }
+}
+
+void AudioManager::killAmbientSFX(int32 id)
+{
+ for (int32 i = 0; i < 4; i++) {
+ AudioAmbientSFX* ambient = &_ambientSFXs[i];
+ if (ambient->_id == id && ambient->_enabled) {
+ ambient->_enabled = false;
+ ambient->_id = -1;
+
+ if (_channels[ambient->_channel]) {
+ _channels[ambient->_channel]->stop(false);
+ }
+ }
+
+ }
+}
+
+void AudioManager::killAllAmbientSFX()
+{
+ for (int32 i = 0; i < 4; i++) {
+ AudioAmbientSFX* ambient = &_ambientSFXs[i];
+ if (ambient->_enabled) {
+ ambient->_enabled = false;
+ ambient->_id = -1;
+
+ if (_channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()) {
+ _channels[ambient->_channel]->stop(false);
+ }
+ }
+ }
+}
+
+void AudioManager::updateAmbientSFX()
+{
+ if (_vm->getMoviePlayer()->isPlaying()) return;
+
+ for (int32 i = 0; i < 4; i++) {
+ AudioAmbientSFX* ambient = &_ambientSFXs[i];
+ if (ambient->_enabled && (ambient->_channel < 0 || !(_channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()))) {
+ if(ambient->_mode == 1) {
+ if (_vm->randRange(0, 32767) < ambient->_delay) {
+ ambient->_channel = playSFX(ambient->_id, ambient->_volume, false);
+ }
+ } else {
+ if (_vm->getOldMilli() > ambient->_lastTimer) {
+ ambient->_channel = playSFX(ambient->_id, ambient->_volume, false);
+ ambient->_lastTimer = _vm->getOldMilli(); // + 60 * _vm->getTickLength() * ambient->_delay;
+ }
+ }
+ }
+ }
+}
+
+
} // End of namespace Toon
diff --git a/engines/toon/audio.h b/engines/toon/audio.h
index 535e909565..32295fdadd 100644
--- a/engines/toon/audio.h
+++ b/engines/toon/audio.h
@@ -109,6 +109,16 @@ protected:
ToonEngine *_vm;
};
+struct AudioAmbientSFX {
+ int32 _id;
+ int32 _volume;
+ int32 _lastTimer;
+ int32 _delay;
+ int32 _mode;
+ int32 _channel;
+ bool _enabled;
+};
+
class AudioManager {
public:
void removeInstance(AudioStreamInstance *inst); // called by destructor
@@ -120,16 +130,22 @@ public:
void playMusic(Common::String dir, Common::String music);
void playVoice(int32 id, bool genericVoice);
- void playSFX(int32 id, int volume, bool genericSFX);
+ int32 playSFX(int32 id, int volume, bool genericSFX);
void stopCurrentVoice();
void setMusicVolume(int32 volume);
void stopMusic();
void muteVoice(bool mute);
void muteMusic(bool mute);
void muteSfx(bool mute);
- bool isVoiceMuted() { return voiceMuted; }
- bool isMusicMuted() { return musicMuted; }
- bool isSfxMuted() { return sfxMuted; }
+ bool isVoiceMuted() { return _voiceMuted; }
+ bool isMusicMuted() { return _musicMuted; }
+ bool isSfxMuted() { return _sfxMuted; }
+
+ void startAmbientSFX(int32 id, int32 delay, int32 mode, int32 volume);
+ void killAmbientSFX(int32 id);
+ void killAllAmbientSFX();
+ void updateAmbientSFX();
+ void setAmbientSFXVolume(int32 id, int volume);
void closeAudioPack(int32 id);
bool loadAudioPack(int32 id, Common::String indexFile, Common::String packFile);
@@ -148,9 +164,11 @@ public:
Audio::Mixer *_mixer;
protected:
- bool voiceMuted;
- bool musicMuted;
- bool sfxMuted;
+ bool _voiceMuted;
+ bool _musicMuted;
+ bool _sfxMuted;
+
+ AudioAmbientSFX _ambientSFXs[4];
};
} // End of namespace Toon
diff --git a/engines/toon/script_func.cpp b/engines/toon/script_func.cpp
index 1f463d065b..be1301cec2 100644
--- a/engines/toon/script_func.cpp
+++ b/engines/toon/script_func.cpp
@@ -1054,18 +1054,26 @@ int32 ScriptFunc::sys_Cmd_Play_Sfx(EMCState *state) {
}
int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx(EMCState *state) {
+ //printf("Ambient Sfx : %d %d %d %d\n", stackPos(0), stackPos(1), stackPos(2), stackPos(3));
+ _vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
return 0;
}
int32 ScriptFunc::sys_Cmd_Kill_Ambient_Sfx(EMCState *state) {
+ //printf("Kill Sfx : %d \n", stackPos(0));
+ _vm->getAudioManager()->killAmbientSFX(stackPos(0));
return 0;
}
int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx_Plus(EMCState *state) {
+ //printf("Ambient Sfx Plus: %d %d %d %d %d %d %d %d\n", stackPos(0), stackPos(1), stackPos(2), stackPos(3), stackPos(4), stackPos(5), stackPos(6), stackPos(7));
+ _vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
return 0;
}
int32 ScriptFunc::sys_Cmd_Set_Ambient_Volume(EMCState *state) {
+ //printf("Ambient Volume : %d %d \n", stackPos(0), stackPos(1));
+ _vm->getAudioManager()->setAmbientSFXVolume(stackPos(0), stackPos(1));
return 0;
}
diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp
index 84a28ae990..c47c2c18df 100644
--- a/engines/toon/toon.cpp
+++ b/engines/toon/toon.cpp
@@ -161,7 +161,7 @@ void ToonEngine::waitForScriptStep() {
// Wait after a specified number of script steps when executing a script
// to lower CPU usage
if (++_scriptStep >= 40) {
- g_system->delayMillis(10);
+ g_system->delayMillis(1);
_scriptStep = 0;
}
}
@@ -359,6 +359,7 @@ void ToonEngine::update(int32 timeIncrement) {
updateTimer(timeIncrement);
updateTimers();
updateScrolling(false, timeIncrement);
+ _audioManager->updateAmbientSFX();
_animationManager->update(timeIncrement);
_cursorAnimationInstance->update(timeIncrement);
@@ -815,6 +816,7 @@ void ToonEngine::simpleUpdate(bool waitCharacterToTalk) {
updateAnimationSceneScripts(elapsedTime);
updateTimer(elapsedTime);
_animationManager->update(elapsedTime);
+ _audioManager->updateAmbientSFX();
render();
@@ -1574,6 +1576,8 @@ void ToonEngine::exitScene() {
strcpy(temp, createRoomFilename(Common::String::printf("%s.pak", _gameState->_locations[_gameState->_currentScene]._name).c_str()).c_str());
resources()->closePackage(temp);
+ _audioManager->killAllAmbientSFX();
+
_drew->stopWalk();
_flux->stopWalk();
@@ -2665,6 +2669,7 @@ void ToonEngine::newGame() {
}
}
+
void ToonEngine::playSFX(int32 id, int32 volume) {
if (id < 0)
_audioManager->playSFX(-id + 1, volume, true);