我试图使OF窗口按比例调整大小,以保持窗口的widthheight之间的比率相同。

例如,如果您创建一个具有400x300尺寸的窗口,并且将宽度拉伸(stretch)到800,那么即使您仅水平拉伸(stretch)该窗口,其高度也将自动拉伸(stretch)到600

无论如何,为了实现此功能,我需要在ofSetWindowShape()侦听器中使用windowResized()

我可以很快在MacOS X上对此进行原型(prototype)制作,并且效果很好。

这是代码:

App.h的

enum ScaleDir { //window scaling directions

    SCALE_DIR_HORIZONTAL,
    SCALE_DIR_VERTICAL,
};
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?

App.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);
    ofSetCircleResolution(50);
    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;
        else
            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);
}

但是,如果我在Ubuntu上运行相同的代码,则在调整窗口大小后,该应用程序将冻结。似乎ofSetWindowShape()调用windowResized()侦听器,因此进入了无限循环。



如何更改代码,使其也可以在Ubuntu上正常工作?
任何建议或指导将不胜感激!

附注:如果Linux用户可以确认应用冻结,我也将不胜感激。

最佳答案

你试过了吗:

ofSetupOpenGL(widthScaled, heightScaled, OF_WINDOW);

显然,应该仅在App::Setup()期间调用ofSetWindowShape()...
see OF tutorials here

08-25 08:23