2006-09-27 18:17:01 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2009-06-24 21:33:11 +00:00
|
|
|
#include "newgrf.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#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
|
|
|
|
2010-02-03 17:25:56 +00:00
|
|
|
static SmallVector<SoundEntry, 8> _sounds;
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
/* Allocate a new Sound */
|
|
|
|
SoundEntry *AllocateSound()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
2009-05-28 16:43:16 +00:00
|
|
|
SoundEntry *sound = _sounds.Append();
|
|
|
|
MemSetT(sound, 0);
|
|
|
|
return sound;
|
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
|
|
|
{
|
2009-05-17 19:36:28 +00:00
|
|
|
_sounds.Clear();
|
2006-09-27 18:17:01 +00:00
|
|
|
|
|
|
|
/* Copy original sound data to the pool */
|
|
|
|
SndCopyToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
SoundEntry *GetSound(SoundID index)
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
2009-05-17 19:36:28 +00:00
|
|
|
if (index >= _sounds.Length()) return NULL;
|
|
|
|
return &_sounds[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
|
|
|
{
|
2009-05-17 19:36:28 +00:00
|
|
|
return _sounds.Length();
|
2006-09-27 18:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-02 11:24:39 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a NewGRF wants to play a different vehicle sound effect.
|
|
|
|
* @param v Vehicle to play sound effect for.
|
|
|
|
* @param event Trigger for the sound effect.
|
|
|
|
* @return false if the default sound effect shall be played instead.
|
|
|
|
*/
|
2006-09-27 18:17:01 +00:00
|
|
|
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
|
|
|
|
{
|
2011-11-01 00:23:41 +00:00
|
|
|
const GRFFile *file = v->GetGRF();
|
2006-09-27 18:17:01 +00:00
|
|
|
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 */
|
2009-09-14 12:22:57 +00:00
|
|
|
if (!HasBit(EngInfo(v->engine_type)->callback_mask, 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);
|
2011-07-02 11:24:39 +00:00
|
|
|
/* Play default sound if callback fails */
|
2006-09-27 18:17:01 +00:00
|
|
|
if (callback == CALLBACK_FAILED) return false;
|
2011-07-02 11:24:39 +00:00
|
|
|
|
2010-04-20 05:52:51 +00:00
|
|
|
if (callback >= ORIGINAL_SAMPLE_COUNT) {
|
|
|
|
callback -= ORIGINAL_SAMPLE_COUNT;
|
2011-07-02 11:24:39 +00:00
|
|
|
|
|
|
|
/* Play no sound if result is out of range */
|
|
|
|
if (callback > file->num_sounds) return true;
|
|
|
|
|
2010-04-20 05:52:51 +00:00
|
|
|
callback += file->sound_offset;
|
|
|
|
}
|
2006-09-27 18:17:01 +00:00
|
|
|
|
2010-04-20 05:52:51 +00:00
|
|
|
assert(callback < GetNumSounds());
|
|
|
|
SndPlayVehicleFx(callback, v);
|
2006-09-27 18:17:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2011-07-02 11:28:22 +00:00
|
|
|
/**
|
|
|
|
* Play a NewGRF sound effect at the location of a specfic tile.
|
|
|
|
* @param file NewGRF triggering the sound effect.
|
|
|
|
* @param sound_id Sound effect the NewGRF wants to play.
|
|
|
|
* @param tile Location of the effect.
|
|
|
|
*/
|
|
|
|
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2010-04-20 05:52:51 +00:00
|
|
|
if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
|
|
|
|
sound_id -= ORIGINAL_SAMPLE_COUNT;
|
2011-07-02 11:28:22 +00:00
|
|
|
if (sound_id > file->num_sounds) return;
|
2010-04-20 05:52:51 +00:00
|
|
|
sound_id += file->sound_offset;
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
2010-04-20 05:52:51 +00:00
|
|
|
|
|
|
|
assert(sound_id < GetNumSounds());
|
|
|
|
SndPlayTileFx(sound_id, tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|