newgrf_sound.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "engine_base.h"
00014 #include "newgrf.h"
00015 #include "newgrf_engine.h"
00016 #include "newgrf_sound.h"
00017 #include "vehicle_base.h"
00018 #include "sound_func.h"
00019 #include "core/mem_func.hpp"
00020
00021 static SmallVector<SoundEntry, 8> _sounds;
00022
00023
00024
00025 SoundEntry *AllocateSound()
00026 {
00027 SoundEntry *sound = _sounds.Append();
00028 MemSetT(sound, 0);
00029 return sound;
00030 }
00031
00032
00033 void InitializeSoundPool()
00034 {
00035 _sounds.Clear();
00036
00037
00038 SndCopyToPool();
00039 }
00040
00041
00042 SoundEntry *GetSound(SoundID index)
00043 {
00044 if (index >= _sounds.Length()) return NULL;
00045 return &_sounds[index];
00046 }
00047
00048
00049 uint GetNumSounds()
00050 {
00051 return _sounds.Length();
00052 }
00053
00054
00055 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
00056 {
00057 const GRFFile *file = GetEngineGRF(v->engine_type);
00058 uint16 callback;
00059
00060
00061 if (file == NULL) return false;
00062
00063
00064 if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
00065
00066 callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
00067 if (callback == CALLBACK_FAILED) return false;
00068 if (callback >= ORIGINAL_SAMPLE_COUNT) callback += file->sound_offset - ORIGINAL_SAMPLE_COUNT;
00069
00070 if (callback < GetNumSounds()) SndPlayVehicleFx(callback, v);
00071 return true;
00072 }
00073
00074 bool PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
00075 {
00076 if (sound_id >= ORIGINAL_SAMPLE_COUNT) sound_id += file->sound_offset - ORIGINAL_SAMPLE_COUNT;
00077
00078 if (sound_id < GetNumSounds()) {
00079 SndPlayTileFx(sound_id, tile);
00080 return true;
00081 }
00082 return false;
00083 }