author | unc0rr |
Thu, 05 Oct 2006 16:33:18 +0000 | |
changeset 183 | 57c2ef19f719 |
parent 181 | 7dfffbf0c7f6 |
child 282 | b1e3387389b6 |
permissions | -rw-r--r-- |
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; |
|
27 |
procedure PlaySound(snd: TSound); |
|
28 |
procedure PlayMusic; |
|
13 | 29 |
procedure StopTPUSound; |
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 |
||
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
70
diff
changeset
|
78 |
s:= PathPrefix + 'Data/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 |
||
85 |
procedure PlaySound(snd: TSound); |
|
86 |
begin |
|
87 |
if not isSoundEnabled then exit; |
|
13 | 88 |
if snd <> sndThrowPowerUp then Mix_PlayChannelTimed(-1, Soundz[snd].id, 0, -1) |
89 |
else Mix_PlayChannelTimed(chanTPU, Soundz[snd].id, 0, -1) |
|
4 | 90 |
end; |
91 |
||
13 | 92 |
procedure StopTPUSound; |
4 | 93 |
begin |
94 |
if not isSoundEnabled then exit; |
|
13 | 95 |
if Mix_Playing(chanTPU) <> 0 then |
96 |
Mix_HaltChannel(chanTPU) |
|
4 | 97 |
end; |
98 |
||
99 |
procedure PlayMusic; |
|
100 |
begin |
|
101 |
if not isSoundEnabled then exit; |
|
102 |
if Mix_PlayingMusic = 0 then |
|
103 |
Mix_PlayMusic(Mus, -1) |
|
104 |
end; |
|
105 |
||
174 | 106 |
function ChangeVolume(voldelta: integer): integer; |
107 |
begin |
|
181 | 108 |
if not isSoundEnabled then |
109 |
begin |
|
110 |
Result:= 0; |
|
111 |
exit |
|
112 |
end; |
|
174 | 113 |
inc(Volume, voldelta); |
114 |
if Volume < 0 then Volume:= 0; |
|
175 | 115 |
Mix_Volume(-1, Volume); |
116 |
Volume:= Mix_Volume(-1, -1); |
|
117 |
Mix_VolumeMusic(Volume * 3 div 8); |
|
174 | 118 |
Result:= Volume * 100 div MIX_MAX_VOLUME |
119 |
end; |
|
120 |
||
4 | 121 |
end. |