Fix scaling on linux (I dread the windows version...)

This commit is contained in:
Phillip Stephens 2022-02-26 13:28:21 -08:00
parent 4b69cb449d
commit 457f63d311
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
2 changed files with 17 additions and 3 deletions

View File

@ -352,12 +352,20 @@ WindowSize get_window_size() noexcept {
int width, height, fb_w, fb_h;
SDL_GetWindowSize(g_window, &width, &height);
SDL_GL_GetDrawableSize(g_window, &fb_w, &fb_h);
float scale = fb_w / width;
#ifndef __APPLE__
if (SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(g_window), nullptr, &scale, nullptr) == 0) {
scale /= 96.f;
}
#endif
return {
.width = static_cast<uint32_t>(width),
.height = static_cast<uint32_t>(height),
.fb_width = static_cast<uint32_t>(fb_w),
.fb_height = static_cast<uint32_t>(fb_h),
.scale = static_cast<float>(fb_w) / static_cast<float>(width),
.scale = scale,
};
}

View File

@ -40,9 +40,15 @@ void process_event(const SDL_Event& event) noexcept {
auto newEvent = event;
if (newEvent.type == SDL_MOUSEMOTION) {
auto& io = ImGui::GetIO();
float mouseX = newEvent.motion.x;
float mouseY = newEvent.motion.y;
#ifdef __APPLE__
mouseX *= g_scale;
mouseY *= g_scale;
#endif
// Scale up mouse coordinates
io.AddMousePosEvent(static_cast<float>(newEvent.motion.x) * g_scale,
static_cast<float>(newEvent.motion.y) * g_scale);
io.AddMousePosEvent(mouseX, mouseY);
return;
}
ImGui_ImplSDL2_ProcessEvent(&newEvent);