2017-08-09 72 views
7

我试图使OF窗口按比例调整大小,保持窗口的widthheight之间的比例相同。例如,如果您创建了一个尺寸为400x300的窗口,并且如果将宽度拉伸至800,那么即使您只横向拉伸窗口,高度也会自动拉伸至600在windowResized()中调用SetWindowShape()在Ubuntu上冻结应用程序

无论如何,为了实现此功能,我需要在windowResized()听众内部使用ofSetWindowShape()

我可以在MacOS X上快速创建原型并且工作得非常好。

下面的代码:

ofApp.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? 

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); 
    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()侦听器,因此它进入无限循环。

(windowResized - > ofSetWindowShape - > windowResized - > ofSetWindowShape ....)

如何更改代码,以便它也可以在Ubuntu上运行没有问题? 任何意见或指导将不胜感激!

P.S:我也很感谢Linux用户是否可以确认应用程序冻结。

回答

0

你尝试:

ofSetupOpenGL(widthScaled, heightScaled, OF_WINDOW); 

显然ofSetWindowShape()只能应用在被称为::设置()... see OF tutorials here

+0

感谢您的回答,我尝试过,但它不”工作。 AFAIK,ofSetupOpenGL()应该只在main.cpp文件中使用,而SetWindowShape()可以在setup()以外的地方调用。 –

+0

啊。这通常是新框架的一个问题 - 文档比实施慢。 “我可以根据SetWindowShape(): ”引用我的回答:“或者,您可以在ofApp的设置 函数中设置窗口大小(和位置),该函数在开始时被调用,因为应用程序 启动” – ac1dh0n3ycl0ud