src/newgrf_sound.cpp
author glx
Sat, 06 Oct 2007 22:30:24 +0000
branchnoai
changeset 9704 197cb8c6ae17
parent 9694 e72987579514
child 6871 5a9dc001e1ad
permissions -rw-r--r--
(svn r11221) [NoAI] -Sync: with trunk r11145:11220
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     1
/* $Id$ */
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     2
9505
9711235f5693 (svn r9396) [NoAI] -Sync with trunk r9362:9393
glx
parents: 9476
diff changeset
     3
/** @file newgrf_sound.cpp */
9711235f5693 (svn r9396) [NoAI] -Sync with trunk r9362:9393
glx
parents: 9476
diff changeset
     4
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     6
#include "openttd.h"
5216
d581e4db95b6 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 4975
diff changeset
     7
#include "oldpool.h"
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     8
#include "sound.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
     9
#include "engine.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    10
#include "vehicle.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    11
#include "newgrf_callbacks.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    12
#include "newgrf_engine.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    13
#include "newgrf_sound.h"
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    14
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    15
static uint _sound_count = 0;
5216
d581e4db95b6 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 4975
diff changeset
    16
STATIC_OLD_POOL(SoundInternal, FileEntry, 3, 1000, NULL, NULL)
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    17
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    18
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    19
/* Allocate a new FileEntry */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6450
diff changeset
    20
FileEntry *AllocateFileEntry()
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    21
{
4975
3a19f44fb5d4 (svn r6978) Use the pool macros for the SoundInternal pool
tron
parents: 4701
diff changeset
    22
	if (_sound_count == GetSoundInternalPoolSize()) {
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    23
		if (!_SoundInternal_pool.AddBlockToPool()) return NULL;
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    24
	}
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    25
4975
3a19f44fb5d4 (svn r6978) Use the pool macros for the SoundInternal pool
tron
parents: 4701
diff changeset
    26
	return GetSoundInternal(_sound_count++);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    27
}
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    28
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    29
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6450
diff changeset
    30
void InitializeSoundPool()
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    31
{
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9505
diff changeset
    32
	_SoundInternal_pool.CleanPool();
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    33
	_sound_count = 0;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    34
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    35
	/* Copy original sound data to the pool */
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    36
	SndCopyToPool();
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    37
}
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    38
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    39
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    40
FileEntry *GetSound(uint index)
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    41
{
6450
1c2016673250 (svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents: 5838
diff changeset
    42
	if (index >= GetNumSounds()) return NULL;
4975
3a19f44fb5d4 (svn r6978) Use the pool macros for the SoundInternal pool
tron
parents: 4701
diff changeset
    43
	return GetSoundInternal(index);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    44
}
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    45
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    46
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6450
diff changeset
    47
uint GetNumSounds()
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    48
{
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    49
	return _sound_count;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    50
}
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    51
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    52
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    53
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    54
{
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    55
	const GRFFile *file = GetEngineGRF(v->engine_type);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    56
	uint16 callback;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    57
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    58
	/* If the engine has no GRF ID associated it can't ever play any new sounds */
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    59
	if (file == NULL) return false;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    60
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    61
	/* Check that the vehicle type uses the sound effect callback */
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9694
diff changeset
    62
	if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_SOUND_EFFECT)) return false;
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    63
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    64
	callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    65
	if (callback == CALLBACK_FAILED) return false;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    66
	if (callback >= GetNumOriginalSounds()) callback += file->sound_offset - GetNumOriginalSounds();
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    67
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    68
	if (callback < GetNumSounds()) SndPlayVehicleFx((SoundFx)callback, v);
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    69
	return true;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
diff changeset
    70
}
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    71
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    72
bool PlayHouseSound(uint16 sound_id, TileIndex tile)
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    73
{
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    74
	if (sound_id < GetNumOriginalSounds()) {
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    75
		SndPlayTileFx((SoundFx)sound_id, tile);
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    76
		return true;
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    77
	}
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    78
	return false;
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6573
diff changeset
    79
}