hedgewars/uSound.pas
author unc0rr
Thu, 05 Oct 2006 16:33:18 +0000
changeset 183 57c2ef19f719
parent 181 7dfffbf0c7f6
child 282 b1e3387389b6
permissions -rw-r--r--
Relicense to GPL
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;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
procedure PlaySound(snd: TSound);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    28
procedure PlayMusic;
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    29
procedure StopTPUSound;
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
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
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
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    85
procedure PlaySound(snd: TSound);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    86
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    87
if not isSoundEnabled then exit;
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    88
if snd <> sndThrowPowerUp then Mix_PlayChannelTimed(-1, Soundz[snd].id, 0, -1)
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    89
                          else Mix_PlayChannelTimed(chanTPU, Soundz[snd].id, 0, -1)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    90
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    91
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    92
procedure StopTPUSound;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    93
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    94
if not isSoundEnabled then exit;
13
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    95
if Mix_Playing(chanTPU) <> 0 then
7a5db822fd3f - Show no crosshair when using mine
unc0rr
parents: 11
diff changeset
    96
   Mix_HaltChannel(chanTPU)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
procedure PlayMusic;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
if not isSoundEnabled then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
if Mix_PlayingMusic = 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   103
   Mix_PlayMusic(Mus, -1)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   106
function ChangeVolume(voldelta: integer): integer;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   107
begin
181
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   108
if not isSoundEnabled then
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   109
   begin
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   110
   Result:= 0;
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   111
   exit
7dfffbf0c7f6 - Fixed crash with sound turned off
unc0rr
parents: 175
diff changeset
   112
   end;
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   113
inc(Volume, voldelta);
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   114
if Volume < 0 then Volume:= 0;
175
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   115
Mix_Volume(-1, Volume);
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   116
Volume:= Mix_Volume(-1, -1);
d226d976d836 - Some improvements to volume control
unc0rr
parents: 174
diff changeset
   117
Mix_VolumeMusic(Volume * 3 div 8);
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   118
Result:= Volume * 100 div MIX_MAX_VOLUME
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   119
end;
0b2c5b22f644 Changeable volume
unc0rr
parents: 97
diff changeset
   120
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   121
end.