hedgewars/uSound.pas
branchexperimental3D
changeset 4812 f924be23ffb4
parent 4748 ce9b48de1f52
child 4948 c3dc41ae68fa
equal deleted inserted replaced
4347:0ddb100fea61 4812:f924be23ffb4
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uSound;
    21 unit uSound;
    22 interface
    22 interface
    23 uses SDLh, uConsts;
    23 uses SDLh, uConsts, uTypes;
    24 
       
    25 type PVoicepack = ^TVoicepack;
       
    26     TVoicepack = record
       
    27         name: shortstring;
       
    28         chunks: array [TSound] of PMixChunk;
       
    29         end;
       
    30 
    24 
    31 var MusicFN: shortstring;
    25 var MusicFN: shortstring;
    32 
    26 
    33 procedure initModule;
    27 procedure initModule;
    34 procedure freeModule;
    28 procedure freeModule;
    43 function  LoopSound(snd: TSound): LongInt;
    37 function  LoopSound(snd: TSound): LongInt;
    44 function  LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
    38 function  LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
    45 procedure PlayMusic;
    39 procedure PlayMusic;
    46 procedure PauseMusic;
    40 procedure PauseMusic;
    47 procedure ResumeMusic;
    41 procedure ResumeMusic;
       
    42 procedure ChangeMusic;
    48 procedure StopSound(snd: TSound);
    43 procedure StopSound(snd: TSound);
    49 procedure StopSound(chn: LongInt);
    44 procedure StopSound(chn: LongInt);
    50 function  ChangeVolume(voldelta: LongInt): LongInt;
    45 function  ChangeVolume(voldelta: LongInt): LongInt;
    51 function  AskForVoicepack(name: shortstring): Pointer;
    46 function  AskForVoicepack(name: shortstring): Pointer;
    52 
    47 
    53 
    48 
    54 implementation
    49 implementation
    55 uses uMisc, uConsole;
    50 uses uVariables, uConsole, uUtils, uCommands, uDebug, uMobile;
    56 
    51 
    57 const chanTPU = 32;
    52 const chanTPU = 32;
    58 var Volume: LongInt;
    53 var Volume: LongInt;
    59     lastChan: array [TSound] of LongInt;
    54     lastChan: array [TSound] of LongInt;
    60     voicepacks: array[0..cMaxTeams] of TVoicepack;
    55     voicepacks: array[0..cMaxTeams] of TVoicepack;
   151 
   146 
   152     for i:= Low(TSound) to High(TSound) do
   147     for i:= Low(TSound) to High(TSound) do
   153     begin
   148     begin
   154         defVoicepack^.chunks[i]:= nil;
   149         defVoicepack^.chunks[i]:= nil;
   155         // preload all the big sound files (>32k) that would otherwise lockup the game
   150         // preload all the big sound files (>32k) that would otherwise lockup the game
   156         if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun, sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
   151         if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun,
       
   152                   sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
   157             and (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
   153             and (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
   158         begin
   154         begin
   159             s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   155             s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   160             WriteToConsole(msgLoading + s + ' ');
   156             WriteToConsole(msgLoading + s + ' ');
   161             defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   157             defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   182 end;
   178 end;
   183 
   179 
   184 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
   180 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
   185 var s:shortstring;
   181 var s:shortstring;
   186 begin
   182 begin
   187     if (not isSoundEnabled) or fastUntilLag then
   183     if (not isSoundEnabled) or fastUntilLag or isDeviceMute() then
   188         exit;
   184         exit;
   189 
   185 
   190     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   186     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   191         exit;
   187         exit;
   192 
   188 
   224 end;
   220 end;
   225 
   221 
   226 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
   222 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
   227 var s: shortstring;
   223 var s: shortstring;
   228 begin
   224 begin
   229     if (not isSoundEnabled) or fastUntilLag then
   225     if (not isSoundEnabled) or fastUntilLag or isDeviceMute() then
   230     begin
   226     begin
   231         LoopSound:= -1;
   227         LoopSound:= -1;
   232         exit
   228         exit
   233     end;
   229     end;
   234 
   230 
   321         exit;
   317         exit;
   322 
   318 
   323     Mix_ResumeMusic(Mus);
   319     Mix_ResumeMusic(Mus);
   324 end;
   320 end;
   325 
   321 
       
   322 procedure ChangeMusic;
       
   323 begin
       
   324     if (MusicFN = '') or (not isMusicEnabled) then
       
   325         exit;
       
   326 
       
   327     if Mus <> nil then
       
   328         Mix_FreeMusic(Mus);
       
   329 
       
   330     PlayMusic;
       
   331 end;
       
   332 
       
   333 procedure chVoicepack(var s: shortstring);
       
   334 begin
       
   335     if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/voicepack"', true);
       
   336     if s[1]='"' then Delete(s, 1, 1);
       
   337     if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
       
   338     CurrentTeam^.voicepack:= AskForVoicepack(s)
       
   339 end;
       
   340 
   326 procedure initModule;
   341 procedure initModule;
   327 begin
   342 begin
       
   343     RegisterVariable('voicepack', vtCommand, @chVoicepack, false);
   328     MusicFN:='';
   344     MusicFN:='';
   329 end;
   345 end;
   330 
   346 
   331 procedure freeModule;
   347 procedure freeModule;
   332 begin
   348 begin