peter1138@4656: /* $Id$ */ peter1138@4656: belugas@6674: /** @file newgrf_sound.cpp */ belugas@6674: peter1138@4656: #include "stdafx.h" peter1138@4656: #include "openttd.h" matthijs@5216: #include "oldpool.h" rubidium@9282: #include "engine_func.h" peter1138@4656: #include "newgrf_callbacks.h" peter1138@4656: #include "newgrf_engine.h" peter1138@4656: #include "newgrf_sound.h" rubidium@8640: #include "vehicle_base.h" rubidium@8653: #include "sound_func.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 */ rubidium@6573: FileEntry *AllocateFileEntry() peter1138@4656: { tron@4975: if (_sound_count == GetSoundInternalPoolSize()) { rubidium@7897: if (!_SoundInternal_pool.AddBlockToPool()) return NULL; peter1138@4656: } peter1138@4656: tron@4975: return GetSoundInternal(_sound_count++); peter1138@4656: } peter1138@4656: peter1138@4656: rubidium@6573: void InitializeSoundPool() peter1138@4656: { rubidium@7897: _SoundInternal_pool.CleanPool(); 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: { Darkvater@6450: if (index >= GetNumSounds()) return NULL; tron@4975: return GetSoundInternal(index); peter1138@4656: } peter1138@4656: peter1138@4656: rubidium@6573: uint GetNumSounds() 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 */ skidd13@8424: if (!HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_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: rubidium@5838: if (callback < GetNumSounds()) SndPlayVehicleFx((SoundFx)callback, v); peter1138@4656: return true; peter1138@4656: } maedhros@6658: maedhros@6658: bool PlayHouseSound(uint16 sound_id, TileIndex tile) maedhros@6658: { maedhros@6658: if (sound_id < GetNumOriginalSounds()) { maedhros@6658: SndPlayTileFx((SoundFx)sound_id, tile); maedhros@6658: return true; maedhros@6658: } maedhros@6658: return false; maedhros@6658: }