Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 148 additions & 23 deletions ConsoleEngine.sln

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions ConsoleEngine/ConsoleEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConsoleEngine.Abstractions\ConsoleEngine.Abstractions.csproj" />
<ProjectReference Include="..\Libs\ConsoleEngine.CrossPlatform\ConsoleEngine.CrossPlatform.csproj" />
<ProjectReference Include="..\Libs\ConsoleEngine.Logger\ConsoleEngine.Logger.csproj" />
<ProjectReference Include="..\Libs\Microsoft.Xna.Framework\Microsoft.Xna.Framework.csproj" />
<ProjectReference Include="..\Libs\ConsoleEngine.Windows\ConsoleEngine.Windows.csproj" />
</ItemGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<DefineConstants>$(DefineConstants);WINDOWS_PLATFORM</DefineConstants>
</PropertyGroup>
</Project>
5 changes: 5 additions & 0 deletions ConsoleEngine/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ConsoleEngine.Infrastructure.Rendering;
using ConsoleEngine.Infrastructure.Scenery;
using ConsoleEngine.Native;
using ConsoleEngine.CrossPlatform;

namespace ConsoleEngine;

Expand All @@ -32,7 +33,11 @@ protected GameBase(int width, int height, int fontWidth, int fontHeight)

protected GameBase(int width, int height, FontInfo fontInfo)
{
#if WINDOWS_PLATFORM
_console = new RenderConsole(new ConsoleHandler(width, height, fontInfo));
#else
_console = new RenderConsole(new CrossPlatformConsoleHandler(width, height, fontInfo));
#endif
Name = "Game";
Scenes = new SceneManager(this);
}
Expand Down
8 changes: 7 additions & 1 deletion ConsoleEngine/Infrastructure/Inputs/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using ConsoleEngine.Abstractions.Inputs;
using ConsoleEngine.Native;
using ConsoleEngine.CrossPlatform;

namespace ConsoleEngine.Infrastructure.Inputs;

Expand All @@ -13,8 +14,13 @@ public class Input
//**********************************************************

public static readonly Input Instance = new();

#if WINDOWS_PLATFORM
private static readonly IInputHandler Handler = new InputHandler();

#else
private static readonly IInputHandler Handler = new CrossPlatformInputHandler();
#endif

//**********************************************************
//** ctor:
//**********************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<RootNamespace>ConsoleEngine.CrossPlatform</RootNamespace>
<Configurations>Release;Debug</Configurations>
<Platforms>AnyCPU</Platforms>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<DefineConstants>$(DefineConstants);WINDOWS_PLATFORM</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\ConsoleEngine.Abstractions\ConsoleEngine.Abstractions.csproj" />
</ItemGroup>
</Project>

68 changes: 68 additions & 0 deletions Libs/ConsoleEngine.CrossPlatform/CrossPlatformConsoleHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using ConsoleEngine.Abstractions.Rendering;

namespace ConsoleEngine.CrossPlatform;

public sealed class CrossPlatformConsoleHandler : IConsoleHandler
{
private readonly FontInfo _fontInfo;
public CrossPlatformConsoleHandler(int width, int height, FontInfo fontInfo)
{
_fontInfo = fontInfo;
Width = width;
Height = height;
}

public int Width { get; }
public int Height { get; }

public void InitializeConsole()
{
Console.Clear();
SetFont(_fontInfo);
}

public void SetTitle(string title)
{
Console.Title = title;
}

public void SetCursorVisible(bool visible)
{
try { Console.CursorVisible = visible; } catch { }
}

public void Resizable(bool resizable)
{
// Not yet supported
}

public void SetFont(FontInfo fontInfo)
{
// Not yet supported
}

public void Render(Span<Pixel> pixels)
{
Console.SetCursorPosition(0, 0);
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
int i = y * Width + x;
if (i < pixels.Length)
Console.Write(pixels[i].Char);
else
Console.Write(' ');
}
Console.WriteLine();
}
}

public void Close()
{
// Not yet supported
}

private static ushort GetColorValue(ConsoleColor fg, ConsoleColor bg) => (ushort)((int)fg | ((int)bg<<4));
}
50 changes: 50 additions & 0 deletions Libs/ConsoleEngine.CrossPlatform/CrossPlatformInputHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ConsoleEngine.Abstractions.Inputs;

namespace ConsoleEngine.CrossPlatform;

public class CrossPlatformInputHandler : IInputHandler
{
private readonly int[] _previousState = new int[256];
private readonly int[] _newState = new int[256];
private readonly KeyState[] _keyStates = new KeyState[256];

public KeyState GetKey(int id)
{
return _keyStates[id];
}

public IEnumerable<int> GetPressedKeyCodes() => _keyStates.Where(k => k.Pressed).Select(k => k.Index);

public void Update()
{
bool[] pressedThisFrame = new bool[_keyStates.Length];
while (Console.KeyAvailable)
{
var keyInfo = Console.ReadKey(true);
int key = (int)keyInfo.Key;
_keyStates[key].Pressed = true;
_keyStates[key].Held = true;
_keyStates[key].Index = key;
pressedThisFrame[key] = true;
}
for (int i = 0; i < _keyStates.Length; i++)
{
if (!pressedThisFrame[i])
{
if (_keyStates[i].Held)
{
_keyStates[i].Released = true;
}
else
{
_keyStates[i].Released = false;
}
_keyStates[i].Held = false;
_keyStates[i].Pressed = false;
}
}
}
}
5 changes: 4 additions & 1 deletion Libs/ConsoleEngine.Windows/ConsoleEngine.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
<Platforms>AnyCPU</Platforms>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<DefineConstants>$(DefineConstants);WINDOWS_PLATFORM</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\ConsoleEngine.Abstractions\ConsoleEngine.Abstractions.csproj" />
</ItemGroup>
</Project>

21 changes: 21 additions & 0 deletions Libs/ConsoleEngine.Windows/LowLevel/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ConsoleEngine.Native.LowLevel;
/// </summary>
internal static class Kernel32
{
#if WINDOWS_PLATFORM
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr GetStdHandle(StdHandle nStdHandle);

Expand Down Expand Up @@ -67,4 +68,24 @@ internal static class Kernel32
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr hObject);
#else
// Unix/macOS/Linux
// Can´t deal with Kernel32 calls, so void them
internal static IntPtr GetStdHandle(StdHandle nStdHandle) => IntPtr.Zero;
internal static bool SetConsoleActiveScreenBuffer(IntPtr hConsoleOutput) => false;
internal static IntPtr CreateConsoleScreenBuffer(GenericRights dwDesiredAccess, uint dwShareMode, IntPtr securityAttributes, uint flags, IntPtr screenBufferData) => IntPtr.Zero;
internal static bool WriteConsoleOutputCharacter(IntPtr screenBuffer, string characters, uint length, Coord writeCoord, out UInt32 numCharsWritten) { numCharsWritten = 0; return false; }
internal static bool WriteConsoleOutput(IntPtr hConsoleOutput, CharInfo[] lpBuffer, Coord dwBufferSize, Coord dwBufferCoord, ref SmallRect lpWriteRegion) => false;
internal static IntPtr GetConsoleWindow() => IntPtr.Zero;
internal static int SetCurrentConsoleFontEx(IntPtr consoleOutput, bool maximumWindow, ref ConsoleFontInfoEx consoleCurrentFontEx) => 0;
internal static bool SetConsoleWindowInfo(IntPtr hConsoleOutput, bool bAbsolute, ref SmallRect lpConsoleWindow) => false;
internal static bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, Coord dwSize) => false;
internal static int GetAsyncKeyState(int vKeys) => 0;
internal static bool SetConsoleOutputCP(uint wCodePageID) => false;
internal static bool SetConsoleCP(uint wCodePageID) => false;
internal static bool SetConsoleTitle(char[] lpConsoleTitle) { try { Console.Title = new string(lpConsoleTitle); return true; } catch { return false; } }
internal static bool GetConsoleCursorInfo(IntPtr hConsoleOutput, out ConsoleCursorInfo lpConsoleCursorInfo) { lpConsoleCursorInfo = default; return false; }
internal static bool SetConsoleCursorInfo(IntPtr hConsoleOutput, ref ConsoleCursorInfo lpConsoleCursorInfo) => false;
internal static bool CloseHandle(IntPtr hObject) => false;
#endif
}
Loading