From 7f1035730ae71163859256a2dad51327d708581c Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 5 Dec 2017 23:04:54 -0500 Subject: [PATCH] Add gWindowRatio and set window size on each step --- include/gui.hpp | 6 ++++++ src/gui.cpp | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/gui.hpp b/include/gui.hpp index 9df01144..fd6c0e77 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -17,8 +17,14 @@ namespace rack { extern GLFWwindow *gWindow; extern NVGcontext *gVg; extern NVGcontext *gFramebufferVg; +/** The default font to use for GUI elements */ extern std::shared_ptr gGuiFont; +/** The scaling ratio */ extern float gPixelRatio; +/* The ratio between the framebuffer size and the window size reported by the OS. +This is not equal to gPixelRatio in general. +*/ +extern float gWindowRatio; extern bool gAllowCursorLock; extern int gGuiFrame; extern Vec gMousePos; diff --git a/src/gui.cpp b/src/gui.cpp index b74a6988..fdb58ad3 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -33,7 +33,8 @@ GLFWwindow *gWindow = NULL; NVGcontext *gVg = NULL; NVGcontext *gFramebufferVg = NULL; std::shared_ptr gGuiFont; -float gPixelRatio = 0.0; +float gPixelRatio = 1.0; +float gWindowRatio = 1.0; bool gAllowCursorLock = true; int gGuiFrame; Vec gMousePos; @@ -42,7 +43,6 @@ std::string lastWindowTitle; void windowSizeCallback(GLFWwindow* window, int width, int height) { - gScene->box.size = Vec(width, height); } void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) { @@ -145,7 +145,7 @@ void mouseButtonStickyCallback(GLFWwindow *window, int button, int action, int m } void cursorPosCallback(GLFWwindow* window, double xpos, double ypos) { - Vec mousePos = Vec(xpos, ypos).round(); + Vec mousePos = Vec(xpos, ypos).div(gPixelRatio / gWindowRatio).round(); Vec mouseRel = mousePos.minus(gMousePos); #ifdef ARCH_MAC @@ -411,11 +411,6 @@ void guiDestroy() { void guiRun() { assert(gWindow); - { - int width, height; - glfwGetWindowSize(gWindow, &width, &height); - windowSizeCallback(gWindow, width, height); - } gGuiFrame = 0; while(!glfwWindowShouldClose(gWindow)) { double startTime = glfwGetTime(); @@ -444,7 +439,7 @@ void guiRun() { lastWindowTitle = windowTitle; } - // Get framebuffer size + // Get desired scaling float pixelRatio; glfwGetWindowContentScale(gWindow, &pixelRatio, NULL); pixelRatio = roundf(pixelRatio); @@ -454,6 +449,15 @@ void guiRun() { gPixelRatio = pixelRatio; } + // Get framebuffer/window ratio + int width, height; + glfwGetFramebufferSize(gWindow, &width, &height); + int windowWidth, windowHeight; + glfwGetWindowSize(gWindow, &windowWidth, &windowHeight); + gWindowRatio = (float)width / windowWidth; + + gScene->box.size = Vec(width, height).div(gPixelRatio / gWindowRatio); + // Step scene gScene->step();