author | unc0rr |
Mon, 12 Jan 2009 21:49:10 +0000 | |
changeset 1654 | 9cfa6f23e767 |
parent 1562 | c0eea030347b |
child 1656 | 209cf0e2fc36 |
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; |
1654 | 37 |
function AskForVoicepack(name: shortstring): Pointer; |
4 | 38 |
|
1097 | 39 |
var MusicFN: shortstring = ''; |
40 |
||
4 | 41 |
implementation |
42 |
uses uMisc, uConsole; |
|
564 | 43 |
|
13 | 44 |
const chanTPU = 12; |
564 | 45 |
var Mus: PMixMusic = nil; |
1654 | 46 |
Volume: LongInt; |
47 |
lastChan: array [TSound] of LongInt; |
|
48 |
voicepacks: array[0..cMaxTeams] of TVoicepack; |
|
49 |
||
50 |
function AskForVoicepack(name: shortstring): Pointer; |
|
51 |
var i: Longword; |
|
52 |
begin |
|
53 |
i:= 0; |
|
54 |
while (voicepacks[i].name <> name) and (voicepacks[i].name <> '') do |
|
55 |
begin |
|
56 |
inc(i); |
|
57 |
TryDo(i <= cMaxTeams, 'Engine bug: AskForVoicepack i > cMaxTeams', true) |
|
58 |
end; |
|
59 |
||
60 |
voicepacks[i].name:= name; |
|
61 |
AskForVoicepack:= @voicepacks[i] |
|
62 |
end; |
|
4 | 63 |
|
64 |
procedure InitSound; |
|
65 |
begin |
|
66 |
if not isSoundEnabled then exit; |
|
67 |
WriteToConsole('Init sound...'); |
|
11 | 68 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0; |
69 |
if isSoundEnabled then |
|
70 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0; |
|
4 | 71 |
if isSoundEnabled then WriteLnToConsole(msgOK) |
72 |
else WriteLnToConsole(msgFailed); |
|
13 | 73 |
Mix_AllocateChannels(Succ(chanTPU)); |
1137 | 74 |
if isMusicEnabled then Mix_VolumeMusic(50); |
174 | 75 |
|
76 |
Volume:= cInitVolume; |
|
77 |
if Volume < 0 then Volume:= 0; |
|
78 |
Volume:= Mix_Volume(-1, Volume) |
|
4 | 79 |
end; |
80 |
||
81 |
procedure ReleaseSound; |
|
82 |
var i: TSound; |
|
83 |
begin |
|
84 |
for i:= Low(TSound) to High(TSound) do |
|
1098 | 85 |
Mix_FreeChunk(Soundz[i].id); |
86 |
||
4 | 87 |
Mix_FreeMusic(Mus); |
88 |
Mix_CloseAudio |
|
89 |
end; |
|
90 |
||
91 |
procedure SoundLoad; |
|
92 |
var i: TSound; |
|
355 | 93 |
s: shortstring; |
4 | 94 |
begin |
95 |
if not isSoundEnabled then exit; |
|
1654 | 96 |
AskForVoicepack('Default'); |
97 |
||
4 | 98 |
for i:= Low(TSound) to High(TSound) do |
1654 | 99 |
begin |
100 |
s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName; |
|
101 |
WriteToConsole(msgLoading + s + ' '); |
|
102 |
Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1); |
|
103 |
TryDo(Soundz[i].id <> nil, msgFailed, true); |
|
104 |
WriteLnToConsole(msgOK); |
|
105 |
end; |
|
4 | 106 |
end; |
107 |
||
351 | 108 |
procedure PlaySound(snd: TSound; infinite: boolean); |
371 | 109 |
var loops: LongInt; |
4 | 110 |
begin |
1562
c0eea030347b
Don't play sounds when quick replaying in spectate mode
unc0rr
parents:
1225
diff
changeset
|
111 |
if (not isSoundEnabled) or fastUntilLag then exit; |
282 | 112 |
if infinite then loops:= -1 else loops:= 0; |
1654 | 113 |
lastChan[snd]:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1) |
4 | 114 |
end; |
115 |
||
282 | 116 |
procedure StopSound(snd: TSound); |
4 | 117 |
begin |
118 |
if not isSoundEnabled then exit; |
|
1654 | 119 |
if Mix_Playing(lastChan[snd]) <> 0 then |
120 |
Mix_HaltChannel(lastChan[snd]) |
|
4 | 121 |
end; |
122 |
||
123 |
procedure PlayMusic; |
|
564 | 124 |
var s: string; |
4 | 125 |
begin |
1097 | 126 |
if (not isSoundEnabled) |
1128 | 127 |
or (MusicFN = '') |
128 |
or (not isMusicEnabled)then exit; |
|
565 | 129 |
|
1097 | 130 |
s:= PathPrefix + '/Music/' + MusicFN; |
564 | 131 |
WriteToConsole(msgLoading + s + ' '); |
565 | 132 |
|
564 | 133 |
Mus:= Mix_LoadMUS(Str2PChar(s)); |
134 |
TryDo(Mus <> nil, msgFailed, false); |
|
135 |
WriteLnToConsole(msgOK); |
|
136 |
||
1225
f882a92ef872
Play music in menu also, with fading effects when run game
unc0rr
parents:
1137
diff
changeset
|
137 |
SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false) |
4 | 138 |
end; |
139 |
||
371 | 140 |
function ChangeVolume(voldelta: LongInt): LongInt; |
174 | 141 |
begin |
181 | 142 |
if not isSoundEnabled then |
1654 | 143 |
exit(0); |
351 | 144 |
|
174 | 145 |
inc(Volume, voldelta); |
146 |
if Volume < 0 then Volume:= 0; |
|
175 | 147 |
Mix_Volume(-1, Volume); |
148 |
Volume:= Mix_Volume(-1, -1); |
|
1137 | 149 |
if isMusicEnabled then Mix_VolumeMusic(Volume * 4 div 8); |
351 | 150 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME |
174 | 151 |
end; |
152 |
||
4 | 153 |
end. |