4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
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 |
|
|
32 |
implementation
|
|
33 |
uses uMisc, uConsole;
|
13
|
34 |
const chanTPU = 12;
|
4
|
35 |
var Mus: PMixMusic;
|
371
|
36 |
Volume: LongInt;
|
4
|
37 |
|
|
38 |
procedure InitSound;
|
|
39 |
begin
|
|
40 |
if not isSoundEnabled then exit;
|
|
41 |
WriteToConsole('Init sound...');
|
11
|
42 |
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
|
|
43 |
if isSoundEnabled then
|
|
44 |
isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0;
|
4
|
45 |
if isSoundEnabled then WriteLnToConsole(msgOK)
|
|
46 |
else WriteLnToConsole(msgFailed);
|
13
|
47 |
Mix_AllocateChannels(Succ(chanTPU));
|
174
|
48 |
Mix_VolumeMusic(48);
|
|
49 |
|
|
50 |
Volume:= cInitVolume;
|
|
51 |
if Volume < 0 then Volume:= 0;
|
|
52 |
Volume:= Mix_Volume(-1, Volume)
|
4
|
53 |
end;
|
|
54 |
|
|
55 |
procedure ReleaseSound;
|
|
56 |
var i: TSound;
|
|
57 |
begin
|
|
58 |
for i:= Low(TSound) to High(TSound) do
|
|
59 |
Mix_FreeChunk(Soundz[i].id);
|
|
60 |
Mix_FreeMusic(Mus);
|
|
61 |
Mix_CloseAudio
|
|
62 |
end;
|
|
63 |
|
|
64 |
procedure SoundLoad;
|
|
65 |
var i: TSound;
|
355
|
66 |
s: shortstring;
|
4
|
67 |
begin
|
|
68 |
if not isSoundEnabled then exit;
|
|
69 |
for i:= Low(TSound) to High(TSound) do
|
|
70 |
begin
|
70
|
71 |
s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
|
355
|
72 |
WriteToConsole(msgLoading + s + ' ');
|
|
73 |
Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
|
4
|
74 |
TryDo(Soundz[i].id <> nil, msgFailed, true);
|
|
75 |
WriteLnToConsole(msgOK);
|
|
76 |
end;
|
|
77 |
|
287
|
78 |
s:= PathPrefix + '/Music/kahvi140a_alexander_chereshnev-illusion.ogg';
|
351
|
79 |
WriteToConsole(msgLoading + string(s) + ' ');
|
|
80 |
Mus:= Mix_LoadMUS(@s);
|
4
|
81 |
TryDo(Mus <> nil, msgFailed, false);
|
|
82 |
WriteLnToConsole(msgOK)
|
|
83 |
end;
|
|
84 |
|
351
|
85 |
procedure PlaySound(snd: TSound; infinite: boolean);
|
371
|
86 |
var loops: LongInt;
|
4
|
87 |
begin
|
|
88 |
if not isSoundEnabled then exit;
|
282
|
89 |
if infinite then loops:= -1 else loops:= 0;
|
|
90 |
Soundz[snd].lastChan:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
|
4
|
91 |
end;
|
|
92 |
|
282
|
93 |
procedure StopSound(snd: TSound);
|
4
|
94 |
begin
|
|
95 |
if not isSoundEnabled then exit;
|
282
|
96 |
if Mix_Playing(Soundz[snd].lastChan) <> 0 then
|
|
97 |
Mix_HaltChannel(Soundz[snd].lastChan)
|
4
|
98 |
end;
|
|
99 |
|
|
100 |
procedure PlayMusic;
|
|
101 |
begin
|
|
102 |
if not isSoundEnabled then exit;
|
|
103 |
if Mix_PlayingMusic = 0 then
|
|
104 |
Mix_PlayMusic(Mus, -1)
|
|
105 |
end;
|
|
106 |
|
371
|
107 |
function ChangeVolume(voldelta: LongInt): LongInt;
|
174
|
108 |
begin
|
181
|
109 |
if not isSoundEnabled then
|
351
|
110 |
exit(0);
|
|
111 |
|
174
|
112 |
inc(Volume, voldelta);
|
|
113 |
if Volume < 0 then Volume:= 0;
|
175
|
114 |
Mix_Volume(-1, Volume);
|
|
115 |
Volume:= Mix_Volume(-1, -1);
|
|
116 |
Mix_VolumeMusic(Volume * 3 div 8);
|
351
|
117 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME
|
174
|
118 |
end;
|
|
119 |
|
4
|
120 |
end.
|