198 lines
7.2 KiB
C#
198 lines
7.2 KiB
C#
using SharpDX;
|
|
using SharpDX.Direct3D11;
|
|
using SharpDX.DXGI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Valve.VR;
|
|
|
|
namespace VDash.Overlay
|
|
{
|
|
public class WindowOverlay : DashOverlay,IDisposable
|
|
{
|
|
public CVRSystem VRSystem { get; private set; }
|
|
private Task m_EventPumpTask = null;
|
|
bool m_Created = false;
|
|
bool m_Running = true;
|
|
|
|
public WindowOverlay(string key, string name) : base(key, name)
|
|
{
|
|
}
|
|
|
|
public void Create()
|
|
{
|
|
ConsoleManager.Show();
|
|
ulong thumbHandle = OpenVR.k_ulOverlayHandleInvalid;
|
|
|
|
//Actually create the overlay
|
|
var overlay = OpenVR.Overlay;
|
|
EVROverlayError err = overlay.CreateDashboardOverlay(m_Key, m_Name, ref m_Handle, ref thumbHandle);
|
|
|
|
if (err != EVROverlayError.None)
|
|
{
|
|
throw new Exception($"Error creating overlay: {err}");
|
|
}
|
|
|
|
overlay.SetOverlayWidthInMeters(m_Handle, 2.5f);
|
|
overlay.SetOverlayInputMethod(m_Handle, VROverlayInputMethod.Mouse);
|
|
m_EventPumpTask = Task.Factory.StartNew(() => { EventPump(); });
|
|
m_Created = true;
|
|
}
|
|
|
|
private void EventPump()
|
|
{
|
|
#region Rendering setup
|
|
var overlay = OpenVR.Overlay;
|
|
|
|
//Skip rendering if overlay isn't visible
|
|
if ((m_Handle == OpenVR.k_ulOverlayHandleInvalid) || overlay == null || (!overlay.IsOverlayVisible(m_Handle)))
|
|
{
|
|
//return;
|
|
}
|
|
|
|
//Taken from here: https://stackoverflow.com/questions/6812068/c-sharp-which-is-the-fastest-way-to-take-a-screen-shot
|
|
|
|
var factory = new Factory1();
|
|
//Get first adapter
|
|
var adapter = factory.GetAdapter1(0);
|
|
//Get device from adapter
|
|
var device = new SharpDX.Direct3D11.Device(adapter);
|
|
|
|
#endregion
|
|
int frames = 0;
|
|
DateTime lastFps = DateTime.MinValue;
|
|
|
|
while (m_Running)
|
|
{
|
|
#region VR Events
|
|
VREvent_t vrEvent = new VREvent_t();
|
|
|
|
while (OpenVR.Overlay.PollNextOverlayEvent(m_Handle, ref vrEvent, (uint)Marshal.SizeOf(vrEvent)))
|
|
{
|
|
switch ((EVREventType)vrEvent.eventType)
|
|
{
|
|
case EVREventType.VREvent_MouseMove:
|
|
{
|
|
var x = vrEvent.data.mouse.x;
|
|
var y = vrEvent.data.mouse.y;
|
|
break;
|
|
}
|
|
|
|
case EVREventType.VREvent_MouseButtonDown:
|
|
{
|
|
var clickType = (EVRMouseButton)vrEvent.data.mouse.button;
|
|
break;
|
|
}
|
|
|
|
case EVREventType.VREvent_MouseButtonUp:
|
|
{
|
|
var clickType = (EVRMouseButton)vrEvent.data.mouse.button;
|
|
break;
|
|
}
|
|
case EVREventType.VREvent_OverlayShown:
|
|
{
|
|
Console.WriteLine("Overlay Shown!");
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Acutally Render
|
|
|
|
var proc = Process.GetProcessesByName("discord")[1];
|
|
var rect = new User32.Rect();
|
|
User32.GetWindowRect(proc.MainWindowHandle, ref rect);
|
|
|
|
int width = rect.right - rect.left;
|
|
int height = rect.bottom - rect.top;
|
|
|
|
using (var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
|
{
|
|
using (Graphics graphics = Graphics.FromImage(bmp))
|
|
{
|
|
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
|
|
|
|
var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
// Create Staging texture CPU-accessible
|
|
var textureDesc = new Texture2DDescription
|
|
{
|
|
CpuAccessFlags = CpuAccessFlags.Read,
|
|
BindFlags = BindFlags.None,
|
|
Format = Format.B8G8R8A8_UNorm,
|
|
Width = width,
|
|
Height = height,
|
|
OptionFlags = ResourceOptionFlags.None,
|
|
MipLevels = 1,
|
|
ArraySize = 1,
|
|
SampleDescription = { Count = 1, Quality = 0 },
|
|
Usage = ResourceUsage.Staging
|
|
};
|
|
|
|
using (var screenTexture = new Texture2D(device, textureDesc, new SharpDX.DataRectangle(data.Scan0, data.Stride)))
|
|
//using (var screenTexture = new Texture2D(device, textureDesc))
|
|
{
|
|
var texture = new Texture_t();
|
|
texture.handle = screenTexture.NativePointer;
|
|
texture.eType = ETextureType.DirectX;
|
|
texture.eColorSpace = EColorSpace.Auto;
|
|
EVROverlayError result = overlay.SetOverlayTexture(m_Handle, ref texture);
|
|
if (result != EVROverlayError.None)
|
|
{
|
|
Console.WriteLine($"Error rendering: {result}");
|
|
}
|
|
|
|
frames++;
|
|
|
|
bmp.UnlockBits(data);
|
|
}
|
|
//Dispose not freeing screenTexture: https://gamedev.stackexchange.com/questions/93311/freeing-texture-memory-in-sharpdx-what-am-i-missing
|
|
device.ImmediateContext.ClearState();
|
|
device.ImmediateContext.Flush();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
Thread.Sleep(10);
|
|
|
|
if (DateTime.Now.Subtract(lastFps).TotalMilliseconds > 1000)
|
|
{
|
|
Console.WriteLine($"{frames} fps.");
|
|
frames = 0;
|
|
lastFps = DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
|
|
private class User32
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct Rect
|
|
{
|
|
public int left;
|
|
public int top;
|
|
public int right;
|
|
public int bottom;
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
//FIXME: Actually implement this...
|
|
}
|
|
}
|
|
}
|