peter1138@4656: /* $Id$ */ peter1138@4656: peter1138@4656: #include "stdafx.h" peter1138@4656: #include "openttd.h" matthijs@5216: #include "oldpool.h" peter1138@4656: #include "sound.h" peter1138@4656: #include "engine.h" peter1138@4656: #include "vehicle.h" peter1138@4656: #include "newgrf_callbacks.h" peter1138@4656: #include "newgrf_engine.h" peter1138@4656: #include "newgrf_sound.h" peter1138@4656: peter1138@4656: static uint _sound_count = 0; matthijs@5216: STATIC_OLD_POOL(SoundInternal, FileEntry, 3, 1000, NULL, NULL) peter1138@4656: peter1138@4656: peter1138@4656: /* Allocate a new FileEntry */ peter1138@4656: FileEntry *AllocateFileEntry(void) peter1138@4656: { tron@4975: if (_sound_count == GetSoundInternalPoolSize()) { tron@4975: if (!AddBlockToPool(&_SoundInternal_pool)) return NULL; peter1138@4656: } peter1138@4656: tron@4975: return GetSoundInternal(_sound_count++); peter1138@4656: } peter1138@4656: peter1138@4656: peter1138@4656: void InitializeSoundPool(void) peter1138@4656: { tron@4975: CleanPool(&_SoundInternal_pool); peter1138@4656: _sound_count = 0; peter1138@4656: peter1138@4656: /* Copy original sound data to the pool */ peter1138@4656: SndCopyToPool(); peter1138@4656: } peter1138@4656: peter1138@4656: peter1138@4656: FileEntry *GetSound(uint index) peter1138@4656: { peter1138@4656: if (index >= _sound_count) return NULL; tron@4975: return GetSoundInternal(index); peter1138@4656: } peter1138@4656: peter1138@4656: peter1138@4656: uint GetNumSounds(void) peter1138@4656: { peter1138@4656: return _sound_count; peter1138@4656: } peter1138@4656: peter1138@4656: peter1138@4656: bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event) peter1138@4656: { peter1138@4656: const GRFFile *file = GetEngineGRF(v->engine_type); peter1138@4656: uint16 callback; peter1138@4656: peter1138@4656: /* If the engine has no GRF ID associated it can't ever play any new sounds */ peter1138@4656: if (file == NULL) return false; peter1138@4656: peter1138@4656: /* Check that the vehicle type uses the sound effect callback */ peter1138@4656: if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_SOUND_EFFECT)) return false; peter1138@4656: peter1138@4656: callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v); peter1138@4656: if (callback == CALLBACK_FAILED) return false; peter1138@4656: if (callback >= GetNumOriginalSounds()) callback += file->sound_offset - GetNumOriginalSounds(); peter1138@4656: peter1138@4701: if (callback < GetNumSounds()) SndPlayVehicleFx(callback, v); peter1138@4656: return true; peter1138@4656: }