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;
|
4
|
31 |
|
1097
|
32 |
var MusicFN: shortstring = '';
|
|
33 |
|
4
|
34 |
implementation
|
|
35 |
uses uMisc, uConsole;
|
564
|
36 |
|
13
|
37 |
const chanTPU = 12;
|
564
|
38 |
var Mus: PMixMusic = nil;
|
611
|
39 |
Volume: LongInt;
|
4
|
40 |
|
|
41 |
procedure InitSound;
|
|
42 |
begin
|
|
43 |
if not isSoundEnabled then exit;
|
|
44 |
WriteToConsole('Init sound...');
|
11
|
45 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
|
|
46 |
if isSoundEnabled then
|
|
47 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0;
|
4
|
48 |
if isSoundEnabled then WriteLnToConsole(msgOK)
|
|
49 |
else WriteLnToConsole(msgFailed);
|
13
|
50 |
Mix_AllocateChannels(Succ(chanTPU));
|
1097
|
51 |
Mix_VolumeMusic(64);
|
174
|
52 |
|
|
53 |
Volume:= cInitVolume;
|
|
54 |
if Volume < 0 then Volume:= 0;
|
|
55 |
Volume:= Mix_Volume(-1, Volume)
|
4
|
56 |
end;
|
|
57 |
|
|
58 |
procedure ReleaseSound;
|
|
59 |
var i: TSound;
|
|
60 |
begin
|
|
61 |
for i:= Low(TSound) to High(TSound) do
|
1098
|
62 |
Mix_FreeChunk(Soundz[i].id);
|
|
63 |
|
4
|
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 |
|
4
|
127 |
end.
|