gpt4 book ai didi

c++ - 如何让多个形状出现在同一个屏幕上?

转载 作者:行者123 更新时间:2023-12-04 07:43:07 25 4
gpt4 key购买 nike

我正在写一个程序,允许用户通过菜单选项绘制不同的形状,绘制后的形状需要在同一个屏幕上,但问题是在菜单中选择另一个选项绘制另一个形状后,之前的形状消失。我怎样才能解决这个问题?这是我的程序:

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

if (shape == 1)
{
draw_rectangle();
}

if (shape == 2)
{
draw_circle();
}

glFlush();
}

void menu(int choice)
{
switch (choice)
{
case 1:
shape = 1;
break;
case 2:
shape = 2;
break;
}
glutPostRedisplay();
}
glClear(GL_COLOR_BUFFER_BIT)仅在 display() .

最佳答案

您必须为每个形状( rectangle_shapecircle_shape )使用单独的 bool 状态变量,而不是一个指示形状的整数变量( shape ):

bool rectangle_shape = false;
bool circle_shape = false;

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

if (rectangle_shape)
{
draw_rectangle();
}

if (circle_shape)
{
draw_circle();
}

glFlush();
}

void menu(int choice)
{
switch (choice)
{
case 1:
rectangle_shape = !rectangle_shape;
break;
case 2:
circle_shape = !circle_shape;
break;
}
glutPostRedisplay();
}

关于c++ - 如何让多个形状出现在同一个屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67347085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com