4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
393
|
3 |
* Copyright (c) 2005, 2007 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 |
|
|
24 |
procedure InitSound;
|
|
25 |
procedure ReleaseSound;
|
|
26 |
procedure SoundLoad;
|
351
|
27 |
procedure PlaySound(snd: TSound; infinite: boolean);
|
4
|
28 |
procedure PlayMusic;
|
282
|
29 |
procedure StopSound(snd: TSound);
|
371
|
30 |
function ChangeVolume(voldelta: LongInt): LongInt;
|
564
|
31 |
procedure InitPlaylistChunk(seed: LongWord);
|
4
|
32 |
|
|
33 |
implementation
|
|
34 |
uses uMisc, uConsole;
|
564
|
35 |
|
|
36 |
{$INCLUDE playlist.inc}
|
|
37 |
|
13
|
38 |
const chanTPU = 12;
|
564
|
39 |
var Mus: PMixMusic = nil;
|
|
40 |
CurrMusic: Longword = 0;
|
371
|
41 |
Volume: LongInt;
|
4
|
42 |
|
|
43 |
procedure InitSound;
|
|
44 |
begin
|
|
45 |
if not isSoundEnabled then exit;
|
|
46 |
WriteToConsole('Init sound...');
|
11
|
47 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
|
|
48 |
if isSoundEnabled then
|
|
49 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0;
|
4
|
50 |
if isSoundEnabled then WriteLnToConsole(msgOK)
|
|
51 |
else WriteLnToConsole(msgFailed);
|
13
|
52 |
Mix_AllocateChannels(Succ(chanTPU));
|
174
|
53 |
Mix_VolumeMusic(48);
|
|
54 |
|
|
55 |
Volume:= cInitVolume;
|
|
56 |
if Volume < 0 then Volume:= 0;
|
|
57 |
Volume:= Mix_Volume(-1, Volume)
|
4
|
58 |
end;
|
|
59 |
|
|
60 |
procedure ReleaseSound;
|
|
61 |
var i: TSound;
|
|
62 |
begin
|
|
63 |
for i:= Low(TSound) to High(TSound) do
|
|
64 |
Mix_FreeChunk(Soundz[i].id);
|
|
65 |
Mix_FreeMusic(Mus);
|
|
66 |
Mix_CloseAudio
|
|
67 |
end;
|
|
68 |
|
|
69 |
procedure SoundLoad;
|
|
70 |
var i: TSound;
|
355
|
71 |
s: shortstring;
|
4
|
72 |
begin
|
|
73 |
if not isSoundEnabled then exit;
|
|
74 |
for i:= Low(TSound) to High(TSound) do
|
|
75 |
begin
|
70
|
76 |
s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
|
355
|
77 |
WriteToConsole(msgLoading + s + ' ');
|
|
78 |
Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
|
4
|
79 |
TryDo(Soundz[i].id <> nil, msgFailed, true);
|
|
80 |
WriteLnToConsole(msgOK);
|
|
81 |
end;
|
|
82 |
end;
|
|
83 |
|
351
|
84 |
procedure PlaySound(snd: TSound; infinite: boolean);
|
371
|
85 |
var loops: LongInt;
|
4
|
86 |
begin
|
|
87 |
if not isSoundEnabled then exit;
|
282
|
88 |
if infinite then loops:= -1 else loops:= 0;
|
|
89 |
Soundz[snd].lastChan:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
|
4
|
90 |
end;
|
|
91 |
|
282
|
92 |
procedure StopSound(snd: TSound);
|
4
|
93 |
begin
|
|
94 |
if not isSoundEnabled then exit;
|
282
|
95 |
if Mix_Playing(Soundz[snd].lastChan) <> 0 then
|
|
96 |
Mix_HaltChannel(Soundz[snd].lastChan)
|
4
|
97 |
end;
|
|
98 |
|
|
99 |
procedure PlayMusic;
|
576
|
100 |
{$IFDEF HAVE_MUSIC}
|
564
|
101 |
var s: string;
|
576
|
102 |
{$ENDIF}
|
4
|
103 |
begin
|
564
|
104 |
{$IFDEF HAVE_MUSIC}
|
4
|
105 |
if not isSoundEnabled then exit;
|
564
|
106 |
if Mix_PlayingMusic() <> 0 then exit;
|
565
|
107 |
|
564
|
108 |
Mix_FreeMusic(Mus);
|
565
|
109 |
|
|
110 |
CurrMusic:= playlistchain[CurrMusic];
|
|
111 |
|
564
|
112 |
s:= PathPrefix + '/Music/' + playlist[CurrMusic];
|
|
113 |
WriteToConsole(msgLoading + s + ' ');
|
565
|
114 |
|
564
|
115 |
Mus:= Mix_LoadMUS(Str2PChar(s));
|
|
116 |
TryDo(Mus <> nil, msgFailed, false);
|
|
117 |
WriteLnToConsole(msgOK);
|
|
118 |
|
565
|
119 |
Mix_PlayMusic(Mus, 1)
|
564
|
120 |
{$ENDIF}
|
4
|
121 |
end;
|
|
122 |
|
371
|
123 |
function ChangeVolume(voldelta: LongInt): LongInt;
|
174
|
124 |
begin
|
181
|
125 |
if not isSoundEnabled then
|
351
|
126 |
exit(0);
|
|
127 |
|
174
|
128 |
inc(Volume, voldelta);
|
|
129 |
if Volume < 0 then Volume:= 0;
|
175
|
130 |
Mix_Volume(-1, Volume);
|
|
131 |
Volume:= Mix_Volume(-1, -1);
|
|
132 |
Mix_VolumeMusic(Volume * 3 div 8);
|
351
|
133 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME
|
174
|
134 |
end;
|
|
135 |
|
564
|
136 |
procedure InitPlaylistChunk(seed: LongWord);
|
576
|
137 |
{$IFDEF HAVE_MUSIC}
|
565
|
138 |
var i, t, nt: Longword;
|
576
|
139 |
{$ENDIF}
|
564
|
140 |
begin
|
|
141 |
{$IFDEF HAVE_MUSIC}
|
|
142 |
for i:= 0 to Pred(cPlayListLength) do
|
565
|
143 |
playlistchain[i]:= 0;
|
564
|
144 |
|
565
|
145 |
t:= 0;
|
|
146 |
for i:= 0 to cPlayListLength - 2 do
|
564
|
147 |
begin
|
565
|
148 |
repeat
|
|
149 |
seed:= seed * 1664525 + 1013904223;
|
|
150 |
nt:= seed mod cPlayListLength;
|
|
151 |
until (t <> nt) and (playlistchain[nt] = 0);
|
|
152 |
playlistchain[t]:= nt;
|
|
153 |
t:= nt;
|
564
|
154 |
end
|
|
155 |
{$ENDIF}
|
|
156 |
end;
|
|
157 |
|
4
|
158 |
end.
|