author | unc0rr |
Fri, 19 Sep 2008 14:15:19 +0000 | |
changeset 1271 | e64221369875 |
parent 1225 | f882a92ef872 |
child 1562 | c0eea030347b |
permissions | -rw-r--r-- |
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)); |
1137 | 51 |
if isMusicEnabled then Mix_VolumeMusic(50); |
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) |
1128 | 102 |
or (MusicFN = '') |
103 |
or (not isMusicEnabled)then exit; |
|
565 | 104 |
|
1097 | 105 |
s:= PathPrefix + '/Music/' + MusicFN; |
564 | 106 |
WriteToConsole(msgLoading + s + ' '); |
565 | 107 |
|
564 | 108 |
Mus:= Mix_LoadMUS(Str2PChar(s)); |
109 |
TryDo(Mus <> nil, msgFailed, false); |
|
110 |
WriteLnToConsole(msgOK); |
|
111 |
||
1225
f882a92ef872
Play music in menu also, with fading effects when run game
unc0rr
parents:
1137
diff
changeset
|
112 |
SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false) |
4 | 113 |
end; |
114 |
||
371 | 115 |
function ChangeVolume(voldelta: LongInt): LongInt; |
174 | 116 |
begin |
181 | 117 |
if not isSoundEnabled then |
351 | 118 |
exit(0); |
119 |
||
174 | 120 |
inc(Volume, voldelta); |
121 |
if Volume < 0 then Volume:= 0; |
|
175 | 122 |
Mix_Volume(-1, Volume); |
123 |
Volume:= Mix_Volume(-1, -1); |
|
1137 | 124 |
if isMusicEnabled then Mix_VolumeMusic(Volume * 4 div 8); |
351 | 125 |
ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME |
174 | 126 |
end; |
127 |
||
4 | 128 |
end. |