NET Core, needs to be tested on other platforms before release

This commit is contained in:
CreepyCrafter24 2020-03-16 22:31:26 +01:00
parent 02f197ef72
commit 64f0a3f302
6 changed files with 149 additions and 110 deletions

View File

@ -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

View File

@ -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<string, string>
{
{"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()

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
namespace testexetrisathlon.SoundManagement
{
public interface ISoundManager : IDisposable
{
public void Init(Dictionary<string, string> manifestResources);
public void SetCurrent(string id);
public void SetVolume(int percent);
}
}

View File

@ -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();
}
}
}

View File

@ -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<string, LoopStream> _loadedSounds;
public void Init(Dictionary<string, string> 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();
}
}
}

View File

@ -1,94 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{6B0BD176-D6E3-49F5-8CA1-AE5F1535AC59}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>testexetrisathlon</RootNamespace>
<AssemblyName>testexetrisathlon</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkProfile />
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Deterministic>false</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
<Optimize>true</Optimize>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<Optimize>true</Optimize>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
<PropertyGroup>
<PostBuildEvent>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</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>tetris_yUxH6t_256px.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingsMan.cs" />
<Compile Include="Tetrominoe.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Intro.wav" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="GameOver.wav" />
<EmbeddedResource Include="InGame1.wav" />
<EmbeddedResource Include="InGame2.wav" />
<Content Include="tetris_yUxH6t_256px.ico" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<PackageReference Include="NAudio" Version="1.10.0" />
</ItemGroup>
</Project>