diff --git a/testexetrisathlon.sln b/testexetrisathlon.sln index 92330b3..536dc97 100644 --- a/testexetrisathlon.sln +++ b/testexetrisathlon.sln @@ -6,14 +6,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testexetrisathlon", "testex EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Debug|x86.ActiveCfg = Debug|x86 - {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Debug|x86.Build.0 = Debug|x86 - {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Release|x86.ActiveCfg = Release|x86 - {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Release|x86.Build.0 = Release|x86 + {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/testexetrisathlon/Program.cs b/testexetrisathlon/Program.cs index abc0515..a5ee897 100644 --- a/testexetrisathlon/Program.cs +++ b/testexetrisathlon/Program.cs @@ -1,11 +1,12 @@ #define WINDOWS using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Media; using System.Reflection; using System.Runtime.InteropServices; +using testexetrisathlon.SoundManagement; using static System.Console; //┌─┐ @@ -32,6 +33,16 @@ namespace testexetrisathlon internal static class Program { public const string Sqr = "■"; + + /*private static readonly WaveStream Intro = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.Intro.wav")); + private static readonly WaveStream InGame1 = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.InGame1.wav")); + private static readonly WaveStream InGame2 = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.InGame2.wav")); + private static readonly WaveStream GameOver = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.GameOver.wav")); + private static WaveStream _inGame = SettingsMan.UsingAltTrack ? InGame2 : InGame1; + private static WaveStream _current = Intro; + private static WaveOutEvent _output = new WaveOutEvent();*/ + private const string Intro = "Intro"; + private const string GameOver = "GameOver"; public static int[,] Grid = new int[23, 10]; public static int[,] DroppedTetrominoeLocationGrid = new int[23, 10]; private static Stopwatch _dropTimer = new Stopwatch(); @@ -48,16 +59,10 @@ namespace testexetrisathlon private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); private static readonly ConsoleColor[] Colors = {BackgroundColor, ForegroundColor}; public static bool Debug; + public static readonly Random Rnd = new Random(); - private static readonly SoundPlayer Intro = - new SoundPlayer(Assembly.GetManifestResourceStream("testexetrisathlon.Intro.wav")); - private static readonly SoundPlayer InGame1 = - new SoundPlayer(Assembly.GetManifestResourceStream("testexetrisathlon.InGame1.wav")); - private static readonly SoundPlayer InGame2 = - new SoundPlayer(Assembly.GetManifestResourceStream("testexetrisathlon.InGame2.wav")); - private static readonly SoundPlayer GameOver = - new SoundPlayer(Assembly.GetManifestResourceStream("testexetrisathlon.GameOver.wav")); - private static SoundPlayer _inGame = SettingsMan.UsingAltTrack ? InGame2 : InGame1; + private static ISoundManager soundManager; + private static string InGame => SettingsMan.UsingAltTrack ? "InGame2" : "InGame1"; #if WINDOWS [DllImport("winmm.dll")] private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume); @@ -66,6 +71,14 @@ namespace testexetrisathlon #if DEBUG private static void Main() { + soundManager = new WindowsSoundManager(); + soundManager.Init(new Dictionary + { + {"Intro", "testexetrisathlon.Intro.wav"}, + {"InGame1", "testexetrisathlon.InGame1.wav"}, + {"InGame2", "testexetrisathlon.InGame2.wav"}, + {"GameOver", "testexetrisathlon.GameOver.wav"} + }); Debug = true; #else private static void Main(string[] args) @@ -92,8 +105,7 @@ namespace testexetrisathlon { case GameState.Menu: Clear(); - GameOver.Stop(); - Intro.PlayLooping(); + soundManager.SetCurrent(Intro); DrawSymbol(); SetCursorPosition(12, 18); Write("HighScore: " + SettingsMan.HighScore); @@ -115,7 +127,6 @@ namespace testexetrisathlon switch (tmp) { case "s": - Intro.Stop(); state = GameState.Game; Clear(); DrawBorder(); @@ -129,7 +140,7 @@ namespace testexetrisathlon } break; case GameState.Game: - _inGame.PlayLooping(); + soundManager.SetCurrent(InGame); _dropTimer.Start(); SetCursorPosition(25, 0); WriteLine("Level " + _level); @@ -144,12 +155,11 @@ namespace testexetrisathlon _tet.Spawn(); _nextTet = new Tetrominoe(); Update(); - _inGame.Stop(); state = GameState.GameOver; break; case GameState.GameOver: SettingsMan.HighScore = _score; - GameOver.PlayLooping(); + soundManager.SetCurrent(GameOver); string input = ""; while (input != "y" && input != "n") { @@ -185,10 +195,7 @@ namespace testexetrisathlon } finally { - Intro.Dispose(); - InGame1.Dispose(); - InGame2.Dispose(); - GameOver.Dispose(); + soundManager.Dispose(); } BackgroundColor = Colors[0]; ForegroundColor = Colors[1]; @@ -274,11 +281,7 @@ namespace testexetrisathlon } break; } -#if WINDOWS - int newVolume = (ushort.MaxValue / 10) * SettingsMan.Volume; - waveOutSetVolume(IntPtr.Zero, ((uint) newVolume & 0x0000ffff) | ((uint) newVolume << 16)); -#endif - _inGame = SettingsMan.UsingAltTrack ? InGame2 : InGame1; + soundManager.SetVolume(SettingsMan.Volume * 10); } } @@ -365,11 +368,9 @@ namespace testexetrisathlon if ((_key.Key == ConsoleKey.UpArrow) & _isKeyPressed) for (; _tet.IsSomethingBelow != true;) _tet.Drop(); - if ((_key.Key == ConsoleKey.Spacebar) & _isKeyPressed) - { - _tet.Rotate(); - _tet.Update(); - } + if (!((_key.Key == ConsoleKey.Spacebar) & _isKeyPressed)) return; + _tet.Rotate(); + _tet.Update(); } public static void Draw() diff --git a/testexetrisathlon/SoundManagement/ISoundManager.cs b/testexetrisathlon/SoundManagement/ISoundManager.cs new file mode 100644 index 0000000..7616462 --- /dev/null +++ b/testexetrisathlon/SoundManagement/ISoundManager.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +namespace testexetrisathlon.SoundManagement +{ + public interface ISoundManager : IDisposable + { + public void Init(Dictionary manifestResources); + public void SetCurrent(string id); + public void SetVolume(int percent); + } +} \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/LoopStream.cs b/testexetrisathlon/SoundManagement/LoopStream.cs new file mode 100644 index 0000000..3ea3255 --- /dev/null +++ b/testexetrisathlon/SoundManagement/LoopStream.cs @@ -0,0 +1,48 @@ +using NAudio.Wave; + +namespace testexetrisathlon.SoundManagement +{ + public class LoopStream : WaveStream + { + private readonly WaveStream _sourceStream; + + public LoopStream(WaveStream sourceStream) + { + _sourceStream = sourceStream; + EnableLooping = true; + } + + private bool EnableLooping { get; } + public override WaveFormat WaveFormat => _sourceStream.WaveFormat; + public override long Length => _sourceStream.Length; + + public override long Position + { + get => _sourceStream.Position; + set => _sourceStream.Position = value; + } + + public override int Read(byte[] buffer, int offset, int count) + { + int totalBytesRead = 0; + + while (totalBytesRead < count) + { + int bytesRead = _sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead); + if (bytesRead == 0) + { + if (_sourceStream.Position == 0 || !EnableLooping) break; + _sourceStream.Position = 0; + } + totalBytesRead += bytesRead; + } + return totalBytesRead; + } + + public new void Dispose() + { + base.Dispose(); + _sourceStream.Dispose(); + } + } +} \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/WindowsSoundManager.cs b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs new file mode 100644 index 0000000..7e03430 --- /dev/null +++ b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using NAudio.Wave; + +namespace testexetrisathlon.SoundManagement +{ + public sealed class WindowsSoundManager : ISoundManager + { + private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); + private static WaveOutEvent _output; + private static WaveStream _current; + private static string _currentId; + private Dictionary _loadedSounds; + + public void Init(Dictionary manifestResources) + { + _output = new WaveOutEvent(); + _loadedSounds = manifestResources.ToDictionary(s => s.Key, + s => new LoopStream(new WaveFileReader(Assembly.GetManifestResourceStream(s.Value)))); + } + + public void SetCurrent(string id) + { + if (_currentId == id) return; + _currentId = id; + if (_current != null) + _current.Position = 0; + _current = _loadedSounds[id]; + _output.Stop(); + _output.Init(_current); + _output.Play(); + } + + public void SetVolume(int percent) => _output.Volume = percent / 100f; + + public void Dispose() + { + foreach (LoopStream reader in _loadedSounds.Values) reader.Dispose(); + _output.Dispose(); + } + } +} \ No newline at end of file diff --git a/testexetrisathlon/testexetrisathlon.csproj b/testexetrisathlon/testexetrisathlon.csproj index af10317..c7b6709 100644 --- a/testexetrisathlon/testexetrisathlon.csproj +++ b/testexetrisathlon/testexetrisathlon.csproj @@ -1,94 +1,29 @@ - - + - Debug + netcoreapp3.1 x86 - {6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59} Exe - testexetrisathlon - testexetrisathlon - v4.6.1 - 8 enable + false + false - - true - full - false - bin\Debug - DEBUG; - prompt - 4 - true - x86 - - - true - bin\Release - prompt - 4 - true - x86 - - - true - bin\Debug\ - DEBUG; - full - AnyCPU - prompt - MinimumRecommendedRules.ruleset - true - - - bin\Release\ - true - AnyCPU - prompt - MinimumRecommendedRules.ruleset - true - - - true - bin\x64\Debug\ - DEBUG; - full - x64 - prompt - MinimumRecommendedRules.ruleset - true - - - bin\x64\Release\ - true - x64 - prompt - MinimumRecommendedRules.ruleset - true + + if exist "$(SolutionDir)Data\pkgtool.exe" ($(SolutionDir)Data\pkgtool.exe build --noLogo --binDir .) else if exist "%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" ("%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" build --noLogo --binDir .) else echo Cound not find Package build tools, skipping tetris_yUxH6t_256px.ico - - - - - - - - - - - - + + + \ No newline at end of file