hedgewars/uSound.pas
changeset 1656 209cf0e2fc36
parent 1654 9cfa6f23e767
child 1657 dde8f60d3e07
equal deleted inserted replaced
1655:16f687fc18e3 1656:209cf0e2fc36
    32 procedure SoundLoad;
    32 procedure SoundLoad;
    33 procedure PlaySound(snd: TSound; infinite: boolean);
    33 procedure PlaySound(snd: TSound; infinite: boolean);
    34 procedure PlayMusic;
    34 procedure PlayMusic;
    35 procedure StopSound(snd: TSound);
    35 procedure StopSound(snd: TSound);
    36 function  ChangeVolume(voldelta: LongInt): LongInt;
    36 function  ChangeVolume(voldelta: LongInt): LongInt;
       
    37 
    37 function  AskForVoicepack(name: shortstring): Pointer;
    38 function  AskForVoicepack(name: shortstring): Pointer;
       
    39 procedure SetVoicePack(vp: PVoicePack);
    38 
    40 
    39 var MusicFN: shortstring = '';
    41 var MusicFN: shortstring = '';
    40 
    42 
    41 implementation
    43 implementation
    42 uses uMisc, uConsole;
    44 uses uMisc, uConsole;
    44 const chanTPU = 12;
    46 const chanTPU = 12;
    45 var Mus: PMixMusic = nil;
    47 var Mus: PMixMusic = nil;
    46 	Volume: LongInt;
    48 	Volume: LongInt;
    47 	lastChan: array [TSound] of LongInt;
    49 	lastChan: array [TSound] of LongInt;
    48 	voicepacks: array[0..cMaxTeams] of TVoicepack;
    50 	voicepacks: array[0..cMaxTeams] of TVoicepack;
       
    51 	defVoicepack: PVoicepack;
       
    52 	currentVP: PVoicepack;
    49 
    53 
    50 function  AskForVoicepack(name: shortstring): Pointer;
    54 function  AskForVoicepack(name: shortstring): Pointer;
    51 var i: Longword;
    55 var i: Longword;
    52 begin
    56 begin
    53 i:= 0;
    57 i:= 0;
    57 	TryDo(i <= cMaxTeams, 'Engine bug: AskForVoicepack i > cMaxTeams', true)
    61 	TryDo(i <= cMaxTeams, 'Engine bug: AskForVoicepack i > cMaxTeams', true)
    58 	end;
    62 	end;
    59 
    63 
    60 voicepacks[i].name:= name;
    64 voicepacks[i].name:= name;
    61 AskForVoicepack:= @voicepacks[i]
    65 AskForVoicepack:= @voicepacks[i]
       
    66 end;
       
    67 
       
    68 procedure SetVoicePack(vp: PVoicePack);
       
    69 begin
       
    70 currentVP:= vp
    62 end;
    71 end;
    63 
    72 
    64 procedure InitSound;
    73 procedure InitSound;
    65 begin
    74 begin
    66 if not isSoundEnabled then exit;
    75 if not isSoundEnabled then exit;
    78 Volume:= Mix_Volume(-1, Volume)
    87 Volume:= Mix_Volume(-1, Volume)
    79 end;
    88 end;
    80 
    89 
    81 procedure ReleaseSound;
    90 procedure ReleaseSound;
    82 var i: TSound;
    91 var i: TSound;
       
    92 	t: Longword;
    83 begin
    93 begin
    84 for i:= Low(TSound) to High(TSound) do
    94 for t:= 0 to cMaxTeams do
    85 	Mix_FreeChunk(Soundz[i].id);
    95 	if voicepacks[t].name <> '' then
       
    96 		for i:= Low(TSound) to High(TSound) do
       
    97 			if voicepacks[t].chunks[i] <> nil then
       
    98 				Mix_FreeChunk(voicepacks[t].chunks[i]);
    86 
    99 
    87 Mix_FreeMusic(Mus);
   100 Mix_FreeMusic(Mus);
    88 Mix_CloseAudio
   101 Mix_CloseAudio
    89 end;
   102 end;
    90 
   103 
    91 procedure SoundLoad;
   104 procedure SoundLoad;
    92 var i: TSound;
   105 var i: TSound;
    93     s: shortstring;
   106 	s: shortstring;
       
   107 	t: Longword;
    94 begin
   108 begin
    95 if not isSoundEnabled then exit;
   109 if not isSoundEnabled then exit;
    96 AskForVoicepack('Default');
   110 
       
   111 defVoicepack:= AskForVoicepack('Default');
       
   112 SetVoicePack(defVoicepack);
    97 
   113 
    98 for i:= Low(TSound) to High(TSound) do
   114 for i:= Low(TSound) to High(TSound) do
    99 	begin
   115 	if Soundz[i].Path <> ptVoices then
   100 	s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   116 		begin
   101 	WriteToConsole(msgLoading + s + ' ');
   117 		s:= Pathz[Soundz[i].Path] + '/Default/' + Soundz[i].FileName;
   102 	Soundz[i].id:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   118 		WriteToConsole(msgLoading + s + ' ');
   103 	TryDo(Soundz[i].id <> nil, msgFailed, true);
   119 		defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   104 	WriteLnToConsole(msgOK);
   120 		TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true);
   105 	end;
   121 		WriteLnToConsole(msgOK);
       
   122 		end;
       
   123 
       
   124 for t:= 0 to cMaxTeams do
       
   125 	if voicepacks[t].name <> '' then
       
   126 		for i:= Low(TSound) to High(TSound) do
       
   127 			if Soundz[i].Path = ptVoices then
       
   128 				begin
       
   129 				s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
       
   130 				WriteToConsole(msgLoading + s + ' ');
       
   131 				voicepacks[t].chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   132 				if voicepacks[t].chunks[i] = nil then
       
   133 					WriteLnToConsole(msgFailed)
       
   134 				else
       
   135 					WriteLnToConsole(msgOK)
       
   136 				end;
   106 end;
   137 end;
   107 
   138 
   108 procedure PlaySound(snd: TSound; infinite: boolean);
   139 procedure PlaySound(snd: TSound; infinite: boolean);
   109 var loops: LongInt;
   140 var loops: LongInt;
   110 begin
   141 begin
   111 if (not isSoundEnabled) or fastUntilLag then exit;
   142 if (not isSoundEnabled) or fastUntilLag then exit;
   112 if infinite then loops:= -1 else loops:= 0;
   143 if infinite then loops:= -1 else loops:= 0;
   113 lastChan[snd]:= Mix_PlayChannelTimed(-1, Soundz[snd].id, loops, -1)
   144 
       
   145 if currentVP^.chunks[snd] <> nil then
       
   146 	lastChan[snd]:= Mix_PlayChannelTimed(-1, currentVP^.chunks[snd], loops, -1)
       
   147 else
       
   148 	lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], loops, -1)
   114 end;
   149 end;
   115 
   150 
   116 procedure StopSound(snd: TSound);
   151 procedure StopSound(snd: TSound);
   117 begin
   152 begin
   118 if not isSoundEnabled then exit;
   153 if not isSoundEnabled then exit;