Skip to content

Commit 40df393

Browse files
authoredApr 11, 2020
Implement DPI scaling for Windows (#9586)
1 parent 5cc06e4 commit 40df393

File tree

5 files changed

+69
-25
lines changed

5 files changed

+69
-25
lines changed
 

‎misc/minetest.exe.manifest

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</security>
99
</trustInfo>
1010
<application xmlns="urn:schemas-microsoft-com:asm.v3">
11-
<windowsSettings>
12-
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
11+
<windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
12+
<dpiAware>true</dpiAware>
Has conversations. Original line has conversations.
1313
</windowsSettings>
1414
</application>
1515
</assembly>

‎misc/winresource.rc

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <windows.h>
2+
#include <winuser.h>
23
#include <commctrl.h>
34
#include <richedit.h>
5+
46
#ifndef USE_CMAKE_CONFIG_H
57
#define USE_CMAKE_CONFIG_H
68
#endif
@@ -13,6 +15,10 @@
1315
#define BUILDMODE "RUN_IN_PLACE=0"
1416
#endif
1517

18+
#ifdef __MINGW32__
19+
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "minetest.exe.manifest"
20+
#endif
21+
1622
LANGUAGE 0, SUBLANG_NEUTRAL
1723
130 ICON "minetest-icon.ico"
1824

‎src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ if(WIN32)
451451
-i${WINRESOURCE_FILE}
452452
-o ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o
453453
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
454-
DEPENDS ${WINRESOURCE_FILE})
454+
DEPENDS ${WINRESOURCE_FILE} ${MINETEST_EXE_MANIFEST_FILE})
455455
SET(extra_windows_SRCS ${CMAKE_CURRENT_BINARY_DIR}/winresource_rc.o)
456456
else(MINGW) # Probably MSVC
457457
set(extra_windows_SRCS ${WINRESOURCE_FILE} ${MINETEST_EXE_MANIFEST_FILE})

‎src/client/renderingengine.cpp

+60-17
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4545
#include <X11/Xlib.h>
4646
#include <X11/Xutil.h>
4747
#include <X11/Xatom.h>
48+
#endif
4849

50+
#ifdef _WIN32
51+
#include <windows.h>
52+
#include <winuser.h>
4953
#endif
5054

5155
#if ENABLE_GLES
@@ -318,6 +322,28 @@ void RenderingEngine::setupTopLevelXorgWindow(const std::string &name)
318322
#endif
319323
}
320324

325+
#ifdef _WIN32
326+
static bool getWindowHandle(irr::video::IVideoDriver *driver, HWND &hWnd)
327+
{
328+
const video::SExposedVideoData exposedData = driver->getExposedVideoData();
329+
330+
switch (driver->getDriverType()) {
331+
case video::EDT_DIRECT3D8:
332+
hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
333+
break;
334+
case video::EDT_DIRECT3D9:
335+
hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
336+
break;
337+
case video::EDT_OPENGL:
338+
hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
339+
break;
340+
default:
341+
return false;
342+
}
343+
344+
return true;
345+
}
346+
#endif
321347

322348
bool RenderingEngine::setWindowIcon()
323349
{
@@ -335,22 +361,9 @@ bool RenderingEngine::setWindowIcon()
335361
"-xorg-icon-128.png");
336362
#endif
337363
#elif defined(_WIN32)
338-
const video::SExposedVideoData exposedData = driver->getExposedVideoData();
339364
HWND hWnd; // Window handle
340-
341-
switch (driver->getDriverType()) {
342-
case video::EDT_DIRECT3D8:
343-
hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
344-
break;
345-
case video::EDT_DIRECT3D9:
346-
hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
347-
break;
348-
case video::EDT_OPENGL:
349-
hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
350-
break;
351-
default:
365+
if (!getWindowHandle(driver, hWnd))
352366
return false;
353-
}
354367

355368
// Load the ICON from resource file
356369
const HICON hicon = LoadIcon(GetModuleHandle(NULL),
@@ -632,7 +645,7 @@ const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYP
632645
}
633646

634647
#ifndef __ANDROID__
635-
#ifdef XORG_USED
648+
#if defined(XORG_USED)
636649

637650
static float calcDisplayDensity()
638651
{
@@ -667,12 +680,42 @@ float RenderingEngine::getDisplayDensity()
667680
return cached_display_density;
668681
}
669682

670-
#else // XORG_USED
683+
#elif defined(_WIN32)
684+
685+
686+
static float calcDisplayDensity(irr::video::IVideoDriver *driver)
687+
{
688+
HWND hWnd;
689+
if (getWindowHandle(driver, hWnd)) {
690+
HDC hdc = GetDC(hWnd);
691+
float dpi = GetDeviceCaps(hdc, LOGPIXELSX);
692+
ReleaseDC(hWnd, hdc);
693+
return dpi / 96.0f;
694+
}
695+
696+
/* return manually specified dpi */
697+
return g_settings->getFloat("screen_dpi") / 96.0f;
698+
}
699+
700+
float RenderingEngine::getDisplayDensity()
701+
{
702+
static bool cached = false;
703+
static float display_density;
704+
if (!cached) {
705+
display_density = calcDisplayDensity(get_video_driver());
706+
cached = true;
707+
}
708+
return display_density;
709+
}
710+
711+
#else
712+
671713
float RenderingEngine::getDisplayDensity()
672714
{
673715
return g_settings->getFloat("screen_dpi") / 96.0;
674716
}
675-
#endif // XORG_USED
717+
718+
#endif
676719

677720
v2u32 RenderingEngine::getDisplaySize()
678721
{

‎src/constants.h

-5
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,5 @@ with this program; if not, write to the Free Software Foundation, Inc.,
110110
GUI related things
111111
*/
112112

113-
// TODO: implement dpi-based scaling for windows and remove this hack
114-
#if defined(_WIN32)
115-
#define TTF_DEFAULT_FONT_SIZE (18)
116-
#else
117113
#define TTF_DEFAULT_FONT_SIZE (16)
118-
#endif
119114
#define DEFAULT_FONT_SIZE (10)

0 commit comments

Comments
 (0)
Please sign in to comment.