PageRenderTime 100ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/Multimedia/Tests/Tutorials.cs

#
C# | 217 lines | 160 code | 21 blank | 36 comment | 10 complexity | 13f2527b79b995bcffe4e4ae225610e7 MD5 | raw file
  1using System;
  2using Delta.Engine;
  3using Delta.InputSystem;
  4using Delta.Rendering.Basics.Fonts;
  5using Delta.Utilities;
  6using Delta.Utilities.Datatypes;
  7using Delta.Utilities.Helpers;
  8using NUnit.Framework;
  9
 10namespace Delta.Multimedia.Tests
 11{
 12	/// <summary>
 13	/// Multimedia tutorials to perform simple multimedia tasks like playing back
 14	/// sounds, music or video files and manipulating properties while doing so.
 15	/// </summary>
 16	internal class Tutorials
 17	{
 18		#region TestSoundPlayback (Static)
 19		/// <summary>
 20		/// Tutorial: Multimedia Tutorial 1: Sound Playback
 21		/// Difficulty: Easy
 22		/// Description: Plays back the DefaultSound and allows to modify its
 23		/// volume. Accepts input from mouse, keyboard, game pads and touch.
 24		/// Image: http://DeltaEngine.net/Icons/Multimedia.png
 25		/// </summary>
 26		[Test, Category("Visual")]
 27		public static void TestSoundPlayback()
 28		{
 29			int currentVolume = 10;
 30
 31			Sound testSound = new Sound("DefaultSound");
 32			Application.Start(delegate
 33			{
 34				Application.Window.Title = "ActiveChannels=" + Sound.ActiveChannels;
 35
 36				Font.Default.Draw("Volume: " + (currentVolume * 10) + "%",
 37					Rectangle.FromCenter(Point.Half, Size.Half));
 38				Font.Default.Draw(
 39					"Click, Touch or Press Space or GamePad A to play sound\n" +
 40					"Use Up/Down to change volume.",
 41					Rectangle.FromCenter(
 42						Point.Half + new Point(0, Font.Default.LineHeight * 2),
 43						Size.Half));
 44
 45				if (Input.Touch.TouchReleased ||
 46				    Input.Mouse.LeftButtonReleased ||
 47				    Input.GamePad.AReleased ||
 48				    Input.Keyboard.SpaceReleased)
 49				{
 50					testSound.Volume = currentVolume / 10.0f;
 51					testSound.Play();
 52				}
 53
 54				// Volume Up
 55				if (Input.Keyboard.CursorUpReleased)
 56				{
 57					currentVolume = MathHelper.Min(currentVolume + 1, 10);
 58				}
 59
 60				// Volume Down
 61				if (Input.Keyboard.CursorDownReleased)
 62				{
 63					currentVolume = MathHelper.Max(currentVolume - 1, 0);
 64				}
 65			});
 66		}
 67		#endregion
 68
 69		#region TestMusicPlayback (Static)
 70		/// <summary>
 71		/// Tutorial: Multimedia Tutorial 2: Music Playback
 72		/// Difficulty: Easy
 73		/// Description: Plays the DefaultMusic file and allows to change the
 74		/// volume. Also displays some information about the music file.
 75		/// Image: http://DeltaEngine.net/Icons/Multimedia.png
 76		/// </summary>
 77		[Test, Category("Visual")]
 78		public static void TestMusicPlayback()
 79		{
 80			Music music = new Music("DefaultMusic");
 81
 82			Rectangle stateRect = Rectangle.FromCenter(Point.Half, Size.Half);
 83			Rectangle volumeRect = Rectangle.FromCenter(
 84				Point.Half + new Point(0, Font.Default.LineHeight), Size.Half);
 85			Rectangle timePlayedRect = Rectangle.FromCenter(
 86				Point.Half + new Point(0, Font.Default.LineHeight * 2), Size.Half);
 87			float currentVolume = 1.0f;
 88
 89			Application.Start(delegate
 90			{
 91				Font.Default.Draw(
 92					"Music state: " + music.State + " (press Space to start)",
 93					stateRect);
 94				Font.Default.Draw(
 95					"Volume: " + (currentVolume * 100) + "%" +" (use Up/Down to change)",
 96					volumeRect);
 97				Font.Default.Draw(
 98					"Time played: " + music.TimePlayed + " sec (use Enter to pause)",
 99					timePlayedRect);
100
101				#region Play
102				if (Input.Touch.TouchReleased ||
103				    Input.Keyboard.SpaceReleased)
104				{
105					music.Play();
106				}
107				#endregion
108
109				#region Stop
110				if (Input.Keyboard.EnterReleased)
111				{
112					music.Stop();
113				}
114				#endregion
115
116				#region Volume Up
117				if (Input.Keyboard.CursorUpReleased)
118				{
119					currentVolume = MathHelper.Min(currentVolume + 0.1f, 1.0f);
120					music.Volume = currentVolume;
121				}
122				#endregion
123
124				#region Volume Down
125				if (Input.Keyboard.CursorDownReleased)
126				{
127					currentVolume = MathHelper.Max(currentVolume - 0.1f, 0.0f);
128					music.Volume = currentVolume;
129				}
130				#endregion
131			});
132		}
133		#endregion
134
135		#region TestVideoPlayback (Static)
136		/// <summary>
137		/// Tutorial: Multimedia Tutorial 3: Video Playback
138		/// Difficulty: Easy
139		/// Description: Test Video playback, works on all platforms. The video
140		/// format is very different on each platform (.mpg, .wmv, .avi).
141		/// Image: http://DeltaEngine.net/Icons/Multimedia.png
142		/// </summary>
143		[Test, Category("Visual")]
144		public static void TestVideoPlayback()
145		{
146			// Make sure we have a video module setup, otherwise video playback will
147			// not work (Vlc is disabled by default on Windows, but should be set for
148			// this project).
149			Assert.False(String.IsNullOrEmpty(Settings.Modules.VideoModule),
150				"This test needs a video module setup in the settings to work!");
151
152			Video deltaIntro = Video.Create("DeltaEngineLogo");
153			deltaIntro.Ended += delegate
154			{
155				Log.Info(
156					"TestVideoPlayback: Playing intro has ended with state '" +
157					deltaIntro.State + "'.", false);
158			};
159
160			bool isFirstRun = true;
161			Application.Start(delegate
162			{
163				Font.DrawTopLeftInformation("Video state: " + deltaIntro.State);
164
165				if ((isFirstRun ||
166				     Input.Keyboard.SpaceReleased) &&
167				    deltaIntro.State != MediaState.Playing)
168				{
169					isFirstRun = false;
170
171					Log.Info("TestVideoPlayback: Start playing video", false);
172					deltaIntro.Play();
173				}
174			});
175		}
176		#endregion
177
178		#region TestPlaylistPlayback (Static)
179		/// <summary>
180		/// Tutorial: Multimedia Tutorial 4: Playlist Playback
181		/// Difficulty: Easy
182		/// Description: Test Music Playlist playback, works on all platforms.
183		/// Image: http://DeltaEngine.net/Icons/Multimedia.png
184		/// </summary>
185		[Test, Category("Visual")]
186		public static void TestPlaylistPlayback()
187		{
188			MusicPlaylist playlist = new MusicPlaylist();
189			playlist.Add(new Music("DefaultMusic"));
190			playlist.Add(new Music("DefaultMusic")
191			{
192				Volume = 0.5f,
193			});
194			playlist.Add(new Music("DefaultMusic"));
195			playlist.Add(new Music("DefaultMusic")
196			{
197				Volume = 0.5f,
198			});
199
200			bool isFirstRun = true;
201			Application.Start(delegate
202			{
203				Font.DrawTopLeftInformation(
204					"Playlist index: " +
205					(playlist.CurrentIndex + 1) + "/" + playlist.Count);
206
207				if (isFirstRun ||
208				    Input.Keyboard.SpaceReleased)
209				{
210					isFirstRun = false;
211					playlist.Play();
212				}
213			});
214		}
215		#endregion
216	}
217}