Calling ofSetWindowShape() in windowResized() freezes app on Ubuntu

Hi, I’m trying to make the OF window get resized proportionally maintaining the same ratio between width and height of the window.

For example, if you created a window with 400x300 dimensions and if you stretch the width to 800, then the height will automatically be stretched to 600 even though you only stretched the window horizontally.

Anyways, in order to implement this feature, I needed to use ofSetWindowShape() inside windowResized() listener.

I could quickly prototype this on Mac OSX and it worked very well.

Here’s the code.

in ofApp.h

enum ScaleDir { //window scaling directions
ScaleDir scaleDir;

int windowWidth, windowHeight; //original window dimensions
float widthScaled, heightScaled; //scaled window dimensions
float windowScale; //scale amount (1.0 = original)
bool bScaleDirFixed; //is direction fixed?

in ofApp.cpp

void ofApp::setup(){
    windowWidth = ofGetWidth();
    windowHeight = ofGetHeight();
    windowScale = 1.0f;
    widthScaled = windowWidth * windowScale;
    heightScaled = windowHeight * windowScale;

void ofApp::update(){

    if (bScaleDirFixed) {
        bScaleDirFixed = false;

void ofApp::draw(){

    ofSetColor(255, 0, 0);
    ofDrawEllipse(widthScaled/2, heightScaled/2, widthScaled, heightScaled); //the ellipse will be scaled as the window gets resized.

void ofApp::windowResized(int w, int h){

    if (!bScaleDirFixed) {
        int gapW = abs(widthScaled - w);
        int gapH = abs(heightScaled - h);
        if (gapW > gapH)
            scaleDir = SCALE_DIR_HORIZONTAL;
            scaleDir = SCALE_DIR_VERTICAL;
        bScaleDirFixed = true;
    float ratio;
    if (scaleDir == SCALE_DIR_HORIZONTAL) {
        ratio = static_cast<float>(windowHeight) / static_cast<float>(windowWidth);
        h = w * ratio;
        windowScale = static_cast<float>(w) / static_cast<float>(windowWidth);
    else if (scaleDir == SCALE_DIR_VERTICAL) {
        ratio = static_cast<float>(windowWidth) / static_cast<float>(windowHeight);
        w = h * ratio;
        windowScale = static_cast<float>(h) / static_cast<float>(windowHeight);
    widthScaled = windowWidth * windowScale;
    heightScaled = windowHeight * windowScale;
    ofSetWindowShape(widthScaled, heightScaled);

However, if I run the same code on Ubuntu, the app freezes as soon as I resize the window. It seems ofSetWindowShape() calls windowResized() listener and therefore it goes into an infinite loop.
(windowResized -> ofSetWindowShape -> windowResized -> ofSetWindowShape…)

How can I change the code so it can also work on Ubuntu without the problem?
Any advice or guidance would be greatly appreciated!

P.S: I would also appreciate if Linux users can confirm the app freezing.

I just started a bounty for this question on stackoverflow.

so that should cause an infinite recursive loop on all platforms…

the callback windowResized() will get called when the window is resized
either by user input or programmatically when you say
ofSetWindowShape(WIDTH, HEIGHT);

you should check to make sure the window size you are requesting is already
the shape and if it isn’t do what is in your windowResized function…else
move on…

Hi, @theDANtheMAN Thanks for your answer.

No, It works fine on Mac OSX but it’s problematic on Ubuntu.

I tried as you suggested by inserting this inside windowResized() at the beginning

if (w == widthScaled && h == heightScaled) 

But it didn’t fix the problem.

Have you tried the code yourself?

I don’t have a Linux machine to try it on.

I see, Thanks anyway.