Browse Source

Use callbacks to set window pos, size, and maximized settings instead of

on Window destruction.
tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
750cbfea5a
4 changed files with 38 additions and 34 deletions
  1. +2
    -0
      include/settings.hpp
  2. +0
    -2
      include/window.hpp
  3. +7
    -0
      src/settings.cpp
  4. +29
    -32
      src/window.cpp

+ 2
- 0
include/settings.hpp View File

@@ -30,6 +30,8 @@ extern bool headless;

/** vcvrack.com user token */
extern std::string token;
/** Whether the window is maximized */
extern bool windowMaximized;
/** Size of window in pixels */
extern math::Vec windowSize;
/** Position in window in pixels */


+ 0
- 2
include/window.hpp View File

@@ -60,10 +60,8 @@ 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. */


+ 7
- 0
src/settings.cpp View File

@@ -20,6 +20,7 @@ bool devMode = false;
bool headless = false;

std::string token;
bool windowMaximized = false;
math::Vec windowSize = math::Vec(1024, 768);
math::Vec windowPos = math::Vec(NAN, NAN);
float zoom = 0.25;
@@ -90,6 +91,8 @@ json_t* toJson() {

json_object_set_new(rootJ, "token", json_string(token.c_str()));

json_object_set_new(rootJ, "windowMaximized", json_boolean(windowMaximized));

json_t* windowSizeJ = json_pack("[f, f]", windowSize.x, windowSize.y);
json_object_set_new(rootJ, "windowSize", windowSizeJ);

@@ -194,6 +197,10 @@ void fromJson(json_t* rootJ) {
if (tokenJ)
token = json_string_value(tokenJ);

json_t* windowMaximizedJ = json_object_get(rootJ, "windowMaximized");
if (windowMaximizedJ)
windowMaximized = json_boolean_value(windowMaximizedJ);

json_t* windowSizeJ = json_object_get(rootJ, "windowSize");
if (windowSizeJ) {
double x, y;


+ 29
- 32
src/window.cpp View File

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


static void windowMaximizeCallback(GLFWwindow* win, int maximized) {
Window* window = (Window*) glfwGetWindowUserPointer(win);
window->updatePosSizeSettings();
static void windowPosCallback(GLFWwindow* win, int x, int y) {
// Window* window = (Window*) glfwGetWindowUserPointer(win);
if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED))
return;
if (glfwGetWindowMonitor(win))
return;
settings::windowPos = math::Vec(x, y);
// DEBUG("windowPosCallback %d %d", x, y);
}


static void windowSizeCallback(GLFWwindow* win, int width, int height) {
Window* window = (Window*) glfwGetWindowUserPointer(win);
window->updatePosSizeSettings();
// Window* window = (Window*) glfwGetWindowUserPointer(win);
if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED))
return;
if (glfwGetWindowMonitor(win))
return;
settings::windowSize = math::Vec(width, height);
// DEBUG("windowSizeCallback %d %d", width, height);
}


static void windowMaximizeCallback(GLFWwindow* win, int maximized) {
// Window* window = (Window*) glfwGetWindowUserPointer(win);
settings::windowMaximized = maximized;
// DEBUG("windowMaximizeCallback %d", maximized);
}


@@ -251,14 +268,12 @@ Window::Window() {
INFO("Window content scale: %f", contentScale);

glfwSetWindowSizeLimits(win, minWindowSize.x, minWindowSize.y, GLFW_DONT_CARE, GLFW_DONT_CARE);
if (settings::windowSize.isZero()) {
glfwMaximizeWindow(win);
glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y);
if (settings::windowPos.isFinite()) {
glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y);
}
else {
glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y);
if (settings::windowPos.isFinite()) {
glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y);
}
if (settings::windowMaximized) {
glfwMaximizeWindow(win);
}
glfwShowWindow(win);

@@ -271,8 +286,9 @@ Window::Window() {
internal->monitorRefreshRate = monitorMode->refreshRate;

// Set window callbacks
glfwSetWindowMaximizeCallback(win, windowMaximizeCallback);
glfwSetWindowPosCallback(win, windowPosCallback);
glfwSetWindowSizeCallback(win, windowSizeCallback);
glfwSetWindowMaximizeCallback(win, windowMaximizeCallback);
glfwSetMouseButtonCallback(win, mouseButtonCallback);
// Call this ourselves, but on every frame instead of only when the mouse moves
// glfwSetCursorPosCallback(win, cursorPosCallback);
@@ -342,13 +358,6 @@ 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);
@@ -362,18 +371,6 @@ 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)) {


Loading…
Cancel
Save