diff --git a/include/window.hpp b/include/window.hpp index 04e5194d..2346d598 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -60,8 +60,10 @@ struct Window { Window(); ~Window(); + math::Vec getPosition(); math::Vec getSize(); void setSize(math::Vec size); + void updatePosSizeSettings(); void run(); void step(); /** Takes a screenshot of the screen and saves it to a PNG file. */ diff --git a/src/settings.cpp b/src/settings.cpp index 0bb7bda0..4c8f920e 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -20,8 +20,8 @@ bool devMode = false; bool headless = false; 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; bool invertZoom = false; float cableOpacity = 0.5; diff --git a/src/window.cpp b/src/window.cpp index a2b3667f..63b3a167 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -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) { - // Do nothing. Window size is reset each frame anyway. + Window* window = (Window*) glfwGetWindowUserPointer(win); + window->updatePosSizeSettings(); } @@ -233,7 +240,7 @@ Window::Window() { #endif // Create window - win = glfwCreateWindow(800, 600, "", NULL, NULL); + win = glfwCreateWindow(1024, 768, "", NULL, NULL); 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."); exit(1); @@ -248,8 +255,10 @@ Window::Window() { glfwMaximizeWindow(win); } else { - glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y); glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y); + if (settings::windowPos.isFinite()) { + glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y); + } } glfwShowWindow(win); @@ -262,6 +271,7 @@ Window::Window() { internal->monitorRefreshRate = monitorMode->refreshRate; // Set window callbacks + glfwSetWindowMaximizeCallback(win, windowMaximizeCallback); glfwSetWindowSizeCallback(win, windowSizeCallback); glfwSetMouseButtonCallback(win, mouseButtonCallback); // Call this ourselves, but on every frame instead of only when the mouse moves @@ -314,19 +324,6 @@ 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) { widget::Widget::ContextDestroyEvent 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() { int 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() { internal->frame = 0; while (!glfwWindowShouldClose(win)) {