Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 95f9eac

Browse files
committed
PR #16409 ( by @cyn0x8 )
1 parent 22759f6 commit 95f9eac

File tree

2 files changed

+120
-30
lines changed

2 files changed

+120
-30
lines changed

source/Main.hx

+4-30
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,11 @@ import backend.Highscore;
4545
@:cppInclude('./external/gamemode_client.h')
4646
@:cppFileCode('#define GAMEMODE_AUTO')
4747
#end
48-
#if windows
49-
@:buildXml('
50-
<target id="haxe">
51-
<lib name="wininet.lib" if="windows" />
52-
<lib name="dwmapi.lib" if="windows" />
53-
</target>
54-
')
55-
@:cppFileCode('
56-
#include <windows.h>
57-
#include <winuser.h>
58-
#pragma comment(lib, "Shell32.lib")
59-
extern "C" HRESULT WINAPI SetCurrentProcessExplicitAppUserModelID(PCWSTR AppID);
60-
')
61-
#end
48+
6249
// // // // // // // // //
6350
class Main extends Sprite
6451
{
65-
var game = {
52+
public static final game = {
6653
width: 1280, // WINDOW width
6754
height: 720, // WINDOW height
6855
initialState: TitleState, // initial game state
@@ -84,21 +71,8 @@ class Main extends Sprite
8471
{
8572
super();
8673

87-
#if windows
88-
// DPI Scaling fix for windows
89-
// this shouldn't be needed for other systems
90-
// Credit to YoshiCrafter29 for finding this function
91-
untyped __cpp__("SetProcessDPIAware();");
92-
93-
var display = lime.system.System.getDisplay(0);
94-
if (display != null) {
95-
var dpiScale:Float = display.dpi / 96;
96-
Application.current.window.width = Std.int(game.width * dpiScale);
97-
Application.current.window.height = Std.int(game.height * dpiScale);
98-
99-
Application.current.window.x = Std.int((Application.current.window.display.bounds.width - Application.current.window.width) / 2);
100-
Application.current.window.y = Std.int((Application.current.window.display.bounds.height - Application.current.window.height) / 2);
101-
}
74+
#if (cpp && windows)
75+
backend.Native.fixScaling();
10276
#end
10377

10478
// Credits to MAJigsaw77 (he's the og author for this code)

source/backend/Native.hx

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package backend;
2+
3+
import lime.app.Application;
4+
import lime.system.Display;
5+
import lime.system.System;
6+
7+
import flixel.util.FlxColor;
8+
9+
#if (cpp && windows)
10+
@:buildXml('
11+
<target id="haxe">
12+
<lib name="dwmapi.lib" if="windows"/>
13+
<lib name="gdi32.lib" if="windows"/>
14+
</target>
15+
')
16+
@:cppFileCode('
17+
#include <windows.h>
18+
#include <dwmapi.h>
19+
#include <winuser.h>
20+
#include <wingdi.h>
21+
22+
#define attributeDarkMode 20
23+
#define attributeDarkModeFallback 19
24+
25+
#define attributeCaptionColor 34
26+
#define attributeTextColor 35
27+
#define attributeBorderColor 36
28+
29+
struct HandleData {
30+
DWORD pid = 0;
31+
HWND handle = 0;
32+
};
33+
34+
BOOL CALLBACK findByPID(HWND handle, LPARAM lParam) {
35+
DWORD targetPID = ((HandleData*)lParam)->pid;
36+
DWORD curPID = 0;
37+
38+
GetWindowThreadProcessId(handle, &curPID);
39+
if (targetPID != curPID || GetWindow(handle, GW_OWNER) != (HWND)0 || !IsWindowVisible(handle)) {
40+
return TRUE;
41+
}
42+
43+
((HandleData*)lParam)->handle = handle;
44+
return FALSE;
45+
}
46+
47+
HWND curHandle = 0;
48+
void getHandle() {
49+
if (curHandle == (HWND)0) {
50+
HandleData data;
51+
data.pid = GetCurrentProcessId();
52+
EnumWindows(findByPID, (LPARAM)&data);
53+
curHandle = data.handle;
54+
}
55+
}
56+
')
57+
#end
58+
class Native
59+
{
60+
public static function __init__():Void
61+
{
62+
registerDPIAware();
63+
}
64+
65+
public static function registerDPIAware():Void
66+
{
67+
#if (cpp && windows)
68+
// DPI Scaling fix for windows
69+
// this shouldn't be needed for other systems
70+
// Credit to YoshiCrafter29 for finding this function
71+
untyped __cpp__('
72+
SetProcessDPIAware();
73+
#ifdef DPI_AWARENESS_CONTEXT
74+
SetProcessDpiAwarenessContext(
75+
#ifdef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
76+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
77+
#else
78+
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE
79+
#endif
80+
);
81+
#endif
82+
');
83+
#end
84+
}
85+
86+
private static var fixedScaling:Bool = false;
87+
public static function fixScaling():Void
88+
{
89+
if (fixedScaling) return;
90+
fixedScaling = true;
91+
92+
#if (cpp && windows)
93+
final display:Null<Display> = System.getDisplay(0);
94+
if (display != null)
95+
{
96+
final dpiScale:Float = display.dpi / 96;
97+
@:privateAccess Application.current.window.width = Std.int(Main.game.width * dpiScale);
98+
@:privateAccess Application.current.window.height = Std.int(Main.game.height * dpiScale);
99+
100+
Application.current.window.x = Std.int((Application.current.window.display.bounds.width - Application.current.window.width) / 2);
101+
Application.current.window.y = Std.int((Application.current.window.display.bounds.height - Application.current.window.height) / 2);
102+
}
103+
104+
untyped __cpp__('
105+
getHandle();
106+
if (curHandle != (HWND)0) {
107+
HDC curHDC = GetDC(curHandle);
108+
RECT curRect;
109+
GetClientRect(curHandle, &curRect);
110+
FillRect(curHDC, &curRect, (HBRUSH)GetStockObject(BLACK_BRUSH));
111+
ReleaseDC(curHandle, curHDC);
112+
}
113+
');
114+
#end
115+
}
116+
}

0 commit comments

Comments
 (0)