如何从main到void获取信息

How to get info from main to void?

本文关键字:void 获取 信息 main      更新时间:2023-10-16

我的任务是使用GLUT要求用户输入坐标并显示矩形。然而,我似乎无法获得从"int main"到"void display"的坐标。

这是我到目前为止的代码:

#include<iostream>
#include<gl/glut.h>
using namespace std;
void display(float yaxis, float xaxis)
{
    glClearColor(1, 1, 1, 1);
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin (GL_QUADS);
    glColor3f(0, 0, 0);
    glVertex2f(yaxis, -xaxis);
    glVertex2f(yaxis, xaxis);
    glVertex2f(-yaxis, xaxis);
    glVertex2f(-yaxis, -xaxis);
    glEnd();
    glFlush();
}
int main(int argc, char** argv)
{
    float xaxis;
    float yaxis;
    cout << "Please enter the co-ordinates for the x axis and press enter.";
    cin >> xaxis;
    cout << "You entered: " << xaxis
            << ".n Please enter the co-ordinates for the y axis and press enter.";
    cin >> yaxis;
    cout << "You entered: " << yaxis << ".n Here is your rectangle.";
    glutInit(&argc, argv);
    glutInitWindowSize(640, 500);
    glutInitWindowPosition(100, 10);
    glutCreateWindow("Triangle");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

glutDisplayFunc函数具有以下声明:

void glutDisplayFunc(void (*func)(void));

因此,您不能在实现display函数时使用它。下面是一个快速示例,可以修复您的错误:

#include<iostream>
#include<gl/glut.h>
using namespace std;
static float yaxis;
static float xaxis;
void display()
{
    glClearColor(1, 1, 1, 1);
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin (GL_QUADS);
    glColor3f(0, 0, 0);
    glVertex2f(yaxis, -xaxis);
    glVertex2f(yaxis, xaxis);
    glVertex2f(-yaxis, xaxis);
    glVertex2f(-yaxis, -xaxis);
    glEnd();
    glFlush();
}
int main(int argc, char** argv)
{
    cout << "Please enter the co-ordinates for the x axis and press enter.";
    cin >> xaxis;
    cout << "You entered: " << xaxis
            << ".n Please enter the co-ordinates for the y axis and press enter.";
    cin >> yaxis;
    cout << "You entered: " << yaxis << ".n Here is your rectangle.";
    glutInit(&argc, argv);
    glutInitWindowSize(640, 500);
    glutInitWindowPosition(100, 10);
    glutCreateWindow("Rectangle");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}