# newgrf_sound.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_sound.cpp 11719 2007-12-29 09:24:26Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "oldpool.h"
    00008 #include "engine.h"
    00009 #include "newgrf_callbacks.h"
    00010 #include "newgrf_engine.h"
    00011 #include "newgrf_sound.h"
    00012 #include "vehicle_base.h"
    00013 #include "sound_func.h"
    00014
    00015 static uint _sound_count = 0;
    00016 STATIC_OLD_POOL(SoundInternal, FileEntry, 3, 1000, NULL, NULL)
    00017
    00018
    00019 /* Allocate a new FileEntry */
    00020 FileEntry *AllocateFileEntry()
    00021 {
    00022   if (_sound_count == GetSoundInternalPoolSize()) {
    00023     if (!_SoundInternal_pool.AddBlockToPool()) return NULL;
    00024   }
    00025
    00026   return GetSoundInternal(_sound_count++);
    00027 }
    00028
    00029
    00030 void InitializeSoundPool()
    00031 {
    00032   _SoundInternal_pool.CleanPool();
    00033   _sound_count = 0;
    00034
    00035   /* Copy original sound data to the pool */
    00036   SndCopyToPool();
    00037 }
    00038
    00039
    00040 FileEntry *GetSound(uint index)
    00041 {
    00042   if (index >= GetNumSounds()) return NULL;
    00043   return GetSoundInternal(index);
    00044 }
    00045
    00046
    00047 uint GetNumSounds()
    00048 {
    00049   return _sound_count;
    00050 }
    00051
    00052
    00053 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
    00054 {
    00055   const GRFFile *file = GetEngineGRF(v->engine_type);
    00056   uint16 callback;
    00057
    00058   /* If the engine has no GRF ID associated it can't ever play any new sounds */
    00059   if (file == NULL) return false;
    00060
    00061   /* Check that the vehicle type uses the sound effect callback */
    00062   if (!HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_SOUND_EFFECT)) return false;
    00063
    00064   callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
    00065   if (callback == CALLBACK_FAILED) return false;
    00066   if (callback >= GetNumOriginalSounds()) callback += file->sound_offset - GetNumOriginalSounds();
    00067
    00068   if (callback < GetNumSounds()) SndPlayVehicleFx((SoundFx)callback, v);
    00069   return true;
    00070 }
    00071
    00072 bool PlayHouseSound(uint16 sound_id, TileIndex tile)
    00073 {
    00074   if (sound_id < GetNumOriginalSounds()) {
    00075     SndPlayTileFx((SoundFx)sound_id, tile);
    00076     return true;
    00077   }
    00078   return false;
    00079 }