author | unc0rr |
Mon, 12 Jan 2009 21:57:41 +0000 | |
changeset 1656 | 209cf0e2fc36 |
parent 1654 | 9cfa6f23e767 |
child 1657 | dde8f60d3e07 |
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; |
|
351 | 33 |
procedure PlaySound(snd: TSound; infinite: boolean); |
4 | 34 |
procedure PlayMusic; |
282 | 35 |
procedure StopSound(snd: TSound); |
371 | 36 |
function ChangeVolume(voldelta: LongInt): LongInt; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
37 |
|
1654 | 38 |
function AskForVoicepack(name: shortstring): Pointer; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
39 |
procedure SetVoicePack(vp: PVoicePack); |
4 | 40 |
|
1097 | 41 |
var MusicFN: shortstring = ''; |
42 |
||
4 | 43 |
implementation |
44 |
uses uMisc, uConsole; |
|
564 | 45 |
|
13 | 46 |
const chanTPU = 12; |
564 | 47 |
var Mus: PMixMusic = nil; |
1654 | 48 |
Volume: LongInt; |
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; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
52 |
currentVP: PVoicepack; |
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 |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
68 |
procedure SetVoicePack(vp: PVoicePack); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
69 |
begin |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
70 |
currentVP:= vp |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
71 |
end; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
72 |
|
4 | 73 |
procedure InitSound; |
74 |
begin |
|
75 |
if not isSoundEnabled then exit; |
|
76 |
WriteToConsole('Init sound...'); |
|
11 | 77 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0; |
78 |
if isSoundEnabled then |
|
79 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0; |
|
4 | 80 |
if isSoundEnabled then WriteLnToConsole(msgOK) |
81 |
else WriteLnToConsole(msgFailed); |
|
13 | 82 |
Mix_AllocateChannels(Succ(chanTPU)); |
1137 | 83 |
if isMusicEnabled then Mix_VolumeMusic(50); |
174 | 84 |
|
85 |
Volume:= cInitVolume; |
|
86 |
if Volume < 0 then Volume:= 0; |
|
87 |
Volume:= Mix_Volume(-1, Volume) |
|
4 | 88 |
end; |
89 |
||
90 |
procedure ReleaseSound; |
|
91 |
var i: TSound; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
92 |
t: Longword; |
4 | 93 |
begin |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
94 |
for t:= 0 to cMaxTeams do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
95 |
if voicepacks[t].name <> '' then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
96 |
for i:= Low(TSound) to High(TSound) do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
97 |
if voicepacks[t].chunks[i] <> nil then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
98 |
Mix_FreeChunk(voicepacks[t].chunks[i]); |
1098 | 99 |
|
4 | 100 |
Mix_FreeMusic(Mus); |
101 |
Mix_CloseAudio |
|
102 |
end; |
|
103 |
||
104 |
procedure SoundLoad; |
|
105 |
var i: TSound; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
106 |
s: shortstring; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
107 |
t: Longword; |
4 | 108 |
begin |
109 |
if not isSoundEnabled then exit; |
|
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
110 |
|
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
111 |
defVoicepack:= AskForVoicepack('Default'); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
112 |
SetVoicePack(defVoicepack); |
1654 | 113 |
|
4 | 114 |
for i:= Low(TSound) to High(TSound) do |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
115 |
if Soundz[i].Path <> ptVoices then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
116 |
begin |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
117 |
s:= Pathz[Soundz[i].Path] + '/Default/' + Soundz[i].FileName; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
118 |
WriteToConsole(msgLoading + s + ' '); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
119 |
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
|
120 |
TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
121 |
WriteLnToConsole(msgOK); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
122 |
end; |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
123 |
|
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
124 |
for t:= 0 to cMaxTeams do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
125 |
if voicepacks[t].name <> '' then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
126 |
for i:= Low(TSound) to High(TSound) do |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
127 |
if Soundz[i].Path = ptVoices then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
128 |
begin |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
129 |
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
|
130 |
WriteToConsole(msgLoading + s + ' '); |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
131 |
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
|
132 |
if voicepacks[t].chunks[i] = nil then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
133 |
WriteLnToConsole(msgFailed) |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
134 |
else |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
135 |
WriteLnToConsole(msgOK) |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
136 |
end; |
4 | 137 |
end; |
138 |
||
351 | 139 |
procedure PlaySound(snd: TSound; infinite: boolean); |
371 | 140 |
var loops: LongInt; |
4 | 141 |
begin |
1562
c0eea030347b
Don't play sounds when quick replaying in spectate mode
unc0rr
parents:
1225
diff
changeset
|
142 |
if (not isSoundEnabled) or fastUntilLag then exit; |
282 | 143 |
if infinite then loops:= -1 else loops:= 0; |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
144 |
|
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
145 |
if currentVP^.chunks[snd] <> nil then |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
146 |
lastChan[snd]:= Mix_PlayChannelTimed(-1, currentVP^.chunks[snd], loops, -1) |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
147 |
else |
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1654
diff
changeset
|
148 |
lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], loops, -1) |
4 | 149 |
end; |
150 |
||
282 | 151 |
procedure StopSound(snd: TSound); |
4 | 152 |
begin |
153 |
if not isSoundEnabled then exit; |
|
1654 | 154 |
if Mix_Playing(lastChan[snd]) <> 0 then |
155 |
Mix_HaltChannel(lastChan[snd]) |
|
4 | 156 |
end; |
157 |
||
158 |
procedure PlayMusic; |
|
564 | 159 |
var s: string; |
4 | 160 |
begin |
1097 | 161 |
if (not isSoundEnabled) |
1128 | 162 |
or (MusicFN = '') |
163 |
or (not isMusicEnabled)then exit; |
|
565 | 164 |
|
1097 | 165 |
s:= PathPrefix + '/Music/' + MusicFN; |
564 | 166 |
WriteToConsole(msgLoading + s + ' '); |
565 | 167 |
|
564 | 168 |
Mus:= Mix_LoadMUS(Str2PChar(s)); |
169 |
TryDo(Mus <> nil, msgFailed, false); |
|
170 |
WriteLnToConsole(msgOK); |
|
171 |
||
1225
f882a92ef872
Play music in menu also, with fading effects when run game
unc0rr
parents:
1137
diff
changeset
|
172 |
SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false) |
4 | 173 |
end; |
174 |
||
371 | 175 |
function ChangeVolume(voldelta: LongInt): LongInt; |
174 | 176 |
begin |
181 | 177 |
if not isSoundEnabled then |
1654 | 178 |
exit(0); |
351 | 179 |
|
174 | 180 |
inc(Volume, voldelta); |
181 |
if Volume < 0 then Volume:= 0; |
|
175 | 182 |
Mix_Volume(-1, Volume); |
183 |
Volume:= Mix_Volume(-1, -1); |
|
1137 | 184 |
if isMusicEnabled then Mix_VolumeMusic(Volume * 4 div 8); |
351 | 185 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME |
174 | 186 |
end; |
187 |
||
4 | 188 |
end. |