Skip to content

Commit

Permalink
Fix screen capture bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TemariVirus committed Aug 27, 2023
1 parent cda2113 commit 42c9cbc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
31 changes: 17 additions & 14 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static partial class Program
private static int _resolution;

private static readonly IntPtr _windowHandle;
private static ReadonlyImage _lastCapture = null;
private static ReadonlyImage _lastSS = null;
private static int _windowWidth = -1;
private static int _windowHeight = -1;
private static int _targetLeft = -1;
Expand Down Expand Up @@ -108,6 +108,8 @@ static Program()

private static void Main()
{
// ActivateDirectXWorkaround();

Console.WriteLine(_jpMode ? "BOTTO wo kidou simasita." : "Bot started.");
Console.WriteLine(_jpMode ? "CTRL + C wo osite teisi." : "Press ctrl + C to stop.");
if (_hardwareAccelerated) { }
Expand All @@ -129,9 +131,6 @@ private static void Main()
Stopwatch waitSw = Stopwatch.StartNew();
while (true)
{
// Invalidate capture
_lastCapture = null;

if (!playing)
{
perfSw.Stop();
Expand All @@ -145,6 +144,8 @@ private static void Main()
continue;
}

InvalidateLastSS();

if (_targetLeft >= 0 && _targetTop >= 0)
{
PlayFishingGame(ref playing, in timeoutSw);
Expand Down Expand Up @@ -179,10 +180,7 @@ private static void Main()
? "20byou inai ni NO-TU ga mitukarimasen desita. kore ga tudukeru baai ha, HoloCure no kaizoudo wo tiisaku site kudasai. MINIGE-MU wo saikidou simasu."
: "No notes detected in 20 seconds. If this continues, try setting HoloCure to a smaller resolution. Restarting minigame."
);
if (!_hardwareAccelerated)
{
ActivateDirectXWorkaround();
}
ActivateDirectXWorkaround();

playing = false;
timeoutSw.Restart();
Expand Down Expand Up @@ -344,6 +342,11 @@ private static void FindTargetArea()

private static void ActivateDirectXWorkaround()
{
if (_hardwareAccelerated)
{
return;
}

_hardwareAccelerated = true;

_notes = new Note[]
Expand Down Expand Up @@ -384,13 +387,13 @@ private static void ActivateDirectXWorkaround()
Console.WriteLine(
_jpMode
? "3. ika no settei wo dekiru kagiri OFU ni site kudasai:"
+ "\n\ta. UXINDOU GE-MU no saitekika"
+ "\n\tb. kahen RIFURESSYU RE-TO"
+ "\n\tc. zidou HDR"
+ "\n a. UXINDOU GE-MU no saitekika"
+ "\n b. kahen RIFURESSYU RE-TO"
+ "\n c. zidou HDR"
: "3. Disable the following (if the setting(s) can be found):"
+ "\n\ta. Optimizations for windowed games"
+ "\n\tb. Variable refresh rate"
+ "\n\tc. Auto HDR"
+ "\n a. Optimizations for windowed games"
+ "\n b. Variable refresh rate"
+ "\n c. Auto HDR"
);
Console.WriteLine(
_jpMode
Expand Down
33 changes: 17 additions & 16 deletions WindowCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

internal static partial class Program
static partial class Program
{
[StructLayout(LayoutKind.Sequential)]
private struct Rect
Expand Down Expand Up @@ -84,10 +84,9 @@ private static int GetHolocureResolution(Rect rect)
: $"HoloCure window was resized to {width} x {height}"
);

// Invalidate target position and capture
// Invalidate target position
_targetLeft = -1;
_targetTop = -1;
_lastCapture = null;
}

width -= GRACE;
Expand Down Expand Up @@ -117,7 +116,7 @@ private static int GetHolocureResolution(Rect rect)
}

// Always behaves as if the window is 640 x 360
public static ReadonlyImage CaptureHolocureWindow(
private static ReadonlyImage CaptureHolocureWindow(
int left = 0,
int top = 0,
int width = -1,
Expand All @@ -141,20 +140,17 @@ public static ReadonlyImage CaptureHolocureWindow(
height = _windowHeight;
}

if (_hardwareAccelerated && _lastCapture != null)
{
return _lastCapture.Crop(left, top, width, height);
}

if (_hardwareAccelerated)
{
Bitmap bmp = new Bitmap(_windowWidth, _windowHeight, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);
ReadonlyImage capture = new ReadonlyImage(bmp).ShrinkBy(_resolution);
_lastCapture = capture;

return capture.Crop(left, top, width, height);
if (_lastSS == null)
{
Bitmap bmp = new Bitmap(_windowWidth, _windowHeight, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);
_lastSS = new ReadonlyImage(bmp);
}

return _lastSS.Crop(left, top, width, height).ShrinkBy(_resolution);
}
else
{
Expand All @@ -169,4 +165,9 @@ public static ReadonlyImage CaptureHolocureWindow(
return new ReadonlyImage(bmp).ShrinkBy(_resolution);
}
}

private static void InvalidateLastSS()
{
_lastSS = null;
}
}

0 comments on commit 42c9cbc

Please sign in to comment.