diff options
-rw-r--r-- | common/engine.cpp | 2 | ||||
-rw-r--r-- | common/timer.cpp | 52 | ||||
-rw-r--r-- | common/timer.h | 11 | ||||
-rw-r--r-- | scumm/imuse_digi.cpp | 12 | ||||
-rw-r--r-- | scumm/player_v3a.cpp | 11 | ||||
-rw-r--r-- | scumm/smush/smush_player.cpp | 6 | ||||
-rw-r--r-- | scumm/sound.cpp | 13 | ||||
-rw-r--r-- | sky/sky.cpp | 6 | ||||
-rw-r--r-- | sword2/driver/d_sound.cpp | 7 | ||||
-rw-r--r-- | sword2/driver/d_sound.h | 2 |
10 files changed, 62 insertions, 60 deletions
diff --git a/common/engine.cpp b/common/engine.cpp index 32bb73a9ac..b26338ed0f 100644 --- a/common/engine.cpp +++ b/common/engine.cpp @@ -41,7 +41,7 @@ Engine::Engine(GameDetector *detector, OSystem *syst) g_system = _system; // FIXME - BIG HACK for MidiEmu - _timer = new Timer(this); + _timer = new Timer(_system); } Engine::~Engine() { diff --git a/common/timer.cpp b/common/timer.cpp index 8ce5b809b2..243eba2872 100644 --- a/common/timer.cpp +++ b/common/timer.cpp @@ -23,16 +23,17 @@ #include "stdafx.h" #include "common/scummsys.h" #include "common/timer.h" +#include "common/util.h" static Timer *g_timer = NULL; -Timer::Timer(Engine * engine) : - _engine(engine), +Timer::Timer(OSystem *system) : + _system(system), _mutex(0), _timerHandler(0), _lastTime(0) { - _mutex = _engine->_system->create_mutex(); + _mutex = _system->create_mutex(); g_timer = this; @@ -42,23 +43,25 @@ Timer::Timer(Engine * engine) : _timerSlots[i].counter = 0; } - _thisTime = _engine->_system->get_msecs(); + _thisTime = _system->get_msecs(); // Set the timer last, after everything has been initialised - _engine->_system->set_timer(10, &timer_handler); + _system->set_timer(10, &timer_handler); } Timer::~Timer() { - _engine->_system->set_timer(0, NULL); - - _engine->_system->lock_mutex(_mutex); - for (int i = 0; i < MAX_TIMERS; i++) { - _timerSlots[i].procedure = NULL; - _timerSlots[i].interval = 0; - _timerSlots[i].counter = 0; + _system->set_timer(0, NULL); + + { + StackLock lock(_mutex); + for (int i = 0; i < MAX_TIMERS; i++) { + _timerSlots[i].procedure = NULL; + _timerSlots[i].interval = 0; + _timerSlots[i].counter = 0; + } } - _engine->_system->unlock_mutex(_mutex); + // FIXME: There is still a potential race condition here, depending on how // the system backend implements set_timer: If timers are done using @@ -68,7 +71,7 @@ Timer::~Timer() { // it is still waiting for the _mutex. So, again depending on the backend, // we might end up unlocking the mutex then immediately deleting it, while // the timer thread is about to lock it. - _engine->_system->delete_mutex(_mutex); + _system->delete_mutex(_mutex); } int Timer::timer_handler(int t) { @@ -78,12 +81,11 @@ int Timer::timer_handler(int t) { } int Timer::handler(int t) { + StackLock lock(_mutex); uint32 interval, l; - _engine->_system->lock_mutex(_mutex); - _lastTime = _thisTime; - _thisTime = _engine->_system->get_msecs(); + _thisTime = _system->get_msecs(); interval = 1000 * (_thisTime - _lastTime); for (l = 0; l < MAX_TIMERS; l++) { @@ -91,31 +93,29 @@ int Timer::handler(int t) { _timerSlots[l].counter -= interval; if (_timerSlots[l].counter <= 0) { _timerSlots[l].counter += _timerSlots[l].interval; - _timerSlots[l].procedure(_engine); + _timerSlots[l].procedure(_timerSlots[l].refCon); } } } - _engine->_system->unlock_mutex(_mutex); - return t; } -bool Timer::installProcedure (TimerProc procedure, int32 interval) { +bool Timer::installProcedure(TimerProc procedure, int32 interval, void *refCon) { + StackLock lock(_mutex); int32 l; bool found = false; - _engine->_system->lock_mutex(_mutex); for (l = 0; l < MAX_TIMERS; l++) { if (!_timerSlots[l].procedure) { _timerSlots[l].procedure = procedure; _timerSlots[l].interval = interval; _timerSlots[l].counter = interval; + _timerSlots[l].refCon = refCon; found = true; break; } } - _engine->_system->unlock_mutex(_mutex); if (!found) warning("Couldn't find free timer slot!"); @@ -123,18 +123,18 @@ bool Timer::installProcedure (TimerProc procedure, int32 interval) { return found; } -void Timer::releaseProcedure (TimerProc procedure) { +void Timer::releaseProcedure(TimerProc procedure) { + StackLock lock(_mutex); int32 l; - _engine->_system->lock_mutex(_mutex); for (l = 0; l < MAX_TIMERS; l++) { if (_timerSlots[l].procedure == procedure) { _timerSlots[l].procedure = 0; _timerSlots[l].interval = 0; _timerSlots[l].counter = 0; + _timerSlots[l].refCon = 0; } } - _engine->_system->unlock_mutex(_mutex); } #endif diff --git a/common/timer.h b/common/timer.h index 7d194e47d9..8299b193f7 100644 --- a/common/timer.h +++ b/common/timer.h @@ -22,11 +22,11 @@ #define COMMON_TIMER_H #include "common/scummsys.h" -#include "common/engine.h" +#include "common/system.h" #define MAX_TIMERS 5 -typedef void (*TimerProc)(void *); +typedef void (*TimerProc)(void *refCon); #ifdef __MORPHOS__ #include "morphos_timer.h" @@ -35,7 +35,7 @@ typedef void (*TimerProc)(void *); class Timer { private: - Engine *_engine; + OSystem *_system; OSystem::MutexRef _mutex; void *_timerHandler; int32 _thisTime; @@ -45,13 +45,14 @@ private: TimerProc procedure; int32 interval; int32 counter; + void *refCon; } _timerSlots[MAX_TIMERS]; public: - Timer(Engine *engine); + Timer(OSystem *system); ~Timer(); - bool installProcedure(TimerProc procedure, int32 interval); + bool installProcedure(TimerProc procedure, int32 interval, void *refCon); void releaseProcedure(TimerProc procedure); protected: diff --git a/scumm/imuse_digi.cpp b/scumm/imuse_digi.cpp index 8f5aa64a60..5330289f18 100644 --- a/scumm/imuse_digi.cpp +++ b/scumm/imuse_digi.cpp @@ -689,11 +689,9 @@ static byte *readCreativeVocFile(byte *ptr, int32 &size, int &rate) { return ret_sound; } -void IMuseDigital::timer_handler(void *engine) { - // Avoid race condition - Scumm *scumm = (Scumm *)engine; - if (scumm && scumm->_imuseDigital) - scumm->_imuseDigital->musicTimer(); +void IMuseDigital::timer_handler(void *refCon) { + IMuseDigital *imuseDigital = (IMuseDigital *)refCon; + imuseDigital->musicTimer(); } IMuseDigital::IMuseDigital(Scumm *scumm) @@ -702,7 +700,7 @@ IMuseDigital::IMuseDigital(Scumm *scumm) for (int l = 0; l < MAX_DIGITAL_CHANNELS; l++) { _channel[l]._mixerChannel = 0; } - _scumm->_timer->installProcedure(timer_handler, 200000); + _scumm->_timer->installProcedure(timer_handler, 200000, this); _pause = false; } @@ -717,7 +715,7 @@ IMuseDigital::~IMuseDigital() { void IMuseDigital::musicTimer() { int l = 0; - if (_pause) + if (_pause || !_scumm) return; for (l = 0; l < MAX_DIGITAL_CHANNELS;l ++) { diff --git a/scumm/player_v3a.cpp b/scumm/player_v3a.cpp index 0ceb439786..0d3c8599c6 100644 --- a/scumm/player_v3a.cpp +++ b/scumm/player_v3a.cpp @@ -56,7 +56,7 @@ Player_V3A::Player_V3A(Scumm *scumm) { _maxvol = 255; - scumm->_timer->installProcedure(timerHandler, 16666); + scumm->_timer->installProcedure(timerHandler, 16666, this); _isinit = false; } @@ -143,6 +143,7 @@ void Player_V3A::playSound (int nr, char *data, int size, int rate, int vol, int } void Player_V3A::startSound(int nr) { + assert(_scumm); byte *data = _scumm->getResourceAddress(rtSound, nr); assert(data); @@ -228,10 +229,10 @@ void Player_V3A::startSound(int nr) { } } -void Player_V3A::timerHandler(void *engine) { - Scumm *scumm = (Scumm *)engine; - if (scumm && scumm->_playerV3A) - scumm->_playerV3A->playMusic(); +void Player_V3A::timerHandler(void *refCon) { + Player_V3A *player = (Player_V3A *)refCon; + assert(player); + player->playMusic(); } void Player_V3A::playMusic() { diff --git a/scumm/smush/smush_player.cpp b/scumm/smush/smush_player.cpp index fb99c8a668..1646ea5872 100644 --- a/scumm/smush/smush_player.cpp +++ b/scumm/smush/smush_player.cpp @@ -195,8 +195,8 @@ static StringResource *getStrings(const char *file, const char *directory, bool SmushPlayer *player; -void SmushPlayer::timerCallback(void *ptr) { - Scumm *scumm = (Scumm *)ptr; +void SmushPlayer::timerCallback(void *refCon) { + Scumm *scumm = (Scumm *)refCon; if (!scumm->_smushPlay) return; @@ -255,7 +255,7 @@ void SmushPlayer::init() { _smixer->_silentMixer = _scumm->_silentDigitalImuse; _scumm->_smushPlay = true; _dst = _scumm->virtscr[0].screenPtr + _scumm->virtscr[0].xstart; - _scumm->_timer->installProcedure(&timerCallback, _speed); + _scumm->_timer->installProcedure(&timerCallback, _speed, _scumm); _alreadyInit = false; } diff --git a/scumm/sound.cpp b/scumm/sound.cpp index 7748481376..d970ad5b04 100644 --- a/scumm/sound.cpp +++ b/scumm/sound.cpp @@ -1046,8 +1046,9 @@ uint32 Sound::decode12BitsSample(byte *src, byte **dst, uint32 size, bool stereo return s_size; } -static void music_handler (void *engine) { - g_scumm->_sound->bundleMusicHandler(g_scumm); +static void music_handler(void *refCon) { + Sound *sound = (Sound *)refCon; + sound->bundleMusicHandler(g_scumm); } void Sound::playBundleMusic(const char *song) { @@ -1088,7 +1089,7 @@ void Sound::playBundleMusic(const char *song) { _bundleMusicTrack = 0; _numberSamplesBundleMusic = _bundle->getNumberOfMusicSamplesByName(song); _nameBundleMusic = song; - _scumm->_timer->installProcedure(&music_handler, 1000000); + _scumm->_timer->installProcedure(&music_handler, 1000000, this); return; } if (strcmp(_nameBundleMusic, song) != 0) { @@ -1443,8 +1444,8 @@ void Sound::playSfxSound_Vorbis(void *sound, uint32 size, PlayingSoundHandle *ha // We use a real timer in an attempt to get better sync with CD tracks. This is // necessary for games like Loom CD. -static void cd_timer_handler(void *ptr) { - Scumm *scumm = (Scumm *) ptr; +static void cd_timer_handler(void *refCon) { + Scumm *scumm = (Scumm *)refCon; // FIXME: Turn off the timer when it's no longer needed. In theory, it // should be possible to check with pollCD(), but since CD sound isn't @@ -1467,7 +1468,7 @@ void Sound::startCDTimer() { timer_interval = 101; _scumm->_timer->releaseProcedure(&cd_timer_handler); - _scumm->_timer->installProcedure(&cd_timer_handler, 1000 * timer_interval); + _scumm->_timer->installProcedure(&cd_timer_handler, 1000 * timer_interval, _scumm); } void Sound::stopCDTimer() { diff --git a/sky/sky.cpp b/sky/sky.cpp index e8fcd62780..49ae6e07c6 100644 --- a/sky/sky.cpp +++ b/sky/sky.cpp @@ -284,7 +284,7 @@ void SkyState::initialise(void) { _skyMouse->useLogicInstance(_skyLogic); _timer = Engine::_timer; // initialize timer *after* _skyScreen has been initialized. - _timer->installProcedure(&timerHandler, 1000000 / 50); //call 50 times per second + _timer->installProcedure(&timerHandler, 1000000 / 50, this); //call 50 times per second _skyControl = new SkyControl(_skyScreen, _skyDisk, _skyMouse, _skyText, _skyMusic, _skyLogic, _skySound, _system, getSavePath()); _skyLogic->useControlInstance(_skyControl); @@ -381,9 +381,9 @@ void **SkyState::fetchItem(uint32 num) { return _itemList[num]; } -void SkyState::timerHandler(void *ptr) { +void SkyState::timerHandler(void *refCon) { - ((SkyState *)ptr)->gotTimerTick(); + ((SkyState *)refCon)->gotTimerTick(); } void SkyState::gotTimerTick(void) { diff --git a/sword2/driver/d_sound.cpp b/sword2/driver/d_sound.cpp index 750d81bb7b..c14892385d 100644 --- a/sword2/driver/d_sound.cpp +++ b/sword2/driver/d_sound.cpp @@ -203,8 +203,9 @@ int32 musicVolTable[17] = { -200, -100, -50, 0 }; */ -void sword2_sound_handler (void *engine) { - g_sword2->_sound->FxServer(); +void sword2_sound_handler(void *refCon) { + Sword2Sound *sound = (Sword2Sound *)refCon; + sound->FxServer(); } Sword2Sound::Sword2Sound(SoundMixer *mixer) { @@ -246,7 +247,7 @@ Sword2Sound::Sword2Sound(SoundMixer *mixer) { memset(bufferSizeFx, 0, sizeof(bufferSizeFx)); soundOn = 1; - g_engine->_timer->installProcedure(sword2_sound_handler, 100000); + g_engine->_timer->installProcedure(sword2_sound_handler, 100000, this); } Sword2Sound::~Sword2Sound() { diff --git a/sword2/driver/d_sound.h b/sword2/driver/d_sound.h index ab92403a98..42c18466e2 100644 --- a/sword2/driver/d_sound.h +++ b/sword2/driver/d_sound.h @@ -41,7 +41,7 @@ #include "sound/mixer.h" #include "common/file.h" -void sword2_sound_handler (void *engine); +extern void sword2_sound_handler(void *refCon); class Sword2Sound { public: |