1 |
//====================================================================== |
/* |
2 |
// Copyright (C) 2002 Daniel Heck |
* Copyright (C) 2002,2003 Daniel Heck |
3 |
// |
* |
4 |
// This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
5 |
// modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
6 |
// as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
7 |
// of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
8 |
// |
* |
9 |
// This program is distributed in the hope that it will be useful, |
* This program is distributed in the hope that it will be useful, |
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
// GNU General Public License for more details. |
* GNU General Public License for more details. |
13 |
// |
* |
14 |
// You should have received a copy of the GNU General Public License along |
* You should have received a copy of the GNU General Public License along |
15 |
// with this program; if not, write to the Free Software Foundation, Inc., |
* with this program; if not, write to the Free Software Foundation, Inc., |
16 |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
17 |
//====================================================================== |
* |
18 |
|
* $Id$ |
19 |
|
*/ |
20 |
#include "enigma.hh" |
#include "enigma.hh" |
21 |
#include "options.hh" |
#include "options.hh" |
22 |
#include "sound.hh" |
#include "sound.hh" |
35 |
bool music_enabled = true; |
bool music_enabled = true; |
36 |
bool sound_initialized = false; |
bool sound_initialized = false; |
37 |
|
|
38 |
int sound_freq = MIX_DEFAULT_FREQUENCY; |
int sound_freq = MIX_DEFAULT_FREQUENCY; |
39 |
Uint16 sound_format = MIX_DEFAULT_FORMAT; |
Uint16 sound_format = MIX_DEFAULT_FORMAT; |
40 |
int sound_channels = MIX_DEFAULT_CHANNELS; |
int sound_channels = MIX_DEFAULT_CHANNELS; |
41 |
|
|
42 |
|
Mix_Music *current_music = 0; |
43 |
|
string current_music_name; |
44 |
|
|
45 |
|
V2 listener_pos; |
46 |
|
|
47 |
px::Dict<Mix_Chunk*> wav_cache; |
px::Dict<Mix_Chunk*> wav_cache; |
48 |
} |
} |
82 |
{ |
{ |
83 |
if (sound_initialized) { |
if (sound_initialized) { |
84 |
Mix_CloseAudio(); |
Mix_CloseAudio(); |
85 |
|
sound_initialized = false; |
86 |
} |
} |
|
sound_initialized = false; |
|
87 |
} |
} |
88 |
|
|
89 |
void |
void |
90 |
sound::DisableSound() {sound_enabled = false;} |
sound::DisableSound() |
91 |
|
{ |
92 |
|
if (sound_enabled) { |
93 |
|
sound_enabled = false; |
94 |
|
Shutdown(); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
void |
void |
99 |
sound::EnableSound() {sound_enabled = true;} |
sound::EnableSound() |
100 |
|
{ |
101 |
|
if (!sound_enabled) { |
102 |
|
sound_enabled = true; |
103 |
|
Init(); |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
void |
void |
108 |
sound::DisableMusic() { music_enabled = false; } |
sound::DisableMusic() |
109 |
|
{ |
110 |
|
music_enabled = false; |
111 |
|
} |
112 |
|
|
113 |
Mix_Chunk * |
Mix_Chunk * |
114 |
cache_sound(const std::string &name) |
cache_sound(const std::string &name) |
127 |
} |
} |
128 |
|
|
129 |
void |
void |
130 |
sound::PlaySound(const char *name) |
sound::SetListenerPosition (const px::V2 &pos) |
131 |
|
{ |
132 |
|
listener_pos = pos; |
133 |
|
} |
134 |
|
|
135 |
|
void |
136 |
|
sound::PlaySound (const char *name, const px::V2 &pos) |
137 |
{ |
{ |
138 |
if (!sound_enabled) |
if (!sound_enabled) |
139 |
return; |
return; |
140 |
|
|
141 |
|
V2 v = pos-listener_pos; |
142 |
|
double vv=length(v); |
143 |
|
|
144 |
|
int left, right; |
145 |
|
|
146 |
|
left = int(255 - options::StereoSeparation*v[0]); |
147 |
|
right = int(255 + options::StereoSeparation*v[0]); |
148 |
|
|
149 |
|
left = Max(0, Min(left, 255)); |
150 |
|
right = Max(0, Min(right, 255)); |
151 |
|
|
152 |
|
double range = 100; // How far can sound travel? |
153 |
|
double volume = 1 - vv/range; |
154 |
if (Mix_Chunk *ch = cache_sound(name)) { |
if (Mix_Chunk *ch = cache_sound(name)) { |
155 |
int channel = Mix_PlayChannel(-1, ch, 0); |
int channel = Mix_PlayChannel(-1, ch, 0); |
156 |
int soundvol = int(options::SoundVolume * MIX_MAX_VOLUME); |
int soundvol = int(volume * options::SoundVolume * MIX_MAX_VOLUME); |
157 |
|
Mix_SetPanning(channel, left, right); |
158 |
Mix_Volume(channel, Max(0, Min(soundvol, MIX_MAX_VOLUME))); |
Mix_Volume(channel, Max(0, Min(soundvol, MIX_MAX_VOLUME))); |
159 |
} |
} |
160 |
} |
} |
161 |
|
|
162 |
namespace |
void |
163 |
|
sound::PlaySound (const char *name) |
164 |
{ |
{ |
165 |
Mix_Music *current_music = 0; |
PlaySound (name, listener_pos); |
|
string current_music_name; |
|
166 |
} |
} |
167 |
|
|
168 |
void |
void |