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;
|
282
|
27 |
procedure PlaySound(snd: TSound; const infinite: boolean = false);
|
4
|
28 |
procedure PlayMusic;
|
282
|
29 |
procedure StopSound(snd: TSound);
|
174
|
30 |
function ChangeVolume(voldelta: integer): integer;
|
4
|
31 |
|
|
32 |
implementation
|
|
33 |
uses uMisc, uConsole;
|
13
|
34 |
const chanTPU = 12;
|
4
|
35 |
var Mus: PMixMusic;
|
174
|
36 |
Volume: integer;
|
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;
|
|
66 |
s: string;
|
|
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;
|
4
|
72 |
WriteToConsole(msgLoading + s + ' ');
|
|
73 |
Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(PChar(s), 'rb'), 1);
|
|
74 |
TryDo(Soundz[i].id <> nil, msgFailed, true);
|
|
75 |
WriteLnToConsole(msgOK);
|
|
76 |
end;
|
|
77 |
|
287
|
78 |
s:= PathPrefix + '/Music/kahvi140a_alexander_chereshnev-illusion.ogg';
|
4
|
79 |
WriteToConsole(msgLoading + s + ' ');
|
|
80 |
Mus:= Mix_LoadMUS(PChar(s));
|
|
81 |
TryDo(Mus <> nil, msgFailed, false);
|
|
82 |
WriteLnToConsole(msgOK)
|
|
83 |
end;
|
|
84 |
|
282
|
85 |
procedure PlaySound(snd: TSound; const infinite: boolean = false);
|
|
86 |
var loops: integer;
|
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 |
|
174
|
107 |
function ChangeVolume(voldelta: integer): integer;
|
|
108 |
begin
|
181
|
109 |
if not isSoundEnabled then
|
|
110 |
begin
|
|
111 |
Result:= 0;
|
|
112 |
exit
|
|
113 |
end;
|
174
|
114 |
inc(Volume, voldelta);
|
|
115 |
if Volume < 0 then Volume:= 0;
|
175
|
116 |
Mix_Volume(-1, Volume);
|
|
117 |
Volume:= Mix_Volume(-1, -1);
|
|
118 |
Mix_VolumeMusic(Volume * 3 div 8);
|
174
|
119 |
Result:= Volume * 100 div MIX_MAX_VOLUME
|
|
120 |
end;
|
|
121 |
|
4
|
122 |
end.
|