hedgewars/uSound.pas
author unc0rr
Thu, 26 Oct 2006 16:08:29 +0000
changeset 205 8d9aff55e6ab
parent 183 57c2ef19f719
child 282 b1e3387389b6
permissions -rw-r--r--
- Set version to 0.8 - Check protocol version to ensure clients compatibility in further versions

(*
 * Hedgewars, a worms-like game
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *)

unit uSound;
interface
uses SDLh, uConsts;
{$INCLUDE options.inc}

procedure InitSound;
procedure ReleaseSound;
procedure SoundLoad;
procedure PlaySound(snd: TSound);
procedure PlayMusic;
procedure StopTPUSound;
function  ChangeVolume(voldelta: integer): integer;

implementation
uses uMisc, uConsole;
const chanTPU = 12;
var Mus: PMixMusic;
    Volume: integer;

procedure InitSound;
begin
if not isSoundEnabled then exit;
WriteToConsole('Init sound...');
isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
if isSoundEnabled then
   isSoundEnabled:= Mix_OpenAudio(22050, $8010, 2, 512) = 0;
if isSoundEnabled then WriteLnToConsole(msgOK)
                  else WriteLnToConsole(msgFailed);
Mix_AllocateChannels(Succ(chanTPU));
Mix_VolumeMusic(48);

Volume:= cInitVolume;
if Volume < 0 then Volume:= 0;
Volume:= Mix_Volume(-1, Volume)
end;

procedure ReleaseSound;
var i: TSound;
begin
for i:= Low(TSound) to High(TSound) do
    Mix_FreeChunk(Soundz[i].id);
Mix_FreeMusic(Mus);
Mix_CloseAudio
end;

procedure SoundLoad;
var i: TSound;
    s: string;
begin
if not isSoundEnabled then exit;
for i:= Low(TSound) to High(TSound) do
    begin
    s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
    WriteToConsole(msgLoading + s + ' ');
    Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(PChar(s), 'rb'), 1);
    TryDo(Soundz[i].id <> nil, msgFailed, true);
    WriteLnToConsole(msgOK);
    end;

s:= PathPrefix + 'Data/Music/kahvi140a_alexander_chereshnev-illusion.ogg';
WriteToConsole(msgLoading + s + ' ');
Mus:= Mix_LoadMUS(PChar(s));
TryDo(Mus <> nil, msgFailed, false);
WriteLnToConsole(msgOK)
end;

procedure PlaySound(snd: TSound);
begin
if not isSoundEnabled then exit;
if snd <> sndThrowPowerUp then Mix_PlayChannelTimed(-1, Soundz[snd].id, 0, -1)
                          else Mix_PlayChannelTimed(chanTPU, Soundz[snd].id, 0, -1)
end;

procedure StopTPUSound;
begin
if not isSoundEnabled then exit;
if Mix_Playing(chanTPU) <> 0 then
   Mix_HaltChannel(chanTPU)
end;

procedure PlayMusic;
begin
if not isSoundEnabled then exit;
if Mix_PlayingMusic = 0 then
   Mix_PlayMusic(Mus, -1)
end;

function ChangeVolume(voldelta: integer): integer;
begin
if not isSoundEnabled then
   begin
   Result:= 0;
   exit
   end;
inc(Volume, voldelta);
if Volume < 0 then Volume:= 0;
Mix_Volume(-1, Volume);
Volume:= Mix_Volume(-1, -1);
Mix_VolumeMusic(Volume * 3 div 8);
Result:= Volume * 100 div MIX_MAX_VOLUME
end;

end.