Learn Programming with SwinGame - Calling Procedures

From SwinBrain

This podcast covers calling procedures and passing parameters, and shows how to play sound effects using SwinGame. If you want more details on using and declaring procedures have a look at the Functions and Procedures article, and watch the podcast on declaring your own procedures.

Contents

Basic Procedure Calls

The Hello World program shows how to call the WriteLn procedure that can be used to print text to the console.

  • WriteLn is a procedure
  • WriteLn can accept a number of parameters
  • Each parameter is an expression
program HelloWorld;
 
var
  name: String;
begin
  WriteLn('Hello World!');
  
  name := 'Fred';
  WriteLn('Hello ', name);
  
  WriteLn('2 + 5 * 7 = ', 2 + 5 * 7);
end.

Punch-Up Code

The Punch-Up code plays some punch sounds using SwinGame. The code for this is in GameMain.pas, which needs the sounds.txt file and these resources. These wav files need to be places in SwinGame's Resources, sounds folder.

GameMain.pas

Remember to download the resources zip file that contains the sound effects you need for this code to work.

GameMain contains the source code that is run when the program starts.

  • OpenAudio is a procedure that prepares SwinGame to play audio.
  • LoadResourceBundle is a procedure that loads resources such as sound effects into SwinGame.
  • PlaySoundEffect is a procedure that can play a sound effect (SoundEffectNamed is a function call that returns a loaded sound effect, see Calling Functions).
  • Delay is a procedure that pauses execution for a number of milliseconds.
  • CloseAudio is a procedure that turns off audio in SwinGame.
  • ReleaseAllResources releases the resources that SwinGame is using, such as the sound effects.
program GameMain;
{$IFNDEF UNIX} {$r GameLauncher.res} {$ENDIF}
uses
  sgTypes, sgCore, sgAudio, sgText, sgGraphics, sgResources;
 
procedure Main();
begin
  OpenAudio();
  
  LoadResourceBundle('sounds.txt');
  
  PlaySoundEffect(SoundEffectNamed('punch1'));
  Delay(400); // 1 second
  
  PlaySoundEffect(SoundEffectNamed('punch2'));
  Delay(300);
  
  PlaySoundEffect(SoundEffectNamed('punch3'));
  Delay(100);
  
  PlaySoundEffect(SoundEffectNamed('punch1'));
  Delay(120);
  
  PlaySoundEffect(SoundEffectNamed('punch3'));
  Delay(100);
  
  PlaySoundEffect(SoundEffectNamed('punch2'));
  Delay(500);
  
  PlaySoundEffect(SoundEffectNamed('boom1'));
  Delay(1000);
  
  CloseAudio();
  ReleaseAllResources();
end;
 
begin
  Main();
end.

sounds.txt

The sounds.txt file needs to be located in SwinGame's Resources, bundles folder. This contains details on resources that SwinGame can load using the LoadResourceBundle procedure.

SOUND,boom1,boom1.wav
SOUND,punch1,punch1.wav
SOUND,punch2,punch2.wav
SOUND,punch3,punch3.wav

[edit]Learn Programming: Getting Started | Calling Procedures | Calling Functions | ...
Links: Topic Brains
Return to Swin Brain