hedgewars/uSound.pas
author unc0rr
Tue, 05 Dec 2006 17:27:27 +0000
changeset 288 929c44745fd9
parent 287 b0eef98928f8
child 351 29bc9c36ad5f
permissions -rw-r--r--
Ammo schemes and ammo stores support in engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
(*
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
 * Hedgewars, a worms-like game
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    12
 * GNU General Public License for more details.
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 181
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
 *)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
unit uSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
interface
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    21
uses SDLh, uConsts;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    22
{$INCLUDE options.inc}
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    23
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    24
procedure InitSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
procedure ReleaseSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
procedure SoundLoad;
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    27
procedure PlaySound(snd: TSound; const infinite: boolean = false);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    28
procedure PlayMusic;
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    29
procedure StopSound(snd: TSound);
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    30
function  ChangeVolume(voldelta: integer): integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    31
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
implementation
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    33
uses uMisc, uConsole;
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    34
const chanTPU = 12;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    35
var Mus: PMixMusic;
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    36
    Volume: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    37
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    38
procedure InitSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    39
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
if not isSoundEnabled then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
WriteToConsole('Init sound...');
11
b64d67d222b9 Fix some issues with sound
unc0rr
parents: 4
diff changeset
    42
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
b64d67d222b9 Fix some issues with sound
unc0rr
parents: 4
diff changeset
    43
if isSoundEnabled then
b64d67d222b9 Fix some issues with sound
unc0rr
parents: 4
diff changeset
    44
   isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
if isSoundEnabled then WriteLnToConsole(msgOK)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
                  else WriteLnToConsole(msgFailed);
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    47
Mix_AllocateChannels(Succ(chanTPU));
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    48
Mix_VolumeMusic(48);
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    49
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    50
Volume:= cInitVolume;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    51
if Volume < 0 then Volume:= 0;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
    52
Volume:= Mix_Volume(-1, Volume)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    53
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    54
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    55
procedure ReleaseSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
var i: TSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    57
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    58
for i:= Low(TSound) to High(TSound) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    59
    Mix_FreeChunk(Soundz[i].id);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
Mix_FreeMusic(Mus);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    61
Mix_CloseAudio
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    63
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
procedure SoundLoad;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
var i: TSound;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
    s: string;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
if not isSoundEnabled then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
for i:= Low(TSound) to High(TSound) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
    begin
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 53
diff changeset
    71
    s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    72
    WriteToConsole(msgLoading + s + ' ');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    73
    Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(PChar(s), 'rb'), 1);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    74
    TryDo(Soundz[i].id <> nil, msgFailed, true);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    75
    WriteLnToConsole(msgOK);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    76
    end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
287
b0eef98928f8 Fix music loading path
unc0rr
parents: 282
diff changeset
    78
s:= PathPrefix + '/Music/kahvi140a_alexander_chereshnev-illusion.ogg';
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    79
WriteToConsole(msgLoading + s + ' ');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
Mus:= Mix_LoadMUS(PChar(s));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    81
TryDo(Mus <> nil, msgFailed, false);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    82
WriteLnToConsole(msgOK)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    83
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    84
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    85
procedure PlaySound(snd: TSound; const infinite: boolean = false);
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    86
var loops: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    87
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    88
if not isSoundEnabled then exit;
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    89
if infinite then loops:= -1 else loops:= 0;
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    90
Soundz[snd].lastChan:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    91
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    92
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    93
procedure StopSound(snd: TSound);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    94
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    95
if not isSoundEnabled then exit;
282
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    96
if Mix_Playing(Soundz[snd].lastChan) <> 0 then
b1e3387389b6 - Pickhammer sound
unc0rr
parents: 183
diff changeset
    97
   Mix_HaltChannel(Soundz[snd].lastChan)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
procedure PlayMusic;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
if not isSoundEnabled then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   103
if Mix_PlayingMusic = 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
   Mix_PlayMusic(Mus, -1)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   106
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   107
function ChangeVolume(voldelta: integer): integer;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   108
begin
181
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   109
if not isSoundEnabled then
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   110
   begin
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   111
   Result:= 0;
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   112
   exit
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   113
   end;
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   114
inc(Volume, voldelta);
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   115
if Volume < 0 then Volume:= 0;
175
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   116
Mix_Volume(-1, Volume);
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   117
Volume:= Mix_Volume(-1, -1);
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   118
Mix_VolumeMusic(Volume * 3 div 8);
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   119
Result:= Volume * 100 div MIX_MAX_VOLUME
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   120
end;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   121
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   122
end.