2006-09-27 18:17:01 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_sound.cpp Handling NewGRF provided sounds. */
|
2007-03-21 03:06:21 +00:00
|
|
|
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
2006-12-03 17:27:43 +00:00
|
|
|
#include "oldpool.h"
|
2008-03-31 00:17:39 +00:00
|
|
|
#include "engine_func.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_callbacks.h"
|
|
|
|
#include "newgrf_engine.h"
|
|
|
|
#include "newgrf_sound.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_base.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
static uint _sound_count = 0;
|
2006-12-03 17:27:43 +00:00
|
|
|
STATIC_OLD_POOL(SoundInternal, FileEntry, 3, 1000, NULL, NULL)
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Allocate a new FileEntry */
|
2007-03-07 11:47:46 +00:00
|
|
|
FileEntry *AllocateFileEntry()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
2006-10-28 11:32:12 +00:00
|
|
|
if (_sound_count == GetSoundInternalPoolSize()) {
|
2007-08-03 20:18:38 +00:00
|
|
|
if (!_SoundInternal_pool.AddBlockToPool()) return NULL;
|
2006-09-27 18:17:01 +00:00
|
|
|
}
|
|
|
|
|
2006-10-28 11:32:12 +00:00
|
|
|
return GetSoundInternal(_sound_count++);
|
2006-09-27 18:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeSoundPool()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
2007-08-03 20:18:38 +00:00
|
|
|
_SoundInternal_pool.CleanPool();
|
2006-09-27 18:17:01 +00:00
|
|
|
_sound_count = 0;
|
|
|
|
|
|
|
|
/* Copy original sound data to the pool */
|
|
|
|
SndCopyToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileEntry *GetSound(uint index)
|
|
|
|
{
|
2007-02-23 12:56:10 +00:00
|
|
|
if (index >= GetNumSounds()) return NULL;
|
2006-10-28 11:32:12 +00:00
|
|
|
return GetSoundInternal(index);
|
2006-09-27 18:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
uint GetNumSounds()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
|
|
|
return _sound_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
|
|
|
|
{
|
|
|
|
const GRFFile *file = GetEngineGRF(v->engine_type);
|
|
|
|
uint16 callback;
|
|
|
|
|
|
|
|
/* If the engine has no GRF ID associated it can't ever play any new sounds */
|
|
|
|
if (file == NULL) return false;
|
|
|
|
|
|
|
|
/* Check that the vehicle type uses the sound effect callback */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_SOUND_EFFECT)) return false;
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
|
|
|
|
if (callback == CALLBACK_FAILED) return false;
|
|
|
|
if (callback >= GetNumOriginalSounds()) callback += file->sound_offset - GetNumOriginalSounds();
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
if (callback < GetNumSounds()) SndPlayVehicleFx((SoundFx)callback, v);
|
2006-09-27 18:17:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-04-21 11:29:01 +00:00
|
|
|
bool PlayTileSound(const GRFFile *file, uint16 sound_id, TileIndex tile)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2008-04-21 11:29:01 +00:00
|
|
|
if (sound_id >= GetNumOriginalSounds()) sound_id += file->sound_offset - GetNumOriginalSounds();
|
|
|
|
|
|
|
|
if (sound_id < GetNumSounds()) {
|
2007-03-19 11:27:30 +00:00
|
|
|
SndPlayTileFx((SoundFx)sound_id, tile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|