File: sfx.cpp

package info (click to toggle)
epiphany 0.7.0+0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,532 kB
  • sloc: cpp: 6,404; ansic: 141; makefile: 78; sh: 7
file content (205 lines) | stat: -rw-r--r-- 4,998 bytes parent folder | download | duplicates (6)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
                          sfx.cpp  -  description
                             -------------------
    begin                : Fri Aug 16 2002
    copyright            : (C) 2002 by Giuseppe D'Aqui'
    email                : kumber@tiscalinet.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License, Version 2, as published by  *
 *   the Free Software Foundation.                                   *
 *                                                                         *
 ***************************************************************************/

#include "dephine.h"
#include "sfx.h"
#include "sample.h"
#include <vector>

#include "SDL/SDL_mixer.h"

#include "resource_factory.h"


void Sample_Manager::init()
{
	if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) 
	{
    	printf("Mix_OpenAudio: %s\n", Mix_GetError());
    	exit(2);
	}
	
	//FIXME: we should auto-resize the vector
	m_samples.resize(25);

	// allocate 32 mixing channels
	Mix_AllocateChannels(32);

	Mix_GroupChannels(0, 1, m_channels_monsters);

	Mix_GroupChannels(2, 3, m_channels_boulders);

	Mix_GroupChannels(4, 5, m_channels_gems);

	Mix_GroupChannels(6, 7, m_channels_explosions);

	Mix_GroupChannel(8, m_channels_timealarm);
	
	load_samples();
	
	this->set_volume(Epiconfig::instance()->get_volume_sound());

}




void Sample_Manager::load_samples()
{

	Resource_Factory* res_factory=Resource_Factory::instance();

	std::string res_path(res_factory->get_resource_path());

	res_path+= "/sfx/";

	m_samples[SFX_BOULDER_FALL]=new Sample(Mix_LoadWAV((res_path+"boulder_fall.wav").c_str()));

	m_samples[SFX_EMERALD_EAT]=new Sample(Mix_LoadWAV((res_path+"emerald_eat.wav").c_str()));

	m_samples[SFX_SAPPHIRE_EAT]=new Sample(Mix_LoadWAV((res_path+"sapphire_eat.wav").c_str()));

	m_samples[SFX_GAME_GAMEOVER]=new Sample(Mix_LoadWAV((res_path+"gameover.wav").c_str()));

	m_samples[SFX_DOOR_PASS]=new Sample(Mix_LoadWAV((res_path+"door_pass.wav").c_str()));

	m_samples[SFX_EMERALD_FALL]=new Sample(Mix_LoadWAV((res_path+"gem_fall.wav").c_str()));

	m_samples[SFX_SAPPHIRE_FALL]=new Sample(Mix_LoadWAV((res_path+"gem_fall.wav").c_str()));

	m_samples[SFX_GRASS_EAT]=new Sample(Mix_LoadWAV((res_path+"grass_eat.wav").c_str()));

	m_samples[SFX_KEY_EAT]=new Sample(Mix_LoadWAV((res_path+"key_eat.wav").c_str()));

	m_samples[SFX_PLAYER_MOVE]=new Sample(Mix_LoadWAV((res_path+"player_move.wav").c_str()));

	m_samples[SFX_MONSTER_MOVE]=new Sample(Mix_LoadWAV((res_path+"monster_move.wav").c_str()));

	m_samples[SFX_WOOD_SMASH]=new Sample(Mix_LoadWAV((res_path+"wood_smash.wav").c_str()));

	m_samples[SFX_GAME_TIMEALARM]=new Sample(Mix_LoadWAV((res_path+"timealarm.wav").c_str()));

	m_samples[SFX_EXPLOSION]=new Sample(Mix_LoadWAV((res_path+"explosion.wav").c_str()));

}




void Sample_Manager::deinit()
{
	delete m_samples[SFX_BOULDER_FALL];
	delete 	m_samples[SFX_EMERALD_EAT];
	delete 	m_samples[SFX_SAPPHIRE_EAT];
	delete 	m_samples[SFX_GAME_GAMEOVER];
	delete 	m_samples[SFX_DOOR_PASS];
	delete 	m_samples[SFX_EMERALD_FALL];
	delete 	m_samples[SFX_SAPPHIRE_FALL];
	delete 	m_samples[SFX_GRASS_EAT];
	delete 	m_samples[SFX_KEY_EAT];
	delete 	m_samples[SFX_PLAYER_MOVE];
	delete 	m_samples[SFX_MONSTER_MOVE];
	delete 	m_samples[SFX_WOOD_SMASH];
	delete 	m_samples[SFX_GAME_TIMEALARM];
	delete 	m_samples[SFX_EXPLOSION];
	
	Epiconfig::instance()->set_volume_sound(this->get_volume());
}




Sample* Sample_Manager::get_sample(Sample_Type sfx)
{
	//FIXME: it should assert the existence of sample
	return m_samples[sfx];
	
}




void Sample_Manager::play(Sample_Type type)
{
	Sint32 channel_group=-1;
	switch(type)
	{
		case SFX_BOULDER_FALL:
			channel_group = m_channels_boulders;
			break;
		case SFX_MONSTER_MOVE:
			channel_group = m_channels_monsters;
			break;
		case SFX_EXPLOSION:
			channel_group = m_channels_explosions;
			break;
		case SFX_EMERALD_FALL:
		case SFX_SAPPHIRE_FALL:
			channel_group = m_channels_gems;
			break;
		case SFX_GAME_TIMEALARM:
			channel_group = m_channels_timealarm;
			break;
		default:
			break;
	}
	
	Sint32 available_channel = Mix_GroupAvailable(channel_group);
	if(available_channel >=0)
	{
		m_samples[type]->play(available_channel);
	}
}




Uint32 Sample_Manager::get_max_volume()
{
	return MIX_MAX_VOLUME/16;
}




void Sample_Manager::set_volume(Uint32 value)
{
	Mix_Volume(-1, value*16);
}




Uint32 Sample_Manager::get_volume()
{
	return Mix_Volume(-1, -1)/16;
}




Sample_Manager* Sample_Manager::_instance = 0;

Sample_Manager* Sample_Manager::instance()
{
	if(_instance==0)
	{
		_instance=new Sample_Manager();
		_instance->init();
	}
	return _instance;
}