author | unc0rr |
Fri, 12 Nov 2010 19:39:45 +0300 | |
branch | 0.9.9 |
changeset 1734 | ee7059418043 |
parent 1712 | f5b916de40f0 |
child 1777 | 88674c291331 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
1654 | 3 |
* Copyright (c) 2005, 2007, 2009 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uSound; |
|
20 |
interface |
|
21 |
uses SDLh, uConsts; |
|
22 |
{$INCLUDE options.inc} |
|
23 |
||
1654 | 24 |
type PVoicepack = ^TVoicepack; |
25 |
TVoicepack = record |
|
26 |
name: shortstring; |
|
27 |
chunks: array [TSound] of PMixChunk; |
|
28 |
end; |
|
29 |
||
4 | 30 |
procedure InitSound; |
31 |
procedure ReleaseSound; |
|
32 |
procedure SoundLoad; |
|
1669 | 33 |
procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack); |
4 | 34 |
procedure PlayMusic; |
1712 | 35 |
procedure PauseMusic; |
36 |
procedure ResumeMusic; |
|
282 | 37 |
procedure StopSound(snd: TSound); |
371 | 38 |
function ChangeVolume(voldelta: LongInt): LongInt; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
39 |
|
1654 | 40 |
function AskForVoicepack(name: shortstring): Pointer; |
4 | 41 |
|
1097 | 42 |
var MusicFN: shortstring = ''; |
43 |
||
4 | 44 |
implementation |
45 |
uses uMisc, uConsole; |
|
564 | 46 |
|
13 | 47 |
const chanTPU = 12; |
1712 | 48 |
var Volume: LongInt; |
1654 | 49 |
lastChan: array [TSound] of LongInt; |
50 |
voicepacks: array[0..cMaxTeams] of TVoicepack; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
51 |
defVoicepack: PVoicepack; |
1712 | 52 |
Mus: PMixMusic = nil; |
1654 | 53 |
|
54 |
function AskForVoicepack(name: shortstring): Pointer; |
|
55 |
var i: Longword; |
|
56 |
begin |
|
57 |
i:= 0; |
|
58 |
while (voicepacks[i].name <> name) and (voicepacks[i].name <> '') do |
|
59 |
begin |
|
60 |
inc(i); |
|
61 |
TryDo(i <= cMaxTeams, 'Engine bug: AskForVoicepack i > cMaxTeams', true) |
|
62 |
end; |
|
63 |
||
64 |
voicepacks[i].name:= name; |
|
65 |
AskForVoicepack:= @voicepacks[i] |
|
66 |
end; |
|
4 | 67 |
|
68 |
procedure InitSound; |
|
69 |
begin |
|
70 |
if not isSoundEnabled then exit; |
|
71 |
WriteToConsole('Init sound...'); |
|
11 | 72 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0; |
73 |
if isSoundEnabled then |
|
74 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0; |
|
4 | 75 |
if isSoundEnabled then WriteLnToConsole(msgOK) |
76 |
else WriteLnToConsole(msgFailed); |
|
13 | 77 |
Mix_AllocateChannels(Succ(chanTPU)); |
1137 | 78 |
if isMusicEnabled then Mix_VolumeMusic(50); |
174 | 79 |
|
80 |
Volume:= cInitVolume; |
|
81 |
if Volume < 0 then Volume:= 0; |
|
82 |
Volume:= Mix_Volume(-1, Volume) |
|
4 | 83 |
end; |
84 |
||
85 |
procedure ReleaseSound; |
|
86 |
var i: TSound; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
87 |
t: Longword; |
4 | 88 |
begin |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
89 |
for t:= 0 to cMaxTeams do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
90 |
if voicepacks[t].name <> '' then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
91 |
for i:= Low(TSound) to High(TSound) do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
92 |
if voicepacks[t].chunks[i] <> nil then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
93 |
Mix_FreeChunk(voicepacks[t].chunks[i]); |
1098 | 94 |
|
4 | 95 |
Mix_FreeMusic(Mus); |
96 |
Mix_CloseAudio |
|
97 |
end; |
|
98 |
||
99 |
procedure SoundLoad; |
|
100 |
var i: TSound; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
101 |
s: shortstring; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
102 |
t: Longword; |
4 | 103 |
begin |
104 |
if not isSoundEnabled then exit; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
105 |
|
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
106 |
defVoicepack:= AskForVoicepack('Default'); |
1654 | 107 |
|
4 | 108 |
for i:= Low(TSound) to High(TSound) do |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
109 |
if Soundz[i].Path <> ptVoices then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
110 |
begin |
1657
dde8f60d3e07
Fix small bug in voicepacks support in engine. It's complete and tested now.
unc0rr
parents:
1656
diff
changeset
|
111 |
s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
112 |
WriteToConsole(msgLoading + s + ' '); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
113 |
defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
114 |
TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
115 |
WriteLnToConsole(msgOK); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
116 |
end; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
117 |
|
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
118 |
for t:= 0 to cMaxTeams do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
119 |
if voicepacks[t].name <> '' then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
120 |
for i:= Low(TSound) to High(TSound) do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
121 |
if Soundz[i].Path = ptVoices then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
122 |
begin |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
123 |
s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
124 |
WriteToConsole(msgLoading + s + ' '); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
125 |
voicepacks[t].chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
126 |
if voicepacks[t].chunks[i] = nil then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
127 |
WriteLnToConsole(msgFailed) |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
128 |
else |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
129 |
WriteLnToConsole(msgOK) |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
130 |
end; |
4 | 131 |
end; |
132 |
||
1669 | 133 |
procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack); |
371 | 134 |
var loops: LongInt; |
4 | 135 |
begin |
1562
c0eea030347b
Don't play sounds when quick replaying in spectate mode
unc0rr
parents:
1225
diff
changeset
|
136 |
if (not isSoundEnabled) or fastUntilLag then exit; |
282 | 137 |
if infinite then loops:= -1 else loops:= 0; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
138 |
|
1669 | 139 |
if (voicepack <> nil) and (voicepack^.chunks[snd] <> nil) then |
140 |
lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], loops, -1) |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
141 |
else |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
142 |
lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], loops, -1) |
4 | 143 |
end; |
144 |
||
282 | 145 |
procedure StopSound(snd: TSound); |
4 | 146 |
begin |
147 |
if not isSoundEnabled then exit; |
|
1654 | 148 |
if Mix_Playing(lastChan[snd]) <> 0 then |
149 |
Mix_HaltChannel(lastChan[snd]) |
|
4 | 150 |
end; |
151 |
||
152 |
procedure PlayMusic; |
|
564 | 153 |
var s: string; |
4 | 154 |
begin |
1097 | 155 |
if (not isSoundEnabled) |
1128 | 156 |
or (MusicFN = '') |
1712 | 157 |
or (not isMusicEnabled) then exit; |
565 | 158 |
|
1097 | 159 |
s:= PathPrefix + '/Music/' + MusicFN; |
564 | 160 |
WriteToConsole(msgLoading + s + ' '); |
565 | 161 |
|
564 | 162 |
Mus:= Mix_LoadMUS(Str2PChar(s)); |
163 |
TryDo(Mus <> nil, msgFailed, false); |
|
164 |
WriteLnToConsole(msgOK); |
|
165 |
||
1225
f882a92ef872
Play music in menu also, with fading effects when run game
unc0rr
parents:
1137
diff
changeset
|
166 |
SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false) |
4 | 167 |
end; |
168 |
||
371 | 169 |
function ChangeVolume(voldelta: LongInt): LongInt; |
174 | 170 |
begin |
181 | 171 |
if not isSoundEnabled then |
1654 | 172 |
exit(0); |
351 | 173 |
|
174 | 174 |
inc(Volume, voldelta); |
175 |
if Volume < 0 then Volume:= 0; |
|
175 | 176 |
Mix_Volume(-1, Volume); |
177 |
Volume:= Mix_Volume(-1, -1); |
|
1137 | 178 |
if isMusicEnabled then Mix_VolumeMusic(Volume * 4 div 8); |
351 | 179 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME |
174 | 180 |
end; |
181 |
||
1712 | 182 |
procedure PauseMusic; |
183 |
begin |
|
184 |
if (MusicFN = '') or (not isMusicEnabled) then exit; |
|
185 |
||
186 |
Mix_PauseMusic(Mus); |
|
187 |
end; |
|
188 |
||
189 |
procedure ResumeMusic; |
|
190 |
begin |
|
191 |
if (MusicFN = '') or (not isMusicEnabled) then exit; |
|
192 |
||
193 |
Mix_ResumeMusic(Mus); |
|
194 |
end; |
|
195 |
||
4 | 196 |
end. |