gr_tut8_screenshot.jpg

SoundAndMusic.cs

#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.Direct3D;

using JadEngine;
using JadEngine.Video;
using JadEngine.Core;
using JadEngine.Mathematics;
using JadEngine.Scene;
using JadEngine.Scene.MD5;
using JadEngine.Physics;
using JadEngine.Particles;
using JadEngine.Sound;
using JadEngine.Tools;
using JadEngine.Input;

#endregion

namespace Tutorial
{
	/// <summary>
	/// This example will teach you to use 2D sound and 5.1 sound.
	/// You´ll have a cube that will represent the sound source. If you move 
	/// the camera, the sound will be displaced to one of the 5 speakers
	/// </summary>
	/// <remarks>
	/// Playing .ogg files is deprecated in Jade 1.1.
	/// </remarks>
	public class SoundAndMusic : JApplication
	{
		#region Fields

		/// <summary>
		/// Indicates if the help must be shown in the screen or not
		/// </summary>
		bool showHelp = true;

		/// <summary>
		/// A mono or stereo sample to play in stereo output
		/// </summary>
		JSample sample;

		/// <summary>
		/// A mono sample to play in 3d (5.1) output
		/// </summary>
		JSample3D sample3D;

		/// <summary>
		/// Sample selected by the user, used to activate the echo effect
		/// </summary>
		JSample sampleSelected;

		/// <summary>
		/// Vorbis sample to play compresed sounds
		/// </summary>
		JSampleVorbis sampleVorbis;

		/// <summary>
		/// Option selected by the user
		/// </summary>
		int option;

		#endregion

		#region Methods

		/// <summary>
		/// The engine calls this method after creating the device
		/// </summary>
		/// <param name="form">Form used as window</param>
		/// <returns>True if everything went right</returns>
		public override bool InitGame(Form form)
		{
			// Create the fonts and texture objects
			Jad.AfterCreateDevice();

			// Load default textures, scenes, shaders,...
			Jad.LoadCreateInHouse();

			// Create a cube

			JMesh cubeMesh = JMesh.CreateCube(1, 1, 1);

			JMeshObject cube = Jad.Scene.MeshObjects.Create("room", false);

			cube.Mesh = cubeMesh;

			JMaterial[] material = new JMaterial[1];

			material[0] = Jad.Scene.Materials.Create();

			JMaterialLayer layer = new JMaterialLayer();

			material[0].AddLayer(layer);

			layer.DiffuseMap.Texture = Jad.Video.Textures.Create2D("brick_d", true);

			cube.Material = material;

			// Set ambient light
			Jad.Scene.Lights.Ambient = new JColor(0.6f, 0.6f, 0.6f);

			#region Sound

			// Create the sound, setting the sound priority and quality (for 3D sound)
			Jad.CreateSound(CooperativeLevel.Priority, JSoundQuality.High);

			// Create the 2D sample
			sample = Jad.Sound.Create("hola.wav");

			// Create the 3D sample
			sample3D = Jad.Sound.Create3D("hola.wav", Mode3D.Normal);

			// Set the positionof our object
			sample3D.Buffer3D.Position = new Vector3(0, 0, 0);

			// Load a sound in ogg format and we load it using streaming
			// instead of loading and decompressing everything in memory
			//sampleVorbis = Jad.Sound.CreateVorbis("moonlight-movement.ogg", true);

			// Play the sound, doing looping
			//sampleVorbis.Play(true);

			#endregion

			// Set the camera
			Jad.Scene.Camera.Transform.Position = new Vector3(0, 0, -2f);

			// Everything is set, start the engine
			Jad.Begin();

			// Create input class to manage the keyboard and the mouse
			Jad.CreateInput();

			return true;
		}

		/// <summary>
		/// This method is called every frame and it´s where the input
		/// events and the game action must be placed
		/// </summary>
		/// <returns>True to continue, false to exit the application</returns>
		public override bool Update()
		{
			// Exit the app?
			if (Jad.Input.Keyboard[Key.Escape].Down) return false;

			if (Jad.Input.Keyboard[Key.F1].Pressed) showHelp = !showHelp;

			// Gizmos

			if (Jad.Input.Keyboard[Key.G].Pressed)
				Jad.Scene.ShowGizmos = !Jad.Scene.ShowGizmos;

			//// M activates/deactivates the music

			//if (Jad.Input.Keyboard[Key.M].Pressed)
			//{
			//    if (sampleVorbis.IsPlaying) sampleVorbis.Stop();
			//    else sampleVorbis.Play(true);
			//}

			// If we press key 1 we activate the 2D sound
			if (Jad.Input.Keyboard[Key.D1].Pressed)
			{
				option = 0;

				sample3D.Stop();

				// Deactivate the effects
				sample.SetEffects(null);

				sample.Play(true);

				// Set the sample where we are going to apply the effects
				sampleSelected = sample;

			}

			// If we press key 2 we activate the 3D sound
			if (Jad.Input.Keyboard[Key.D2].Pressed)
			{
				option = 1;
				sample.Stop();
				sample.SetEffects(null);
				sample3D.Play(true);

				// Set the sample where we are going to apply the effects
				sampleSelected = sample3D;
			}

			// Activate the echo effect
			if (Jad.Input.Keyboard[Key.E].Pressed && sampleSelected != null)
			{
				// If sound is playing, we have to stop it and play it again

				bool isPlaying = sampleSelected.IsPlaying;
				if (sampleSelected.IsPlaying) sampleSelected.Stop();

				// Create an array with 1 effect
				Microsoft.DirectX.DirectSound.EffectDescription[] effects = new Microsoft.DirectX.DirectSound.EffectDescription[1];

				effects[0].GuidEffectClass = DSoundHelper.StandardEchoGuid;

				// Set the effect
				sampleSelected.SetEffects(effects);

				// Set the parameters of this effect
				EchoEffect echoFx = (EchoEffect) sampleSelected.Buffer.GetEffects(0);
				EffectsEcho echoParams = echoFx.AllParameters;
				echoParams.LeftDelay = 500;
				echoParams.RightDelay = 500;
				echoParams.PanDelay = 0;
				echoParams.WetDryMix = 50f;
				echoParams.Feedback = 50f;
				echoFx.AllParameters = echoParams;

				// Activate the sound again. If it wasn´t playing, we start from the begining, and if it was
				// playing we resume it
				if (!isPlaying) sampleSelected.Play(true);
				else sampleSelected.PlayFromCurrentPosition();
			}

			// Everything was right, continue
			return true;
		}

		/// <summary>
		/// Render method
		/// </summary>
		public override void Render()
		{
			JView backbuffer = Jad.Video.Views[0];

			// Render the Jad.Scene using the scene camera
			backbuffer.Render();

			if (showHelp)
			{
				// Get access to the 1st font. The engine creates a font by default
				JFont font = Jad.Video.Fonts[0];

				// Start fonts rendering
				Jad.Video.Fonts.Begin();

				// Write text in the first line, position x=0, yellow color
				font.RenderLine(Jad.Version + ". Press ESC to exit. Press G to Show/Hide Gizmos. F1 show/hide text", 0, JColor.Yellow);

				// Write text in the second line, position x=0, white color
				font.RenderLine(Jad.Video.Views.CurrentView.Stats.Fps + " FPS", 0, JColor.White);

				font.RenderLine(JMath.Vector3ToString(Jad.Scene.Camera.Transform.Position), 0, JColor.White);

				font.RenderLine("Press E to activate echo effect. Press 1 for 2D sound, 2 for 3D sound", 0, JColor.White);

				//font.RenderLine("Press M to activate/deactivate the music", 0, JColor.White);

				if (option == 1)
					font.RenderLine("Move with WASD keys to listen the 5.1 effect", 0, JColor.White);

				// End fonts rendering
				Jad.Video.Fonts.End();
			}

			// Show the backbuffer
			backbuffer.Present();
		}

		#endregion
	}
}

Last edited Mar 8, 2007 at 12:22 PM by Vicente, version 5

Comments

No comments yet.