Unit gameSoundUnit

The code of gameSoundUnit

unit gameSoundUnit;
{
    Copyright (c) 2011 Christopher Winward
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sdl, sdl_mixer, gameVariables;

const
  Audio_Frequency : integer = 44100;
  Audio_Format : Word = Audio_S16;
  Audio_Channels : Integer = 2;
  Audio_ChunkSize : Integer = 4096;

type soundRecord = record
  soundFile : pMix_Chunk;
  //soundID : smallint; //Irrelevant?
  soundName : string;
  //soundType : shortstring;
end;

var
  soundBank : array[1 .. maxSounds] of soundRecord;

procedure audioInitialise;
procedure loadSound(fileLocation : PChar; name : string; fileType : shortstring = 'wav');
procedure playSoundD(sound : pMix_Chunk);
procedure playSound(name : string);
procedure SetVolume(name : string; volume : U8);

implementation

procedure audioInitialise;
{We want to set up the audio mixer using Mix_OpenAudio, and if we can do this,
we then want to initialise the SoundBank to 0.
}
var
  count : U16;
begin
  if (Mix_OpenAudio(Audio_Frequency, Audio_Format, Audio_Channels, Audio_ChunkSize) <> 0) then
    begin
      writeln('Error opening the audio mixer.');
      SDL_Quit;
      Halt;
    end;
  for count := 1 to maxSounds do
    begin
      with soundBank[count] do
        begin
          soundFile := nil;
          soundName := 'nil';
        end;
    end;
end;

procedure loadSound(fileLocation : PChar; name : string; fileType : shortstring = 'wav');
{We want to see if it's possible to load the sound (does it exist?) and then
add it to the soundBank.
}
var
  inputSoundName : string;
  soundAddress : U16;
begin
  inputSoundName := uppercase(name);
  soundAddress := hashString(inputSoundName, maxSounds);
                  //hash the name to find the array address
  with soundBank[soundAddress] do
    begin
      soundFile := Mix_LoadWav(PChar('sounds/' + fileLocation + '.' + fileType));
      if (soundFile = nil) then
        writeln('Error loading sound ', fileLocation, '.', fileType);
      soundName := name;
    end;
end;

procedure playSoundD(sound : pMix_Chunk);
begin
  Mix_PlayChannel(-1, sound, 0);
end;

procedure PlaySound(name : string);
var
  inputSoundName : string;
  soundAddress : U16;
begin
  inputSoundName := uppercase(name);
  soundAddress := hashString(inputSoundName, maxSounds);

  if (not (soundBank[soundAddress].soundName = 'nil')) then
    PlaySoundD(soundBank[soundAddress].soundFile);
end;

procedure SetVolume(name : string; volume : U8);
var
  inputSoundName : string;
  soundAddress : U16;
begin
  inputSoundName := uppercase(name);
  soundAddress := hashString(inputSoundName,maxSounds);

  if (soundBank[soundAddress].soundName = 'nil') then
    begin
      writeln('Sound ' + name + ' not found in the sound bank.');
    end
  else
    begin
      mix_volumechunk(soundBank[soundAddress].soundFile, volume);
    end;
end;

end.

Remarks

Could you write a game that uses routines in these SpaceShooter units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16