当我按下“ B”键时,我希望能够循环显示5种背景色。现在,我只能将颜色更改为红色。我不确定是否要采用正确的方法。任何帮助,将不胜感激。
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_B && action == GLFW_PRESS)
{
for (int i = 0; i < 5; i++)
{
switch (i)
{
case 0:
glClearColor(1.0, 0.0, 0.0, 1.0);
break;
case 1:
glClearColor(0.0, 1.0, 0.0, 1.0);
break;
case 2:
glClearColor(0.41, 0.41, 0.41, 1.0);
break;
case 3:
glClearColor(1.0, 1.0, 1.0, 1.0);
break;
case 4:
glClearColor(0.0, 0.0, 1.0, 1.0);
return;
}
}
}
}
答案 0 :(得分:0)
按B键时,循环开始,并且您看到最后一种颜色,需要将按键的测试放入循环中。像这样:
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
for (int i = 0; i < 5; i++)
{
Readkey(key);
if (key == GLFW_KEY_B && action == GLFW_PRESS)
{
switch (i)
{
case 0:
glClearColor(1.0, 0.0, 0.0, 1.0);
break;
case 1:
glClearColor(0.0, 1.0, 0.0, 1.0);
break;
case 2:
glClearColor(0.41, 0.41, 0.41, 1.0);
break;
case 3:
glClearColor(1.0, 1.0, 1.0, 1.0);
break;
case 4:
glClearColor(0.0, 0.0, 1.0, 1.0);
return;
}
}
}