RynthCore/spikes/WglDxInterop/Win32.cs
Tom Bohar 68c301dac8 RynthCore as of 2026-09-05 (internal f9abab9)
A snapshot of the working tree for aelrynth.com/git/: the code, build files
and public docs. Internal review notes and agent instructions are left out.
MIT licensed; see LICENSE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 21:22:59 -04:00

141 lines
5 KiB
C#

// Win32 P/Invoke surface for the spike. Kept narrow — we only need window
// creation, a message loop, DC management, and pixel-format setup. All the
// GL-side P/Invokes live in Gl.cs; D3D9 P/Invokes live in D3D9.cs.
using System;
using System.Runtime.InteropServices;
namespace WglDxInterop;
internal static unsafe class Win32
{
public const int WS_OVERLAPPEDWINDOW = 0x00CF0000;
public const int WS_VISIBLE = 0x10000000;
public const int WS_POPUP = unchecked((int)0x80000000);
public const int CW_USEDEFAULT = unchecked((int)0x80000000);
public const int SW_SHOW = 5;
public const uint WM_DESTROY = 0x0002;
public const uint WM_QUIT = 0x0012;
public const uint WM_SIZE = 0x0005;
public const uint WM_CLOSE = 0x0010;
public const uint WM_PAINT = 0x000F;
public const int PFD_DRAW_TO_WINDOW = 0x00000004;
public const int PFD_SUPPORT_OPENGL = 0x00000020;
public const int PFD_DOUBLEBUFFER = 0x00000001;
public const int PFD_TYPE_RGBA = 0;
public const byte PFD_MAIN_PLANE = 0;
[StructLayout(LayoutKind.Sequential)]
public struct PIXELFORMATDESCRIPTOR
{
public ushort nSize;
public ushort nVersion;
public uint dwFlags;
public byte iPixelType;
public byte cColorBits;
public byte cRedBits, cRedShift;
public byte cGreenBits, cGreenShift;
public byte cBlueBits, cBlueShift;
public byte cAlphaBits, cAlphaShift;
public byte cAccumBits, cAccumRedBits, cAccumGreenBits, cAccumBlueBits, cAccumAlphaBits;
public byte cDepthBits, cStencilBits, cAuxBuffers;
public byte iLayerType;
public byte bReserved;
public uint dwLayerMask, dwVisibleMask, dwDamageMask;
}
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public IntPtr hwnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public int ptX;
public int ptY;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WNDCLASSEXW
{
public uint cbSize;
public uint style;
public IntPtr lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public IntPtr lpszMenuName;
public IntPtr lpszClassName;
public IntPtr hIconSm;
}
public delegate IntPtr WndProcDelegate(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern ushort RegisterClassExW(ref WNDCLASSEXW wc);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateWindowExW(
int dwExStyle, string lpClassName, string lpWindowName, int dwStyle,
int x, int y, int nWidth, int nHeight,
IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr DefWindowProcW(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyWindow(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("user32.dll")]
public static extern bool PeekMessageW(out MSG msg, IntPtr hwnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
[DllImport("user32.dll")]
public static extern bool TranslateMessage(ref MSG msg);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr DispatchMessageW(ref MSG msg);
[DllImport("user32.dll")]
public static extern void PostQuitMessage(int nExitCode);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hwnd, out RECT lpRect);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr GetModuleHandleW(string? lpModuleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr LoadLibraryA(string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("gdi32.dll")]
public static extern int ChoosePixelFormat(IntPtr hdc, ref PIXELFORMATDESCRIPTOR ppfd);
[DllImport("gdi32.dll")]
public static extern bool SetPixelFormat(IntPtr hdc, int format, ref PIXELFORMATDESCRIPTOR ppfd);
[DllImport("gdi32.dll")]
public static extern bool SwapBuffers(IntPtr hdc);
[StructLayout(LayoutKind.Sequential)]
public struct RECT { public int Left, Top, Right, Bottom; }
public const uint PM_REMOVE = 0x0001;
}