Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into HEAD
  • Loading branch information
Nivaturimika committed Jun 1, 2024
2 parents 3b39e6d + 623623f commit a7802c8
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 19 deletions.
23 changes: 22 additions & 1 deletion src/entry_point_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Windows.h>
#include <shellapi.h>
#include <shellscalingapi.h>
#include "Objbase.h"
#include "window.hpp"

Expand Down Expand Up @@ -114,7 +115,27 @@ int WINAPI wWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPWSTR
signal(SIGABRT, signal_abort_handler);
}

SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// Workaround for old machines
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pSetProcessDpiAwarenessContext = (decltype(&SetProcessDpiAwarenessContext))GetProcAddress(hUser32dll, "SetProcessDpiAwarenessContext");
if(pSetProcessDpiAwarenessContext != NULL) {
pSetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
} else {
// windows 8.1 (not present on windows 8 and only available on desktop apps)
if(HINSTANCE hShcoredll = LoadLibrary(L"Shcore.dll"); hShcoredll) {
auto pSetProcessDpiAwareness = (decltype(&SetProcessDpiAwareness))GetProcAddress(hShcoredll, "SetProcessDpiAwareness");
if(pSetProcessDpiAwareness != NULL) {
pSetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
} else {
SetProcessDPIAware(); //vista+
}
FreeLibrary(hShcoredll);
} else {
SetProcessDPIAware(); //vista+
}
}
FreeLibrary(hUser32dll);
}

if(SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
// do everything here: create a window, read messages
Expand Down
49 changes: 46 additions & 3 deletions src/launcher/launcher_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#endif
#include <Windowsx.h>
#include <shellapi.h>
#include <shellscalingapi.h>
#include "Objbase.h"
#ifndef GLEW_STATIC
#define GLEW_STATIC
Expand Down Expand Up @@ -1681,6 +1682,21 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
}
}
return 0;
case WM_NCCREATE:
{
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pSetProcessDpiAwarenessContext = (decltype(&SetProcessDpiAwarenessContext))GetProcAddress(hUser32dll, "SetProcessDpiAwarenessContext");
if(pSetProcessDpiAwarenessContext == NULL) {
// not present, so have to call this
auto pEnableNonClientDpiScaling = (decltype(&EnableNonClientDpiScaling))GetProcAddress(hUser32dll, "EnableNonClientDpiScaling");
if(pEnableNonClientDpiScaling != NULL) {
pEnableNonClientDpiScaling(hwnd); //windows 10
}
}
FreeLibrary(hUser32dll);
}
break;
}
case WM_CHAR:
{
if(GetKeyState(VK_CONTROL) & 0x8000) {
Expand Down Expand Up @@ -1827,7 +1843,27 @@ int WINAPI wWinMain(
signal(SIGABRT, signal_abort_handler);
}

SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// Workaround for old machines
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pSetProcessDpiAwarenessContext = (decltype(&SetProcessDpiAwarenessContext))GetProcAddress(hUser32dll, "SetProcessDpiAwarenessContext");
if(pSetProcessDpiAwarenessContext != NULL) {
pSetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
} else {
// windows 8.1 (not present on windows 8 and only available on desktop apps)
if(HINSTANCE hShcoredll = LoadLibrary(L"Shcore.dll"); hShcoredll) {
auto pSetProcessDpiAwareness = (decltype(&SetProcessDpiAwareness))GetProcAddress(hShcoredll, "SetProcessDpiAwareness");
if(pSetProcessDpiAwareness != NULL) {
pSetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
} else {
SetProcessDPIAware(); //vista+
}
FreeLibrary(hShcoredll);
} else {
SetProcessDPIAware(); //vista+
}
}
FreeLibrary(hUser32dll);
}

if(!SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
return 0;
Expand Down Expand Up @@ -1883,8 +1919,15 @@ int WINAPI wWinMain(
);

if(launcher::m_hwnd) {

launcher::dpi = float(GetDpiForWindow((HWND)(launcher::m_hwnd)));
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pGetDpiForWindow = (decltype(&GetDpiForWindow))GetProcAddress(hUser32dll, "GetDpiForWindow");
if(pGetDpiForWindow != NULL) {
launcher::dpi = float(pGetDpiForWindow((HWND)(launcher::m_hwnd)));
} else {
launcher::dpi = 96.0f; //100%, default
}
FreeLibrary(hUser32dll);
}

auto monitor_handle = MonitorFromWindow((HWND)(launcher::m_hwnd), MONITOR_DEFAULTTOPRIMARY);
MONITORINFO mi;
Expand Down
95 changes: 80 additions & 15 deletions src/window/window_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ void set_borderless_full_screen(sys::state& game_state, bool fullscreen) {
WS_CLIPCHILDREN | WS_CLIPSIBLINGS;

RECT rectangle = {left, top, left + game_state.win_ptr->creation_x_size, top + game_state.win_ptr->creation_y_size};
AdjustWindowRectExForDpi(&rectangle, win32Style, false, 0, GetDpiForWindow(game_state.win_ptr->hwnd));
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pAdjustWindowRectExForDpi = (decltype(&AdjustWindowRectExForDpi))GetProcAddress(hUser32dll, "AdjustWindowRectExForDpi");
if(pAdjustWindowRectExForDpi != NULL) {
auto pGetDpiForWindow = (decltype(&GetDpiForWindow))GetProcAddress(hUser32dll, "GetDpiForWindow");
pAdjustWindowRectExForDpi(&rectangle, win32Style, false, 0, pGetDpiForWindow(game_state.win_ptr->hwnd));
} else {
AdjustWindowRectEx(&rectangle, win32Style, false, 0); //w2000+
}
FreeLibrary(hUser32dll);
}
int32_t final_width = rectangle.right - rectangle.left;
int32_t final_height = rectangle.bottom - rectangle.top;

Expand All @@ -72,7 +81,16 @@ void set_borderless_full_screen(sys::state& game_state, bool fullscreen) {
DWORD win32Style = WS_VISIBLE | WS_BORDER | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;

RECT rectangle = mi.rcMonitor;
AdjustWindowRectExForDpi(&rectangle, win32Style, false, WS_EX_TOPMOST, GetDpiForWindow(game_state.win_ptr->hwnd));
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pAdjustWindowRectExForDpi = (decltype(&AdjustWindowRectExForDpi))GetProcAddress(hUser32dll, "AdjustWindowRectExForDpi");
if(pAdjustWindowRectExForDpi != NULL) {
auto pGetDpiForWindow = (decltype(&GetDpiForWindow))GetProcAddress(hUser32dll, "GetDpiForWindow");
pAdjustWindowRectExForDpi(&rectangle, win32Style, false, WS_EX_TOPMOST, pGetDpiForWindow(game_state.win_ptr->hwnd));
} else {
AdjustWindowRectEx(&rectangle, win32Style, false, 0); //w2000+
}
FreeLibrary(hUser32dll);
}
int32_t win_width = (rectangle.right - rectangle.left);
int32_t win_height = (rectangle.bottom - rectangle.top);

Expand Down Expand Up @@ -164,7 +182,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
sound::pause_all(*state);
}
return 0;
case WM_LBUTTONDOWN: {
case WM_LBUTTONDOWN:
{
SetCapture(hwnd);
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
Expand All @@ -174,7 +193,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
state->win_ptr->left_mouse_down = true;
return 0;
}
case WM_LBUTTONUP: {
case WM_LBUTTONUP:
{
ReleaseCapture();
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
Expand All @@ -184,7 +204,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
state->win_ptr->left_mouse_down = false;
return 0;
}
case WM_MOUSEMOVE: {
case WM_MOUSEMOVE:
{
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
state->on_mouse_move(x, y, get_current_modifiers());
Expand All @@ -197,39 +218,44 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

return 0;
}
case WM_RBUTTONDOWN: {
case WM_RBUTTONDOWN:
{
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
state->on_rbutton_down(x, y, get_current_modifiers());
state->mouse_x_position = x;
state->mouse_y_position = y;
return 0;
}
case WM_RBUTTONUP: {
case WM_RBUTTONUP:
{
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
state->on_rbutton_up(x, y, get_current_modifiers());
state->mouse_x_position = x;
state->mouse_y_position = y;
return 0;
}
case WM_MBUTTONDOWN: {
case WM_MBUTTONDOWN:
{
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
state->on_mbutton_down(x, y, get_current_modifiers());
state->mouse_x_position = x;
state->mouse_y_position = y;
return 0;
}
case WM_MBUTTONUP: {
case WM_MBUTTONUP:
{
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
state->on_mbutton_up(x, y, get_current_modifiers());
state->mouse_x_position = x;
state->mouse_y_position = y;
return 0;
}
case WM_SIZE: {
case WM_SIZE:
{
window::window_state t = window::window_state::normal;

if(wParam == SIZE_MAXIMIZED) {
Expand All @@ -253,7 +279,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

return 0;
}
case WM_MOUSEWHEEL: {
case WM_MOUSEWHEEL:
{
state->on_mouse_wheel(state->mouse_x_position, state->mouse_y_position, get_current_modifiers(), (float)(GET_WHEEL_DELTA_WPARAM(wParam)) / 120.0f);
return 0;
}
Expand Down Expand Up @@ -281,7 +308,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_KEYUP:
state->on_key_up(sys::virtual_key(wParam), get_current_modifiers());
return 0;
case WM_CHAR: {
case WM_CHAR:
{
if(state->ui_state.edit_target) {
char turned_into = process_utf16_to_win1250(wchar_t(wParam));
if(turned_into)
Expand All @@ -291,7 +319,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
}

case WM_PAINT:
case WM_DISPLAYCHANGE: {
case WM_DISPLAYCHANGE:
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
Expand All @@ -305,10 +334,28 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
// this is the message that tells us there is a DirectShow event
sound::update_music_track(*state);
break;
case WM_NCCREATE:
{
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pSetProcessDpiAwarenessContext = (decltype(&SetProcessDpiAwarenessContext))GetProcAddress(hUser32dll, "SetProcessDpiAwarenessContext");
if(pSetProcessDpiAwarenessContext == NULL) {
// not present, so have to call this
auto pEnableNonClientDpiScaling = (decltype(&EnableNonClientDpiScaling))GetProcAddress(hUser32dll, "EnableNonClientDpiScaling");
if(pEnableNonClientDpiScaling != NULL) {
pEnableNonClientDpiScaling(hwnd); //windows 10
}
FreeLibrary(hUser32dll);
}
}
break;
}
case WM_GETMINMAXINFO:
{
LPMINMAXINFO info = (LPMINMAXINFO)lParam;
info->ptMinTrackSize.x = 640;
info->ptMinTrackSize.y = 400;
break;
}
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
Expand Down Expand Up @@ -361,7 +408,16 @@ void create_window(sys::state& game_state, creation_parameters const& params) {
int top = (mi.rcWork.bottom - mi.rcWork.top) / 2 - game_state.win_ptr->creation_y_size / 2;

RECT rectangle = {left, top, left + game_state.win_ptr->creation_x_size, top + game_state.win_ptr->creation_y_size};
AdjustWindowRectExForDpi(&rectangle, win32Style, false, 0, GetDpiForWindow(game_state.win_ptr->hwnd));
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pAdjustWindowRectExForDpi = (decltype(&AdjustWindowRectExForDpi))GetProcAddress(hUser32dll, "AdjustWindowRectExForDpi");
if(pAdjustWindowRectExForDpi != NULL) {
auto pGetDpiForWindow = (decltype(&GetDpiForWindow))GetProcAddress(hUser32dll, "GetDpiForWindow");
pAdjustWindowRectExForDpi(&rectangle, win32Style, false, 0, pGetDpiForWindow(game_state.win_ptr->hwnd));
} else {
AdjustWindowRectEx(&rectangle, win32Style, false, 0); //w2000+
}
FreeLibrary(hUser32dll);
}
int32_t final_width = rectangle.right - rectangle.left;
int32_t final_height = rectangle.bottom - rectangle.top;

Expand All @@ -384,7 +440,16 @@ void create_window(sys::state& game_state, creation_parameters const& params) {
GetMonitorInfoW(monitor_handle, &mi);

RECT rectangle = mi.rcMonitor;
AdjustWindowRectExForDpi(&rectangle, win32Style, false, WS_EX_TOPMOST, GetDpiForWindow(game_state.win_ptr->hwnd));
if(HINSTANCE hUser32dll = LoadLibrary(L"User32.dll"); hUser32dll) {
auto pAdjustWindowRectExForDpi = (decltype(&AdjustWindowRectExForDpi))GetProcAddress(hUser32dll, "AdjustWindowRectExForDpi");
if(pAdjustWindowRectExForDpi != NULL) {
auto pGetDpiForWindow = (decltype(&GetDpiForWindow))GetProcAddress(hUser32dll, "GetDpiForWindow");
pAdjustWindowRectExForDpi(&rectangle, win32Style, false, WS_EX_TOPMOST, pGetDpiForWindow(game_state.win_ptr->hwnd));
} else {
AdjustWindowRectEx(&rectangle, win32Style, false, 0); //w2000+
}
FreeLibrary(hUser32dll);
}
int32_t win_width = (rectangle.right - rectangle.left);
int32_t win_height = (rectangle.bottom - rectangle.top);

Expand Down

0 comments on commit a7802c8

Please sign in to comment.