Setting a GLFW window as not resizable
Asked Answered
E

6

16

I have a GLFW3 window that I am trying to change from resizable to not resizable.

I tried changing the Window Hint after the window was created but this doesn't do anything as the hints only affect window to be created.

what I tried:

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE)

Is this possible? One way of doing it that I thought of was having a onResize function that changes the window size back to the current size after being set not resizable. This seems very hacky.

Erythrocytometer answered 30/11, 2014 at 5:34 Comment(3)
I put the command before the glfwCreateWindow(...) and it works. Now, I cannot resize the window anymore. (Version: GLFW 3.0.4 )Quincuncial
I think you can now do it with glfwSetWindowAttrib. The documentation here says: > GLFW_RESIZABLE indicates whether the specified window is resizable by the user. This can be set before creation with the GLFW_RESIZABLE window hint or after with glfwSetWindowAttrib.Incontestable
The second parameter should be GLFW_FALSE, not GL_FALSE!Orchidectomy
F
9

GLFW currently has no API for changing that state after the window is created.

When you want to use GLFW, I see two options:

  1. The workaround you already have.
  2. Create a new window when you switch that state.
  3. Use the GLFW native access to obtain the real window handles and implement the feature for each platform (you care about).

All variants don't seem too appealing to me. Option 2 is especially bad because of the way GL contexts are tied to windows in GLFW, it should be possible by using an extra (invisible) window and shared GL contexts, but it will be ugly.

Option 3 has the advantage that it should work flawlessly once it is implemented for all relevant platforms. As GLFW is open source, this enables also option 3b): implement this directly in GLFW and extend the API. You might even be able to get this integrated into the official GLFW version.

Fishery answered 30/11, 2014 at 14:2 Comment(0)
M
16

Your approach works as of GLFW 3.2.1-1 in Ubuntu 18.10:

main.cpp

#include <GLFW/glfw3.h>

int main(void) {
    GLFWwindow* window;
    if (!glfwInit())
        return -1;
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    window = glfwCreateWindow(640, 480, __FILE__, NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

Compile and run:

g++ -std=c++11 -Wall -Wextra -pedantic-errors -o main.out main.cpp -lglfw
./main.out

As I hover the borders of the created window, the cursor never changes to resize mode.

Mcmahan answered 27/3, 2016 at 10:6 Comment(2)
Downvoters, thanks for telling me why this answer is wrong so I can learn and improve info.Mcmahan
To be perfectly correct here: Parameters to glfwWindowHint should be GLFW_TRUE and GLFW_FALSE (not GL_*)Orchidectomy
F
9

GLFW currently has no API for changing that state after the window is created.

When you want to use GLFW, I see two options:

  1. The workaround you already have.
  2. Create a new window when you switch that state.
  3. Use the GLFW native access to obtain the real window handles and implement the feature for each platform (you care about).

All variants don't seem too appealing to me. Option 2 is especially bad because of the way GL contexts are tied to windows in GLFW, it should be possible by using an extra (invisible) window and shared GL contexts, but it will be ugly.

Option 3 has the advantage that it should work flawlessly once it is implemented for all relevant platforms. As GLFW is open source, this enables also option 3b): implement this directly in GLFW and extend the API. You might even be able to get this integrated into the official GLFW version.

Fishery answered 30/11, 2014 at 14:2 Comment(0)
D
3

You can change the GLFW_RESIZABLE attribute of an existing window with the following code:

bool enable;
glfwSetWindowAttrib(window, GLFW_RESIZABLE, enable);
Diapophysis answered 12/4, 2022 at 4:10 Comment(0)
C
1

This works but I highly recommend the other solutions, as this is only if you strictly need to be able to toggle it.

IntBuffer wid = BufferUtils.createIntBuffer(1);
IntBuffer hei = BufferUtils.createIntBuffer(1);

glfwGetWindowSize(window, wid, hei);

int windowWidth = wid.get();
int windowHeight = hei.get(); // I recommend making this public

while(!glfwWindowShouldClose(window)) {
    glfwSetWindowSize(window, windowWidth, windowHeight);
    // People can still maximize the window ... Comment if you have a solution :)
}
Costotomy answered 10/7, 2017 at 5:17 Comment(0)
N
1

My solution:

// before create:
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

// create window
// ...

// after create
void setResizable(arg) {
    if(arg)
        glfwSetWindowSizeLimits(window, 0, 0, 0xffff, 0xffff);
    else {
        int w, h;
        glfwGetWindowSize(window, &w, &h);
        glfwSetWindowSizeLimits(window, w, h, w, h);
    }
}
Nork answered 14/2, 2019 at 20:4 Comment(1)
The resize icon and maximize controls are still going to be active, so this is just a (quite dirty) hack.Mccrae
B
1

This worked to me with GLFW 3.3 but be carefull to put it after glfwInit()

int main( void )
{
// Initialise GLFW
if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        getchar();
        return -1;
    }
 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

 ...
}
Beleaguer answered 3/1, 2020 at 17:39 Comment(1)
Why the getChar() call here?Warrior

© 2022 - 2024 — McMap. All rights reserved.