PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Multimedia/Tests/Tutorials.cs

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