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;
|
564
|
100 |
var s: string;
|
4
|
101 |
begin
|
564
|
102 |
{$IFDEF HAVE_MUSIC}
|
4
|
103 |
if not isSoundEnabled then exit;
|
564
|
104 |
if Mix_PlayingMusic() <> 0 then exit;
|
565
|
105 |
|
564
|
106 |
Mix_FreeMusic(Mus);
|
565
|
107 |
|
|
108 |
CurrMusic:= playlistchain[CurrMusic];
|
|
109 |
|
564
|
110 |
s:= PathPrefix + '/Music/' + playlist[CurrMusic];
|
|
111 |
WriteToConsole(msgLoading + s + ' ');
|
565
|
112 |
|
564
|
113 |
Mus:= Mix_LoadMUS(Str2PChar(s));
|
|
114 |
TryDo(Mus <> nil, msgFailed, false);
|
|
115 |
WriteLnToConsole(msgOK);
|
|
116 |
|
565
|
117 |
Mix_PlayMusic(Mus, 1)
|
564
|
118 |
{$ENDIF}
|
4
|
119 |
end;
|
|
120 |
|
371
|
121 |
function ChangeVolume(voldelta: LongInt): LongInt;
|
174
|
122 |
begin
|
181
|
123 |
if not isSoundEnabled then
|
351
|
124 |
exit(0);
|
|
125 |
|
174
|
126 |
inc(Volume, voldelta);
|
|
127 |
if Volume < 0 then Volume:= 0;
|
175
|
128 |
Mix_Volume(-1, Volume);
|
|
129 |
Volume:= Mix_Volume(-1, -1);
|
|
130 |
Mix_VolumeMusic(Volume * 3 div 8);
|
351
|
131 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME
|
174
|
132 |
end;
|
|
133 |
|
564
|
134 |
procedure InitPlaylistChunk(seed: LongWord);
|
565
|
135 |
var i, t, nt: Longword;
|
564
|
136 |
begin
|
|
137 |
{$IFDEF HAVE_MUSIC}
|
|
138 |
for i:= 0 to Pred(cPlayListLength) do
|
565
|
139 |
playlistchain[i]:= 0;
|
564
|
140 |
|
565
|
141 |
t:= 0;
|
|
142 |
for i:= 0 to cPlayListLength - 2 do
|
564
|
143 |
begin
|
565
|
144 |
repeat
|
|
145 |
seed:= seed * 1664525 + 1013904223;
|
|
146 |
nt:= seed mod cPlayListLength;
|
|
147 |
until (t <> nt) and (playlistchain[nt] = 0);
|
|
148 |
playlistchain[t]:= nt;
|
|
149 |
t:= nt;
|
564
|
150 |
end
|
|
151 |
{$ENDIF}
|
|
152 |
end;
|
|
153 |
|
4
|
154 |
end.
|