Browse Source

Add gWindowRatio and set window size on each step

tags/v0.6.0
Andrew Belt 7 years ago
parent
commit
7f1035730a
2 changed files with 19 additions and 9 deletions
  1. +6
    -0
      include/gui.hpp
  2. +13
    -9
      src/gui.cpp

+ 6
- 0
include/gui.hpp View File

@@ -17,8 +17,14 @@ namespace rack {
extern GLFWwindow *gWindow; extern GLFWwindow *gWindow;
extern NVGcontext *gVg; extern NVGcontext *gVg;
extern NVGcontext *gFramebufferVg; extern NVGcontext *gFramebufferVg;
/** The default font to use for GUI elements */
extern std::shared_ptr<Font> gGuiFont; extern std::shared_ptr<Font> gGuiFont;
/** The scaling ratio */
extern float gPixelRatio; 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 bool gAllowCursorLock;
extern int gGuiFrame; extern int gGuiFrame;
extern Vec gMousePos; extern Vec gMousePos;


+ 13
- 9
src/gui.cpp View File

@@ -33,7 +33,8 @@ GLFWwindow *gWindow = NULL;
NVGcontext *gVg = NULL; NVGcontext *gVg = NULL;
NVGcontext *gFramebufferVg = NULL; NVGcontext *gFramebufferVg = NULL;
std::shared_ptr<Font> gGuiFont; std::shared_ptr<Font> gGuiFont;
float gPixelRatio = 0.0;
float gPixelRatio = 1.0;
float gWindowRatio = 1.0;
bool gAllowCursorLock = true; bool gAllowCursorLock = true;
int gGuiFrame; int gGuiFrame;
Vec gMousePos; Vec gMousePos;
@@ -42,7 +43,6 @@ std::string lastWindowTitle;




void windowSizeCallback(GLFWwindow* window, int width, int height) { void windowSizeCallback(GLFWwindow* window, int width, int height) {
gScene->box.size = Vec(width, height);
} }


void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) { 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) { 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); Vec mouseRel = mousePos.minus(gMousePos);


#ifdef ARCH_MAC #ifdef ARCH_MAC
@@ -411,11 +411,6 @@ void guiDestroy() {


void guiRun() { void guiRun() {
assert(gWindow); assert(gWindow);
{
int width, height;
glfwGetWindowSize(gWindow, &width, &height);
windowSizeCallback(gWindow, width, height);
}
gGuiFrame = 0; gGuiFrame = 0;
while(!glfwWindowShouldClose(gWindow)) { while(!glfwWindowShouldClose(gWindow)) {
double startTime = glfwGetTime(); double startTime = glfwGetTime();
@@ -444,7 +439,7 @@ void guiRun() {
lastWindowTitle = windowTitle; lastWindowTitle = windowTitle;
} }


// Get framebuffer size
// Get desired scaling
float pixelRatio; float pixelRatio;
glfwGetWindowContentScale(gWindow, &pixelRatio, NULL); glfwGetWindowContentScale(gWindow, &pixelRatio, NULL);
pixelRatio = roundf(pixelRatio); pixelRatio = roundf(pixelRatio);
@@ -454,6 +449,15 @@ void guiRun() {
gPixelRatio = pixelRatio; 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 // Step scene
gScene->step(); gScene->step();




Loading…
Cancel
Save