summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/sound.cpp
blob: 855f2e60da966106e3176f318fcf11452df16ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "constants.h"
#include "sound.h"

Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS];
Mix_Music *SoundHandler :: music;
int SoundHandler :: soundChannels[NR_SOUNDS];
bool SoundHandler :: soundOn;
bool SoundHandler :: musicOn;

bool SoundHandler :: init( )
{
	// We're going to be requesting certain things from our audio
	// device, so we set them up beforehand
	int audio_rate = 22050;
	Uint16 audio_format = AUDIO_S16; //AUDIO_S16; /* 16-bit stereo */
	int audio_channels = 2;
	int audio_buffers = 1024;//4096;

	// This is where we open up our audio device.  Mix_OpenAudio takes
	// as its parameters the audio format we'd /like/ to have.
	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
	{
		printf("Unable to open audio!\n");
		return false;
	}

	// We're going to pre-load the sound effects that we need right here
	sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
	sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");

	music = 0;

	soundOn = true;
	musicOn = true;

	return true;
}

void SoundHandler :: cleanUp()
{
	// Free audio sounds
	if ( sounds[SND_EXPLOSION] )
    	Mix_FreeChunk( sounds[SND_EXPLOSION] );
	if ( sounds[SND_THRUST] )
    	Mix_FreeChunk( sounds[SND_THRUST] );

	if ( music )
		Mix_FreeMusic( music );

	Mix_CloseAudio();
}

int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
{
	if ( !soundOn )
		return -1;

	if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
		return -1;

	Mix_Chunk *chunk = sounds[soundNr];
    if( channel == -1 || !Mix_Playing( channel ) )
		channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);

	Mix_Volume( channel, MIX_MAX_VOLUME );
	return channel;
}

void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs )
{
	if ( !soundOn )
		return;

	if ( !fadeOut )
		Mix_HaltChannel( channel );
	else
	{
		Mix_FadeOutChannel( channel, nrMilliSecs );
	}
}

void SoundHandler :: playMusic( string musicFile )
{
	if ( !soundOn )
		return;

	// If music already exists - stop it playing if necessary and free it up
	if ( music )
	{
		stopMusic();
		Mix_FreeMusic( music );
	}

	// Load music
	music = Mix_LoadMUS( musicFile.c_str() );
	if(!music)
	{
		printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError());
		// this might be a critical error...
	}

	playMusic();
}

void SoundHandler :: playMusic( bool fade )
{
	if ( !musicOn )
		return;

	if ( music )
	{
		Mix_VolumeMusic( MIX_MAX_VOLUME );
		Mix_RewindMusic();

		if ( fade )
    		Mix_FadeInMusic( music, -1, 1000 );
        else
    		Mix_PlayMusic( music, -1 );

	}
}

void SoundHandler :: stopMusic( bool fadeOut )
{
	if ( !music || !Mix_PlayingMusic() )
		return;

	if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING )
	{
		Mix_FadeOutMusic( 1000 );
	}
	else
	{
		Mix_HaltMusic();
	}

}

void SoundHandler :: setMusicVolume( int vol )
{
    Mix_VolumeMusic( vol );
}

void SoundHandler :: setSoundsOn( bool val )
{
	soundOn = val;
}

void SoundHandler :: setMusicOn( bool val )
{
	musicOn = val;

	if ( !musicOn )
	   stopMusic();
    else
        playMusic( true );
}