# newgrf_sound.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    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
    00020 static SmallVector<SoundEntry, 8> _sounds;
    00021
    00022
    00023 /* Allocate a new Sound */
    00024 SoundEntry *AllocateSound()
    00025 {
    00026   SoundEntry *sound = _sounds.Append();
    00027   MemSetT(sound, 0);
    00028   return sound;
    00029 }
    00030
    00031
    00032 void InitializeSoundPool()
    00033 {
    00034   _sounds.Clear();
    00035
    00036   /* Copy original sound data to the pool */
    00037   SndCopyToPool();
    00038 }
    00039
    00040
    00041 SoundEntry *GetSound(SoundID index)
    00042 {
    00043   if (index >= _sounds.Length()) return NULL;
    00044   return &_sounds[index];
    00045 }
    00046
    00047
    00048 uint GetNumSounds()
    00049 {
    00050   return _sounds.Length();
    00051 }
    00052
    00053
    00054 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
    00055 {
    00056   const GRFFile *file = GetEngineGRF(v->engine_type);
    00057   uint16 callback;
    00058
    00059   /* If the engine has no GRF ID associated it can't ever play any new sounds */
    00060   if (file == NULL) return false;
    00061
    00062   /* Check that the vehicle type uses the sound effect callback */
    00063   if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
    00064
    00065   callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
    00066   if (callback == CALLBACK_FAILED) return false;
    00067   if (callback >= ORIGINAL_SAMPLE_COUNT) {
    00068     callback -= ORIGINAL_SAMPLE_COUNT;
    00069     if (callback > file->num_sounds) return false;
    00070     callback += file->sound_offset;
    00071   }
    00072
    00073   assert(callback < GetNumSounds());
    00074   SndPlayVehicleFx(callback, v);
    00075   return true;
    00076 }
    00077
    00078 bool PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
    00079 {
    00080   if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
    00081     sound_id -= ORIGINAL_SAMPLE_COUNT;
    00082     if (sound_id > file->num_sounds) return false;
    00083     sound_id += file->sound_offset;
    00084   }
    00085
    00086   assert(sound_id < GetNumSounds());
    00087   SndPlayTileFx(sound_id, tile);
    00088   return true;
    00089 }