diff --git a/testexetrisathlon/Program.cs b/testexetrisathlon/Program.cs index a5ee897..b7d1ed6 100644 --- a/testexetrisathlon/Program.cs +++ b/testexetrisathlon/Program.cs @@ -1,4 +1,4 @@ -#define WINDOWS +//#define WINDOWS using System; using System.Collections.Generic; @@ -33,14 +33,6 @@ 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]; @@ -63,15 +55,17 @@ namespace testexetrisathlon public static readonly Random Rnd = new Random(); 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); -#endif #if DEBUG private static void Main() { - soundManager = new WindowsSoundManager(); + soundManager = new +#if WINDOW + WindowsSoundManager +#else + LinuxSoundManager +#endif + (); soundManager.Init(new Dictionary { {"Intro", "testexetrisathlon.Intro.wav"}, @@ -87,15 +81,13 @@ namespace testexetrisathlon #endif BackgroundColor = ConsoleColor.Red; ForegroundColor = ConsoleColor.Yellow; +#if WINDOWS SetWindowSize(42, 29); - SetCursorPosition(0, 0); - Clear(); if (Debug) SetWindowSize(50, 40); -#if WINDOWS - int newVolume = (ushort.MaxValue / 10) * SettingsMan.Volume; - waveOutSetVolume(IntPtr.Zero, ((uint) newVolume & 0x0000ffff) | ((uint) newVolume << 16)); #endif + SetCursorPosition(0, 0); + Clear(); bool playing = true; GameState state = GameState.Menu; try @@ -325,7 +317,7 @@ namespace testexetrisathlon { _linesCleared++; combo++; - Beep(400, 200); + Beeper.Beep(400, 200); for (int j = 0; j < 10; j++) DroppedTetrominoeLocationGrid[i, j] = 0; int[,] newDroppedTetrominoeLocationGrid = new int[23, 10]; for (int k = 1; k < i; k++) diff --git a/testexetrisathlon/SoundManagement/Beeper.cs b/testexetrisathlon/SoundManagement/Beeper.cs new file mode 100644 index 0000000..b83d1e5 --- /dev/null +++ b/testexetrisathlon/SoundManagement/Beeper.cs @@ -0,0 +1,16 @@ +using System; + +namespace testexetrisathlon.SoundManagement +{ + public static class Beeper + { + public static void Beep(int frequency, int duration) + { +#if WINDOWS + Console.Beep(frequency, duration); +#else + Console.Write("\a"); +#endif + } + } +} \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/LinuxSoundManager.cs b/testexetrisathlon/SoundManagement/LinuxSoundManager.cs new file mode 100644 index 0000000..a8e26fb --- /dev/null +++ b/testexetrisathlon/SoundManagement/LinuxSoundManager.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Reflection; + +namespace testexetrisathlon.SoundManagement +{ + public class LinuxSoundManager : ISoundManager + { + private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); + private Dictionary _files; + private string _current; + private Process? alsa; + public void Dispose() + { + foreach (string file in _files.Values) File.Delete(file); + } + + public void Init(Dictionary manifestResources) + { + _files = new Dictionary(); + foreach ((string name, string key) in manifestResources) + { + string file = Path.GetTempFileName(); + File.Move(file, Path.ChangeExtension(file, "wav")); + file = Path.ChangeExtension(file, "wav"); + using Stream resource = Assembly.GetManifestResourceStream(key); + using FileStream fileStream = File.Create(file); + resource.Seek(0, SeekOrigin.Begin); + resource.CopyTo(fileStream); + _files.Add(name, file); + } + } + + public void SetCurrent(string id) + { + if (_current == id) return; + alsa?.Kill(); + _current = id; + //TODO fix actually killing, remove orphan processes + alsa = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "/bin/sh", + Arguments = $"-c \"while [ true ]; do aplay -q {_files[id].Replace("\"", "\\\"")}; done\"", + RedirectStandardOutput = false, + RedirectStandardInput = false, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + alsa.Start(); + } + + public void SetVolume(int percent) + { + + } + } +} \ No newline at end of file diff --git a/testexetrisathlon/SoundManagement/WindowsSoundManager.cs b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs index 7e03430..0136d2b 100644 --- a/testexetrisathlon/SoundManagement/WindowsSoundManager.cs +++ b/testexetrisathlon/SoundManagement/WindowsSoundManager.cs @@ -8,9 +8,9 @@ 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 WaveOutEvent _output; + private WaveStream _current; + private string _currentId; private Dictionary _loadedSounds; public void Init(Dictionary manifestResources) diff --git a/testexetrisathlon/Tetrominoe.cs b/testexetrisathlon/Tetrominoe.cs index c91aa1c..34d18a8 100644 --- a/testexetrisathlon/Tetrominoe.cs +++ b/testexetrisathlon/Tetrominoe.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using testexetrisathlon.SoundManagement; using static System.Console; namespace testexetrisathlon @@ -85,7 +86,7 @@ namespace testexetrisathlon { Location.ForEach(s => Program.DroppedTetrominoeLocationGrid[s[0], s[1]] = 1); Program.IsDropped = true; - Beep(800, 200); + Beeper.Beep(800, 200); } else { diff --git a/testexetrisathlon/testexetrisathlon.csproj b/testexetrisathlon/testexetrisathlon.csproj index c7b6709..cc5343d 100644 --- a/testexetrisathlon/testexetrisathlon.csproj +++ b/testexetrisathlon/testexetrisathlon.csproj @@ -1,7 +1,6 @@  netcoreapp3.1 - x86 Exe 8 enable @@ -9,7 +8,12 @@ false - 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 + + 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 + + + if [[ -f "$(SolutionDir)Data/pkgtool.exe" ]]; then $(SolutionDir)Data/pkgtool.exe build --noLogo --binDir .; else echo Cound not find Package build tools, skipping; fi + tetris_yUxH6t_256px.ico