Skip to content

Commit cce7a1e

Browse files
committed
Add ClampRectangle method and update D3D11Screen logic
Introduced a new private method `ClampRectangle` in the `AIManager` class to ensure rectangles are clamped within screen bounds, preventing out-of-bounds issues. Updated the `D3D11Screen` method to use `ClampRectangle` for adjusting the `detectionBox` parameter. Added a `using` directive for `System.Windows.Threading` to support the use of `Dispatcher`.
1 parent f195e56 commit cce7a1e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Aimmy2/AILogic/AIManager.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Drawing.Imaging;
1313
using System.IO;
1414
using System.Windows;
15+
using System.Windows.Threading;
1516
using Visuality;
1617
using Vortice.Direct3D;
1718
using Vortice.Direct3D11;
@@ -672,7 +673,12 @@ private void ReinitializeD3D11()
672673
// Reinitialize Direct3D device and context
673674
InitializeDirect3D();
674675
}
675-
676+
private Rectangle ClampRectangle(Rectangle rect, int screenWidth, int screenHeight)
677+
{
678+
int x = Math.Max(0, Math.Min(rect.X, screenWidth - rect.Width));
679+
int y = Math.Max(0, Math.Min(rect.Y, screenHeight - rect.Height));
680+
return new Rectangle(x, y, rect.Width, rect.Height);
681+
}
676682
private Bitmap? D3D11Screen(Rectangle detectionBox)
677683
{
678684
try
@@ -681,7 +687,7 @@ private void ReinitializeD3D11()
681687
{
682688
throw new InvalidOperationException("Direct3D device or context is not initialized.");
683689
}
684-
690+
detectionBox = ClampRectangle(detectionBox, ScreenWidth, ScreenHeight);
685691
var result = _outputDuplication.AcquireNextFrame(500, out var frameInfo, out var desktopResource);
686692

687693
if (result != Result.Ok)

0 commit comments

Comments
 (0)