add docs for game
This commit is contained in:
@@ -14,10 +14,16 @@ namespace PresenterConsole;
|
||||
|
||||
public class ConsolePresenter : IPresenter
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public event Action<ResizeEventArgs>? OnResize;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Width { get; private set; } = 2;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Height { get; private set; } = 1;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsExiting { get; private set; }
|
||||
|
||||
private static readonly char[] LIGHTMAP = " .,:;=*#%@".Reverse().ToArray();
|
||||
@@ -27,8 +33,8 @@ public class ConsolePresenter : IPresenter
|
||||
private readonly Engine.Graphics.Shader.Program _asciiProgram;
|
||||
private readonly Framebuffer _framebuffer;
|
||||
private readonly IndexBuffer _indexBuffer;
|
||||
private readonly VertexArray _vertexArray;
|
||||
private readonly VertexBuffer<AsciiVertex> _vertexBuffer;
|
||||
private readonly VertexArray _vertexArray;
|
||||
|
||||
private Image<AsciiPixel>? _asciiImage;
|
||||
|
||||
@@ -62,8 +68,6 @@ public class ConsolePresenter : IPresenter
|
||||
{
|
||||
var openglTexture = (Texture)parTexture;
|
||||
|
||||
// GL.Viewport(0, 0, Width / 2, Height);
|
||||
|
||||
_framebuffer.Bind();
|
||||
|
||||
openglTexture.BindUnit();
|
||||
|
||||
@@ -8,38 +8,101 @@ using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace PresenterConsole;
|
||||
|
||||
/// <summary>
|
||||
/// Provides Windows FFI (Foreign Function Interface) functionality to interact with the Windows API
|
||||
/// for various operations such as file handling, console operations, keyboard state management, and window handling.
|
||||
/// </summary>
|
||||
public static partial class WindowsFFI
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a coordinate with X and Y values, typically used for positioning or grid-like structures.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Coord(short parX, short parY)
|
||||
{
|
||||
/// <summary>
|
||||
/// The X coordinate.
|
||||
/// </summary>
|
||||
public short X = parX;
|
||||
|
||||
/// <summary>
|
||||
/// The Y coordinate.
|
||||
/// </summary>
|
||||
public short Y = parY;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A union structure that can hold either a Unicode or an ASCII character at the same memory location.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct CharUnion
|
||||
{
|
||||
/// <summary>
|
||||
/// The Unicode character value.
|
||||
/// </summary>
|
||||
[FieldOffset(0)] public char UnicodeChar;
|
||||
|
||||
/// <summary>
|
||||
/// The ASCII character value.
|
||||
/// </summary>
|
||||
[FieldOffset(0)] public byte AsciiChar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a character and its associated attributes in the console output, including the character's color.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct CharInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// The character.
|
||||
/// </summary>
|
||||
[FieldOffset(0)] public CharUnion Char;
|
||||
|
||||
/// <summary>
|
||||
/// The attributes associated with the character (e.g., color).
|
||||
/// </summary>
|
||||
[FieldOffset(2)] public short Attributes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a rectangle in terms of its left, top, right, and bottom coordinates, used for defining the bounds of a console screen area.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SmallRect
|
||||
{
|
||||
/// <summary>
|
||||
/// The left coordinate of the rectangle.
|
||||
/// </summary>
|
||||
public short Left;
|
||||
|
||||
/// <summary>
|
||||
/// The top coordinate of the rectangle.
|
||||
/// </summary>
|
||||
public short Top;
|
||||
|
||||
/// <summary>
|
||||
/// The right coordinate of the rectangle.
|
||||
/// </summary>
|
||||
public short Right;
|
||||
|
||||
/// <summary>
|
||||
/// The bottom coordinate of the rectangle.
|
||||
/// </summary>
|
||||
public short Bottom;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses the kernel32.dll's CreateFileW function to open or create a file with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="parFileName">The name of the file to open or create.</param>
|
||||
/// <param name="parFileAccess">The requested access to the file.</param>
|
||||
/// <param name="parFileShare">The sharing mode for the file.</param>
|
||||
/// <param name="parSecurityAttributes">Security attributes for the file.</param>
|
||||
/// <param name="parCreationDisposition">Action to take when opening the file.</param>
|
||||
/// <param name="parFlags">File-specific flags.</param>
|
||||
/// <param name="parTemplate">An optional template file.</param>
|
||||
/// <returns>A handle to the opened or created file.</returns>
|
||||
[LibraryImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileW")]
|
||||
public static partial SafeFileHandle CreateFile(
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string parFileName,
|
||||
@@ -51,6 +114,15 @@ public static partial class WindowsFFI
|
||||
IntPtr parTemplate
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Uses the kernel32.dll's WriteConsoleOutputW function to write characters to the console output at the specified coordinates.
|
||||
/// </summary>
|
||||
/// <param name="parHConsoleOutput">A handle to the console output.</param>
|
||||
/// <param name="parLpBuffer">The buffer containing the characters and their attributes to be written.</param>
|
||||
/// <param name="parDwBufferSize">The size of the buffer.</param>
|
||||
/// <param name="parDwBufferCoord">The coordinates of the starting position in the console buffer.</param>
|
||||
/// <param name="parLpWriteRegion">The region of the console buffer to write to.</param>
|
||||
/// <returns>True if the operation was successful, otherwise false.</returns>
|
||||
[LibraryImport("kernel32.dll", SetLastError = true, EntryPoint = "WriteConsoleOutputW")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool WriteConsoleOutput(
|
||||
@@ -62,32 +134,61 @@ public static partial class WindowsFFI
|
||||
ref SmallRect parLpWriteRegion
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Uses the user32.dll's GetKeyboardState function to retrieve the state of the keyboard keys.
|
||||
/// </summary>
|
||||
/// <param name="parKeyboardState">An array to receive the current state of the keyboard keys.</param>
|
||||
/// <returns>True if the operation was successful, otherwise false.</returns>
|
||||
[LibraryImport("user32.dll", SetLastError = true, EntryPoint = "GetKeyboardState")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool GetKeyboardState(byte[] parKeyboardState);
|
||||
|
||||
/// <summary>
|
||||
/// Uses the user32.dll's GetAsyncKeyState function to retrieve the state of a specific key.
|
||||
/// </summary>
|
||||
/// <param name="parKeyCode">The key code for the key whose state is to be retrieved.</param>
|
||||
/// <returns>A short value representing the key's state.</returns>
|
||||
[LibraryImport("user32.dll", SetLastError = true, EntryPoint = "GetAsyncKeyState")]
|
||||
public static partial short GetAsyncKeyState(int parKeyCode);
|
||||
|
||||
/// <summary>
|
||||
/// Uses the user32.dll's GetForegroundWindow function to retrieve the handle of the foreground window.
|
||||
/// </summary>
|
||||
/// <returns>The handle of the foreground window.</returns>
|
||||
[LibraryImport("user32.dll", SetLastError = true, EntryPoint = "GetForegroundWindow")]
|
||||
public static partial IntPtr GetForegroundWindow();
|
||||
|
||||
/// <summary>
|
||||
/// Uses the user32.dll's GetWindowThreadProcessId function to retrieve the process ID of the thread that owns a specified window.
|
||||
/// </summary>
|
||||
/// <param name="parHwnd">The handle to the window.</param>
|
||||
/// <param name="parLpdwProcessId">An optional pointer to receive the process ID of the window's thread.</param>
|
||||
/// <returns>The thread ID of the window.</returns>
|
||||
[LibraryImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowThreadProcessId")]
|
||||
public static partial uint GetWindowThreadProcessId(IntPtr parHwnd, IntPtr parLpdwProcessId);
|
||||
|
||||
/// <summary>
|
||||
/// Uses the user32.dll's GetKeyboardLayout function to retrieve the input locale identifier for the current thread.
|
||||
/// </summary>
|
||||
/// <param name="parThreadId">The ID of the thread for which to retrieve the keyboard layout.</param>
|
||||
/// <returns>The handle to the keyboard layout for the thread.</returns>
|
||||
[LibraryImport("user32.dll", SetLastError = true, EntryPoint = "GetKeyboardLayout")]
|
||||
public static partial IntPtr GetKeyboardLayout(uint parThreadId);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the current keyboard layout for the foreground window's process. Assumes English (1033) if an error occurs.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="CultureInfo"/> object representing the current keyboard layout.</returns>
|
||||
public static CultureInfo GetCurrentKeyboardLayout()
|
||||
{
|
||||
try
|
||||
{
|
||||
IntPtr foregroundWindow = GetForegroundWindow();
|
||||
uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
|
||||
int keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
|
||||
var foregroundWindow = GetForegroundWindow();
|
||||
var foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
|
||||
var keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
|
||||
return new CultureInfo(keyboardLayout);
|
||||
}
|
||||
catch (Exception _)
|
||||
catch (Exception)
|
||||
{
|
||||
return new CultureInfo(1033); // Assume English if something went wrong.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user