Browse Source

Correctly update window position and size when window is resized or

maximized.
tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
ca44ad96cc
3 changed files with 36 additions and 18 deletions
  1. +2
    -0
      include/window.hpp
  2. +2
    -2
      src/settings.cpp
  3. +32
    -16
      src/window.cpp

+ 2
- 0
include/window.hpp View File

@@ -60,8 +60,10 @@ struct Window {


Window(); Window();
~Window(); ~Window();
math::Vec getPosition();
math::Vec getSize(); math::Vec getSize();
void setSize(math::Vec size); void setSize(math::Vec size);
void updatePosSizeSettings();
void run(); void run();
void step(); void step();
/** Takes a screenshot of the screen and saves it to a PNG file. */ /** Takes a screenshot of the screen and saves it to a PNG file. */


+ 2
- 2
src/settings.cpp View File

@@ -20,8 +20,8 @@ bool devMode = false;
bool headless = false; bool headless = false;


std::string token; std::string token;
math::Vec windowSize;
math::Vec windowPos;
math::Vec windowSize = math::Vec(1024, 768);
math::Vec windowPos = math::Vec(NAN, NAN);
float zoom = 0.25; float zoom = 0.25;
bool invertZoom = false; bool invertZoom = false;
float cableOpacity = 0.5; float cableOpacity = 0.5;


+ 32
- 16
src/window.cpp View File

@@ -91,8 +91,15 @@ struct Window::Internal {
}; };




static void windowMaximizeCallback(GLFWwindow* win, int maximized) {
Window* window = (Window*) glfwGetWindowUserPointer(win);
window->updatePosSizeSettings();
}


static void windowSizeCallback(GLFWwindow* win, int width, int height) { static void windowSizeCallback(GLFWwindow* win, int width, int height) {
// Do nothing. Window size is reset each frame anyway.
Window* window = (Window*) glfwGetWindowUserPointer(win);
window->updatePosSizeSettings();
} }




@@ -233,7 +240,7 @@ Window::Window() {
#endif #endif


// Create window // Create window
win = glfwCreateWindow(800, 600, "", NULL, NULL);
win = glfwCreateWindow(1024, 768, "", NULL, NULL);
if (!win) { if (!win) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not open GLFW window. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed."); osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not open GLFW window. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed.");
exit(1); exit(1);
@@ -248,8 +255,10 @@ Window::Window() {
glfwMaximizeWindow(win); glfwMaximizeWindow(win);
} }
else { else {
glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y);
glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y); glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y);
if (settings::windowPos.isFinite()) {
glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y);
}
} }
glfwShowWindow(win); glfwShowWindow(win);


@@ -262,6 +271,7 @@ Window::Window() {
internal->monitorRefreshRate = monitorMode->refreshRate; internal->monitorRefreshRate = monitorMode->refreshRate;


// Set window callbacks // Set window callbacks
glfwSetWindowMaximizeCallback(win, windowMaximizeCallback);
glfwSetWindowSizeCallback(win, windowSizeCallback); glfwSetWindowSizeCallback(win, windowSizeCallback);
glfwSetMouseButtonCallback(win, mouseButtonCallback); glfwSetMouseButtonCallback(win, mouseButtonCallback);
// Call this ourselves, but on every frame instead of only when the mouse moves // Call this ourselves, but on every frame instead of only when the mouse moves
@@ -314,19 +324,6 @@ Window::Window() {




Window::~Window() { Window::~Window() {
if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED)) {
settings::windowSize = math::Vec();
settings::windowPos = math::Vec();
}
else {
int winWidth, winHeight;
glfwGetWindowSize(win, &winWidth, &winHeight);
int winX, winY;
glfwGetWindowPos(win, &winX, &winY);
settings::windowSize = math::Vec(winWidth, winHeight);
settings::windowPos = math::Vec(winX, winY);
}

if (APP->scene) { if (APP->scene) {
widget::Widget::ContextDestroyEvent e; widget::Widget::ContextDestroyEvent e;
APP->scene->onContextDestroy(e); APP->scene->onContextDestroy(e);
@@ -345,6 +342,13 @@ Window::~Window() {
} }




math::Vec Window::getPosition() {
int x, y;
glfwGetWindowPos(win, &x, &y);
return math::Vec(x, y);
}


math::Vec Window::getSize() { math::Vec Window::getSize() {
int width, height; int width, height;
glfwGetWindowSize(win, &width, &height); glfwGetWindowSize(win, &width, &height);
@@ -358,6 +362,18 @@ void Window::setSize(math::Vec size) {
} }




void Window::updatePosSizeSettings() {
if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED)) {
settings::windowSize = math::Vec();
settings::windowPos = math::Vec();
}
else {
settings::windowSize = getSize();
settings::windowPos = getPosition();
}
}


void Window::run() { void Window::run() {
internal->frame = 0; internal->frame = 0;
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {


Loading…
Cancel
Save