# newgrf_sound.cpp

Go to the documentation of this file.

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