hedgewars/uSound.pas
changeset 1654 9cfa6f23e767
parent 1562 c0eea030347b
child 1656 209cf0e2fc36
equal deleted inserted replaced
1653:34c1d1e3f04e 1654:9cfa6f23e767
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2005, 2007 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2005, 2007, 2009 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     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
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    19 unit uSound;
    19 unit uSound;
    20 interface
    20 interface
    21 uses SDLh, uConsts;
    21 uses SDLh, uConsts;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 
    23 
       
    24 type PVoicepack = ^TVoicepack;
       
    25 	TVoicepack = record
       
    26 		name: shortstring;
       
    27 		chunks: array [TSound] of PMixChunk;
       
    28 		end;
       
    29 
    24 procedure InitSound;
    30 procedure InitSound;
    25 procedure ReleaseSound;
    31 procedure ReleaseSound;
    26 procedure SoundLoad;
    32 procedure SoundLoad;
    27 procedure PlaySound(snd: TSound; infinite: boolean);
    33 procedure PlaySound(snd: TSound; infinite: boolean);
    28 procedure PlayMusic;
    34 procedure PlayMusic;
    29 procedure StopSound(snd: TSound);
    35 procedure StopSound(snd: TSound);
    30 function  ChangeVolume(voldelta: LongInt): LongInt;
    36 function  ChangeVolume(voldelta: LongInt): LongInt;
       
    37 function  AskForVoicepack(name: shortstring): Pointer;
    31 
    38 
    32 var MusicFN: shortstring = '';
    39 var MusicFN: shortstring = '';
    33 
    40 
    34 implementation
    41 implementation
    35 uses uMisc, uConsole;
    42 uses uMisc, uConsole;
    36 
    43 
    37 const chanTPU = 12;
    44 const chanTPU = 12;
    38 var Mus: PMixMusic = nil;
    45 var Mus: PMixMusic = nil;
    39     Volume: LongInt;
    46 	Volume: LongInt;
       
    47 	lastChan: array [TSound] of LongInt;
       
    48 	voicepacks: array[0..cMaxTeams] of TVoicepack;
       
    49 
       
    50 function  AskForVoicepack(name: shortstring): Pointer;
       
    51 var i: Longword;
       
    52 begin
       
    53 i:= 0;
       
    54 while (voicepacks[i].name <> name) and (voicepacks[i].name <> '') do
       
    55 	begin
       
    56 	inc(i);
       
    57 	TryDo(i <= cMaxTeams, 'Engine bug: AskForVoicepack i > cMaxTeams', true)
       
    58 	end;
       
    59 
       
    60 voicepacks[i].name:= name;
       
    61 AskForVoicepack:= @voicepacks[i]
       
    62 end;
    40 
    63 
    41 procedure InitSound;
    64 procedure InitSound;
    42 begin
    65 begin
    43 if not isSoundEnabled then exit;
    66 if not isSoundEnabled then exit;
    44 WriteToConsole('Init sound...');
    67 WriteToConsole('Init sound...');
    68 procedure SoundLoad;
    91 procedure SoundLoad;
    69 var i: TSound;
    92 var i: TSound;
    70     s: shortstring;
    93     s: shortstring;
    71 begin
    94 begin
    72 if not isSoundEnabled then exit;
    95 if not isSoundEnabled then exit;
       
    96 AskForVoicepack('Default');
       
    97 
    73 for i:= Low(TSound) to High(TSound) do
    98 for i:= Low(TSound) to High(TSound) do
    74     begin
    99 	begin
    75     s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   100 	s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
    76     WriteToConsole(msgLoading + s + ' ');
   101 	WriteToConsole(msgLoading + s + ' ');
    77     Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   102 	Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
    78     TryDo(Soundz[i].id <> nil, msgFailed, true);
   103 	TryDo(Soundz[i].id <> nil, msgFailed, true);
    79     WriteLnToConsole(msgOK);
   104 	WriteLnToConsole(msgOK);
    80     end;
   105 	end;
    81 end;
   106 end;
    82 
   107 
    83 procedure PlaySound(snd: TSound; infinite: boolean);
   108 procedure PlaySound(snd: TSound; infinite: boolean);
    84 var loops: LongInt;
   109 var loops: LongInt;
    85 begin
   110 begin
    86 if (not isSoundEnabled) or fastUntilLag then exit;
   111 if (not isSoundEnabled) or fastUntilLag then exit;
    87 if infinite then loops:= -1 else loops:= 0;
   112 if infinite then loops:= -1 else loops:= 0;
    88 Soundz[snd].lastChan:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
   113 lastChan[snd]:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
    89 end;
   114 end;
    90 
   115 
    91 procedure StopSound(snd: TSound);
   116 procedure StopSound(snd: TSound);
    92 begin
   117 begin
    93 if not isSoundEnabled then exit;
   118 if not isSoundEnabled then exit;
    94 if Mix_Playing(Soundz[snd].lastChan) <> 0 then
   119 if Mix_Playing(lastChan[snd]) <> 0 then
    95    Mix_HaltChannel(Soundz[snd].lastChan)
   120 	Mix_HaltChannel(lastChan[snd])
    96 end;
   121 end;
    97 
   122 
    98 procedure PlayMusic;
   123 procedure PlayMusic;
    99 var s: string;
   124 var s: string;
   100 begin
   125 begin
   113 end;
   138 end;
   114 
   139 
   115 function ChangeVolume(voldelta: LongInt): LongInt;
   140 function ChangeVolume(voldelta: LongInt): LongInt;
   116 begin
   141 begin
   117 if not isSoundEnabled then
   142 if not isSoundEnabled then
   118    exit(0);
   143 	exit(0);
   119 
   144 
   120 inc(Volume, voldelta);
   145 inc(Volume, voldelta);
   121 if Volume < 0 then Volume:= 0;
   146 if Volume < 0 then Volume:= 0;
   122 Mix_Volume(-1, Volume);
   147 Mix_Volume(-1, Volume);
   123 Volume:= Mix_Volume(-1, -1);
   148 Volume:= Mix_Volume(-1, -1);