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