1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

SDL2: Support highdpi (#14703)

and handle DPI changes at runtime
This commit is contained in:
grorp 2024-06-16 17:49:42 +02:00 committed by GitHub
parent 7a64527db5
commit a9cca5e76c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 84 additions and 47 deletions

View file

@ -285,6 +285,11 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters &param) :
SDL_SetHint(SDL_HINT_TV_REMOTE_AS_JOYSTICK, "0");
#endif
#if SDL_VERSION_ATLEAST(2, 24, 0)
// highdpi support on Windows
SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, "1");
#endif
// Minetest has its own code to synthesize mouse events from touch events,
// so we prevent SDL from doing it.
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
@ -475,6 +480,7 @@ bool CIrrDeviceSDL::createWindow()
bool CIrrDeviceSDL::createWindowWithContext()
{
u32 SDL_Flags = 0;
SDL_Flags |= SDL_WINDOW_ALLOW_HIGHDPI;
SDL_Flags |= getFullscreenFlag(CreationParams.Fullscreen);
if (Resizable)
@ -589,13 +595,16 @@ bool CIrrDeviceSDL::createWindowWithContext()
return false;
}
// Update Width and Height to match the actual window size.
// In fullscreen mode, the window size specified in SIrrlichtCreationParameters
// is ignored, so we cannot rely on it.
int w = 0, h = 0;
SDL_GetWindowSize(Window, &w, &h);
Width = w;
Height = h;
updateSizeAndScale();
if (ScaleX != 1.0f || ScaleY != 1.0f) {
// The given window size is in pixels, not in screen coordinates.
// We can only do the conversion now since we didn't know the scale before.
SDL_SetWindowSize(Window, CreationParams.WindowSize.Width / ScaleX,
CreationParams.WindowSize.Height / ScaleY);
// Re-center, otherwise large, non-maximized windows go offscreen.
SDL_SetWindowPosition(Window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
updateSizeAndScale();
}
return true;
#endif // !_IRR_EMSCRIPTEN_PLATFORM_
@ -659,10 +668,10 @@ bool CIrrDeviceSDL::run()
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
MouseXRel = SDL_event.motion.xrel;
MouseYRel = SDL_event.motion.yrel;
MouseX = irrevent.MouseInput.X = SDL_event.motion.x * ScaleX;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y * ScaleY;
MouseXRel = SDL_event.motion.xrel * ScaleX;
MouseYRel = SDL_event.motion.yrel * ScaleY;
irrevent.MouseInput.ButtonStates = MouseButtonStates;
irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;
irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;
@ -694,8 +703,8 @@ bool CIrrDeviceSDL::run()
SDL_Keymod keymod = SDL_GetModState();
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.X = SDL_event.button.x;
irrevent.MouseInput.Y = SDL_event.button.y;
irrevent.MouseInput.X = SDL_event.button.x * ScaleX;
irrevent.MouseInput.Y = SDL_event.button.y * ScaleY;
irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;
irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;
@ -827,14 +836,25 @@ bool CIrrDeviceSDL::run()
case SDL_WINDOWEVENT:
switch (SDL_event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
if ((SDL_event.window.data1 != (int)Width) || (SDL_event.window.data2 != (int)Height)) {
Width = SDL_event.window.data1;
Height = SDL_event.window.data2;
case SDL_WINDOWEVENT_SIZE_CHANGED:
#if SDL_VERSION_ATLEAST(2, 0, 18)
case SDL_WINDOWEVENT_DISPLAY_CHANGED:
#endif
u32 old_w = Width, old_h = Height;
f32 old_scale_x = ScaleX, old_scale_y = ScaleY;
updateSizeAndScale();
if (old_w != Width || old_h != Height) {
if (VideoDriver)
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
}
if (old_scale_x != ScaleX || old_scale_y != ScaleY) {
irrevent.EventType = EET_APPLICATION_EVENT;
irrevent.ApplicationEvent.EventType = EAET_DPI_CHANGED;
postEventFromUser(irrevent);
}
break;
}
break;
case SDL_USEREVENT:
irrevent.EventType = irr::EET_USER_EVENT;
@ -1030,25 +1050,26 @@ bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> &joystickInfo)
return false;
}
void CIrrDeviceSDL::updateSizeAndScale()
{
int window_w, window_h;
SDL_GetWindowSize(Window, &window_w, &window_h);
int drawable_w, drawable_h;
SDL_GL_GetDrawableSize(Window, &drawable_w, &drawable_h);
ScaleX = (float)drawable_w / (float)window_w;
ScaleY = (float)drawable_h / (float)window_h;
Width = drawable_w;
Height = drawable_h;
}
//! Get the display density in dots per inch.
float CIrrDeviceSDL::getDisplayDensity() const
{
if (!Window)
return 0.0f;
int window_w;
int window_h;
SDL_GetWindowSize(Window, &window_w, &window_h);
int drawable_w;
int drawable_h;
SDL_GL_GetDrawableSize(Window, &drawable_w, &drawable_h);
// assume 96 dpi
float dpi_w = (float)drawable_w / (float)window_w * 96.0f;
float dpi_h = (float)drawable_h / (float)window_h * 96.0f;
return std::max(dpi_w, dpi_h);
return std::max(ScaleX * 96.0f, ScaleY * 96.0f);
}
void CIrrDeviceSDL::SwapWindow()