newgrf_sound.c
changeset 4656 9c1d8c4d3e60
child 4701 9e83e5ae6de7
equal deleted inserted replaced
4655:2af9a0c4cec2 4656:9c1d8c4d3e60
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "pool.h"
       
     6 #include "sound.h"
       
     7 #include "engine.h"
       
     8 #include "vehicle.h"
       
     9 #include "newgrf_callbacks.h"
       
    10 #include "newgrf_engine.h"
       
    11 #include "newgrf_sound.h"
       
    12 
       
    13 enum {
       
    14 	SOUND_POOL_BLOCK_SIZE_BITS = 3, /* (1 << 3) == 8 items */
       
    15 	SOUND_POOL_MAX_BLOCKS      = 1000,
       
    16 };
       
    17 
       
    18 static uint _sound_count = 0;
       
    19 static MemoryPool _sound_pool = { "Sound", SOUND_POOL_MAX_BLOCKS, SOUND_POOL_BLOCK_SIZE_BITS, sizeof(FileEntry), NULL, NULL, 0, 0, NULL };
       
    20 
       
    21 
       
    22 /* Allocate a new FileEntry */
       
    23 FileEntry *AllocateFileEntry(void)
       
    24 {
       
    25 	if (_sound_count == _sound_pool.total_items) {
       
    26 		if (!AddBlockToPool(&_sound_pool)) return NULL;
       
    27 	}
       
    28 
       
    29 	return (FileEntry*)GetItemFromPool(&_sound_pool, _sound_count++);
       
    30 }
       
    31 
       
    32 
       
    33 void InitializeSoundPool(void)
       
    34 {
       
    35 	CleanPool(&_sound_pool);
       
    36 	_sound_count = 0;
       
    37 
       
    38 	/* Copy original sound data to the pool */
       
    39 	SndCopyToPool();
       
    40 }
       
    41 
       
    42 
       
    43 FileEntry *GetSound(uint index)
       
    44 {
       
    45 	if (index >= _sound_count) return NULL;
       
    46 	return (FileEntry*)GetItemFromPool(&_sound_pool, index);
       
    47 }
       
    48 
       
    49 
       
    50 uint GetNumSounds(void)
       
    51 {
       
    52 	return _sound_count;
       
    53 }
       
    54 
       
    55 
       
    56 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
       
    57 {
       
    58 	const GRFFile *file = GetEngineGRF(v->engine_type);
       
    59 	uint16 callback;
       
    60 
       
    61 	/* If the engine has no GRF ID associated it can't ever play any new sounds */
       
    62 	if (file == NULL) return false;
       
    63 
       
    64 	/* Check that the vehicle type uses the sound effect callback */
       
    65 	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_SOUND_EFFECT)) return false;
       
    66 
       
    67 	callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
       
    68 	if (callback == CALLBACK_FAILED) return false;
       
    69 	if (callback >= GetNumOriginalSounds()) callback += file->sound_offset - GetNumOriginalSounds();
       
    70 
       
    71 	SndPlayVehicleFx(callback, v);
       
    72 	return true;
       
    73 }