我正在尝试编写一个使屏幕尺寸和正常尺寸更大的功能。更重要的是,当我将屏幕尺寸调整到正常尺寸时,它会重新放回到我完全调整尺寸之前的位置..我该怎么办?它...
这就是我所做的
//global Variables
int scr_pos_x = 100, scr_pos_y = 150;
//somewhere else in main method
glutInitWindowPosition(scr_pos_x, scr_pos_y);
....
glutKeyboardFunc(myKeyboard);
//myKeyBoardFunction
void myKeyboard(unsigned char key, int x, int y){
if(key == 'f'){
int scr_pos_x = glutGet((GLenum)GLUT_WINDOW_X);
int scr_pos_y = glutGet((GLenum)GLUT_WINDOW_Y);
cout << " while f press "<<scr_pos_x <<" "<<scr_pos_y << endl; // to check
glutFullScreen();
}else if(key=='x'){
cout << " while x press "<<scr_pos_x <<" "<<scr_pos_y << endl; // to check
glutPositionWindow(scr_pos_x, scr_pos_y);
glutReshapeWindow(640, 480);
}
}
当我按'f'时,我可以看到scr_pos_x和scr_pos_y被设置为适当的值,但当我按'x'时,这些值会以某种方式变为100和150.我错过了什么?
答案 0 :(得分:3)
if(key == 'f'){
int scr_pos_x = glutGet((GLenum)GLUT_WINDOW_X);
int scr_pos_y = glutGet((GLenum)GLUT_WINDOW_Y);
cout << " while f press "<<scr_pos_x <<" "<<scr_pos_y << endl; // to check
glutFullScreen();
}
在这里,您创建了两个名为scr_pos_x
和scr_pos_y
的全新变量,这些变量一直持续到if
块结束。它们会覆盖全局的那些。
将int
放在两个声明的开头,以便glutGet()
覆盖全局 scr_pos_x
和scr_pos_y
变量。< / p>