Jelajahi Sumber

Added Audio Module to Library

Added a basic Audio module to the library that handles playing music in channel 0 and sound effects in channel 1. This will be a good starting point for anyone trying to quickly put sound in their game. Also removed any audio code from the AppCore.
Peter Robinson 3 tahun lalu
induk
melakukan
4e85e715db

+ 1 - 3
engine/source/audio/audio.cc

@@ -2148,9 +2148,7 @@ void alxUpdateMaxDistance()
 //--------------------------------------------------------------------------
 void alxUpdate()
 {
-   //if(mForceMaxDistanceUpdate)
-      alxUpdateMaxDistance();
-
+   alxUpdateMaxDistance();
    alxCloseHandles();
    alxUpdateScores(false);
    alxLoopingUpdate();

+ 10 - 5
library/AppCore/main.cs → library/AppCore/appCore.cs

@@ -22,28 +22,33 @@
 
 function AppCore::create( %this )
 {
+	// Init variables
+	%this.canvasCreated = false;
+
     // Load system scripts
     exec("./scripts/constants.cs");
     exec("./scripts/defaultPreferences.cs");
 	exec("./gui/guiProfiles.cs");
     exec("./scripts/canvas.cs");
-    exec("./scripts/openal.cs");
 
     // Initialize the canvas
-    initializeCanvas("Torque2D: Rocket Edition");
+    %this.initializeCanvas("Torque2D: Rocket Edition");
 
     // Set the canvas color
     Canvas.BackgroundColor = "Black";
     Canvas.UseBackgroundColor = true;
 
-    // Initialize audio
-    initializeOpenAL();
-
 	// Load other modules
     ModuleDatabase.loadGroup("gameBase");
 
 	// Put a gui in the Canvas
 	Canvas.setContent(TamlRead("./gui/defaultGui.gui.taml"));
+
+	// Play Music - If you are using the standard Audio module
+	if(isObject(Audio))
+	{
+		Audio.PlayMusic("planetfall");
+	}
 }
 
 //-----------------------------------------------------------------------------

+ 2 - 2
library/AppCore/module.taml

@@ -1,8 +1,8 @@
 <ModuleDefinition
 	ModuleId="AppCore"
 	VersionId="1"
-	Description="Barebones startup module"
-	ScriptFile="main.cs"
+	Description="Provides critical initialization and functionality needed for every project."
+	ScriptFile="appCore.cs"
 	CreateFunction="create"
 	DestroyFunction="destroy" >
 	<DeclaredAssets

+ 6 - 7
library/AppCore/scripts/canvas.cs

@@ -24,11 +24,10 @@
 // initializeCanvas
 // Constructs and initializes the default canvas window.
 //------------------------------------------------------------------------------
-$canvasCreated = false;
-function initializeCanvas(%windowName)
+function AppCore::initializeCanvas(%this, %windowName)
 {
     // Don't duplicate the canvas.
-    if($canvasCreated)
+    if(%this.canvasCreated)
     {
         error("Cannot instantiate more than one canvas!");
         return;
@@ -76,7 +75,7 @@ function initializeCanvas(%windowName)
 // resetCanvas
 // Forces the canvas to redraw itself.
 //------------------------------------------------------------------------------
-function resetCanvas()
+function AppCore::resetCanvas(%this)
 {
     if (isObject(Canvas))
         Canvas.repaint();
@@ -86,12 +85,12 @@ function resetCanvas()
 // iOSResolutionFromSetting
 // Helper function that grabs resolution strings based on device type
 //------------------------------------------------------------------------------
-function iOSResolutionFromSetting( %deviceType, %deviceScreenOrientation )
+function AppCore::iOSResolutionFromSetting( %this, %deviceType, %deviceScreenOrientation )
 {
     // A helper function to get a string based resolution from the settings given.
     %x = 0;
     %y = 0;
-    
+
     %scaleFactor = $pref::iOS::RetinaEnabled ? 2 : 1;
 
     switch(%deviceType)
@@ -132,6 +131,6 @@ function iOSResolutionFromSetting( %deviceType, %deviceScreenOrientation )
                 %y =  $iOS::constant::iPhone5Width;
             }
     }
-   
+
     return %x @ " " @ %y;
 }

+ 0 - 11
library/AppCore/scripts/defaultPreferences.cs

@@ -35,17 +35,6 @@ $pref::iOS::EnableOrientationRotation = 1;
 $pref::iOS::EnableOtherOrientationRotation = 1;
 $pref::iOS::StatusBarType       = 0;
 
-/// Audio
-$pref::Audio::driver = "OpenAL";
-$pref::Audio::forceMaxDistanceUpdate = 0;
-$pref::Audio::environmentEnabled = 0;
-$pref::Audio::masterVolume   = 1.0;
-$pref::Audio::channelVolume1 = 1.0;
-$pref::Audio::channelVolume2 = 1.0;
-$pref::Audio::channelVolume3 = 1.0;
-$pref::Audio::sfxVolume = 1.0;
-$pref::Audio::musicVolume = 1.0;
-
 /// T2D
 $pref::T2D::ParticlePlayerEmissionRateScale = 1.0;
 $pref::T2D::ParticlePlayerSizeScale = 1.0;

+ 0 - 64
library/AppCore/scripts/openal.cs

@@ -1,64 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (c) 2013 GarageGames, LLC
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to
-// deal in the Software without restriction, including without limitation the
-// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-// sell copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-// IN THE SOFTWARE.
-//-----------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-// Audio channel descriptions.
-//------------------------------------------------------------------------------
-$musicAudioType = 1;
-$effectsAudioType = 2;
-
-//------------------------------------------------------------------------------
-// initializeOpenAL
-// Starts up the OpenAL driver.
-//------------------------------------------------------------------------------
-function initializeOpenAL()
-{
-    // Just in case it is already started.
-    shutdownOpenAL();
-
-    echo("OpenAL Driver Init");
-
-    if (!OpenALInitDriver())
-    {
-        echo("OpenALInitDriver() failed");
-        $Audio::initFailed = true;
-    }
-    else
-    {
-        // Set the master volume.
-        alxListenerf(AL_GAIN_LINEAR, $pref::Audio::masterVolume);
-
-        // Set the channel volumes.
-        for (%channel = 1; %channel <= 3; %channel++)
-            alxSetChannelVolume(%channel, $pref::Audio::channelVolume[%channel]);
-
-        echo("OpenAL Driver Init Success");
-    }
-}
-
-//------------------------------------------------------------------------------
-// shutdownOpenAL
-//------------------------------------------------------------------------------
-function shutdownOpenAL()
-{
-    OpenALShutdownDriver();
-}

+ 135 - 0
library/Audio/audio.cs

@@ -0,0 +1,135 @@
+function Audio::create(%this)
+{
+	if(%this.initializeOpenAL())
+	{
+	    %this.MusicOn = true;
+	    %this.SoundOn = true;
+		%this.setMasterVolume(1);
+		%this.SetMusicVolume(1);
+		%this.SetSoundVolume(1);
+	    %this.CurrentSong = "";
+	}
+}
+
+function Audio::destroy( %this )
+{
+	OpenALShutdownDriver();
+}
+
+function Audio::initializeOpenAL()
+{
+    OpenALShutdownDriver();
+    echo("Initializing OpenAL Driver...");
+
+    if (!OpenALInitDriver())
+    {
+        echo("OpenAL driver failed to initialize... so quiet...");
+        return false;
+    }
+    else
+    {
+        echo("OpenAL driver initialized successfully! Let's make some noise!");
+		return true;
+    }
+}
+
+function Audio::setMasterVolume(%this, %volume)
+{
+	%this.MasterVolume = mClamp(%volume, 0, 1);
+	alxListenerf(AL_GAIN_LINEAR, %this.MasterVolume);
+}
+
+function Audio::SetMusicVolume(%this, %volume)
+{
+	%this.MusicVolume = mClamp(%volume, 0, 1);
+    alxSetChannelVolume(0, %this.MusicVolume);
+}
+
+function Audio::SetSoundVolume(%this, %volume)
+{
+    %this.SoundVolume = mClamp(%volume, 0,1);
+    alxSetChannelVolume(1, %this.SoundVolume);
+}
+
+function Audio::SetPitch(%this, %noise, %pitch)
+{
+	alxSourcef(%noise, AL_PITCH, %pitch);
+}
+
+function Audio::StopAllAndPlayMusic(%this, %song)
+{
+    if(%song !$= %this.CurrentSong)
+    {
+        alxStopAll();
+        %this.CurrentSong = %song;
+        if(%this.MusicOn)
+        {
+            cancel(%this.fadeMusicSchedule);
+            %this.Music = alxPlay("Audio:" @ %song);
+        }
+    }
+}
+
+function Audio::PlayMusic(%this, %song)
+{
+   if(%song !$= %this.CurrentSong)
+   {
+      %this.CurrentSong = %song;
+      if(%this.MusicOn)
+      {
+         cancel(%this.fadeMusicSchedule);
+         alxStop(%this.Music);
+         %this.Music = alxPlay("Audio:" @ %song);
+      }
+   }
+}
+
+function Audio::RestartMusic(%this)
+{
+	cancel(%this.fadeMusicSchedule);
+	alxStop(%this.Music);
+	%this.Music = alxPlay("Audio:" @ %this.CurrentSong);
+}
+
+function Audio::StopMusic(%this)
+{
+	alxStop(%this.Music);
+}
+
+function Audio::PlaySound(%this, %name)
+{
+    if(%this.SoundOn)
+    {
+        %sound = alxPlay("Audio:" @ %name);
+    }
+
+    return %sound;
+}
+
+function Audio::FadeMusicVolumeTo(%this, %time, %volume)
+{
+	//Time is in milliseconds
+	%volume = mClamp(%volume, 0,1);
+
+	if(%volume == %this.MusicVolume)
+	{
+		return;
+	}
+
+	%difference = %volume - %this.MusicVolume;
+	%rate = 50;
+	%steps = mCeil(%time / %rate);
+	%delta = %difference / %steps;
+	%this.fadeMusicSchedule = %this.schedule(%rate, "FadeMusicStep", %volume, %rate, %delta);
+}
+
+function Audio::FadeMusicStep(%this, %targetVolume, %rate, %delta)
+{
+	if(mAbs(%targetVolume - %this.MusicVolume) < mAbs(%delta))
+	{
+		%this.SetMusicVolume(%targetVolume);
+		return;
+	}
+	%this.SetMusicVolume(%this.MusicVolume + %delta);
+	%this.fadeMusicSchedule = %this.schedule(%rate, "FadeMusicStep", %targetVolume, %rate, %delta);
+}

+ 18 - 0
library/Audio/module.taml

@@ -0,0 +1,18 @@
+<ModuleDefinition
+	ModuleId="Audio"
+	VersionId="1"
+	Dependencies="AppCore"
+    Group="gameBase"
+	Description="Provides basic audio functionality for music and sound effects."
+	ScriptFile="audio.cs"
+	CreateFunction="create"
+	DestroyFunction="destroy">
+		<DeclaredAssets
+			Path="music"
+			Extension="audio.taml"
+			Recurse="true"/>
+		<DeclaredAssets
+			Path="sound"
+			Extension="audio.taml"
+			Recurse="true"/>
+</ModuleDefinition>

+ 6 - 0
library/Audio/music/planetfall.audio.taml

@@ -0,0 +1,6 @@
+<AudioAsset
+    AssetName="planetfall"
+    AssetAutoUnload="0"
+    AudioFile="planetfall.ogg"
+	VolumeChannel="0"
+	Looping="true" />

TEMPAT SAMPAH
library/Audio/music/planetfall.ogg


+ 4 - 0
library/Audio/sound/laser.audio.taml

@@ -0,0 +1,4 @@
+<AudioAsset
+    AssetName="laser"
+    AudioFile="laser.wav"
+ 	VolumeChannel="1" />

TEMPAT SAMPAH
library/Audio/sound/laser.wav