gpt4 book ai didi

c - 为什么 GlutPostRedisplay 和 sleep 函数在此代码中不起作用?

转载 作者:行者123 更新时间:2023-11-30 17:14:11 24 4
gpt4 key购买 nike

我尝试在这个项目中实现usb和cpu之间的数据传输。数据传输显示为从计算机的一个组件移动到另一个组件的小矩形。

在下面的代码中,GlutPostRedisplay 不起作用。

另外,有人可以告诉我使用的 sleep() 是否正确,因为显示中调用的函数不能同步工作。 case() 永远不会被执行。 fisrtscreen()之后直接跳转到opened(),operate()不起作用。

这段代码有什么错误?

void operate()
{
URLTEXTX = 200;
URLTEXTY = 950;
displayString(READUSB,1);
//southbrigde to northbrigde
bottom(488.0,425.0,380.0);
back(488.0,188.0,380.0);
top(188.0,380.0,550.0);
//northbridge to cpu
front(230.0,350.0,595.0);
top(345.0,600.0,650.0);
//read from usb
back(700.0,625.0,465.0);
bottom(625.0,460.0,385.0);
back(620.0,525.0,390.0);
sleep(1);

URLTEXTX = 200;
URLTEXTY = 950;
displayString(WRITEUSB,1);
//cpu to northbridge
bottom(350.0,650.0,595.0);
back(350.0,230.0,600.0);
//northbridge to southbridge
bottom(188.0,550.0,380.0);
front(188.0,488.0,380.0);
top(483.0,380.0,425.0);
//write to usb
front(525.0,625.0,385.0);
top(625.0,385.0,460.0);
front(620.0,700.0,460.0);
sleep(1);

URLTEXTX = 200;
URLTEXTY = 950;
displayString(READDVD,1);
//read from dvd
back(600.0,560.0,810.0);
bottom(570.0,810.0,600.0);
back(560.0,525.0,610.0);
//ram to northbridge
back(450.0,230.0,580.0);
//northbridge to cpu
front(230.0,350.0,595.0);
top(345.0,600.0,650.0);
sleep(1);

URLTEXTX = 200;
URLTEXTY = 950;
displayString(WRITEDVD,1);
//cpu to northbridge
bottom(350.0,650.0,595.0);
back(350.0,230.0,600.0);
//northbridge to ram
front(230.0,450.0,580.0);
//write to dvd
front(525.0,570.0,600.0);
top(570.0,600.0,800.0);
front(560.0,600.0,800.0);
sleep(1);

URLTEXTX = 200;
URLTEXTY = 950;
displayString(READHD,1);
//read from hard disc
back(640.0,560.0,300.0);
top(560.0,300.0,530.0);
back(560.0,525.0,530.0);
//ram to northbridge
back(450.0,230.0,580.0);
//northbridge to cpu
front(230.0,350.0,595.0);
top(345.0,600.0,650.0);
sleep(1);

URLTEXTX = 200;
URLTEXTY = 950;
displayString(WRITEHD,1);
//cpu to northbridge
bottom(350.0,650.0,595.0);
back(350.0,230.0,600.0);
//northbridge to ram
front(230.0,450.0,580.0);
//write to hard disc
front(525.0,560.0,530.0);
bottom(560.0,530.0,300.0);
front(560.0,640.0,300.0);
sleep(1);
}
void front(GLfloat x1,GLfloat x2,GLfloat y1)//to move in forward direction
{
GLfloat i;
for(i=x1;i<=x2;i++)
{
drawbit(i,x1+5,y1,y1-5);
glutPostRedisplay();
}

}
void back(GLfloat x1,GLfloat x2,GLfloat y1)//to move in backward direction
{
GLfloat i;
for(i=x1;i>=x2;i--)
{
drawbit(i,i-5,y1,y1-5);
glutPostRedisplay();
}

}
void top(GLfloat x1,GLfloat y1,GLfloat y2)//to move in upward direction
{
GLfloat i;
for(i=y1;i<=y2;i++)
{
drawbit(x1,x1+5,i,i+5);
glutPostRedisplay();
}
}
void bottom(GLfloat x1,GLfloat y1,GLfloat y2)//to move in downward direction
{
GLfloat i;
for(i=y1;i>=y2;i--)
{
drawbit(x1,x1-5,i,i-5);
glutPostRedisplay();
}
}
void drawbit(GLfloat x1,GLfloat x2,GLfloat y1,GLfloat y2)
{
glBegin(GL_POLYGON);
glColor3f(1.0,1.0,1.0);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
glFlush();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
firstscreen(); //introduction to the project
sleep(3);
glClear(GL_COLOR_BUFFER_BIT);
casing(); //cpu case
sleep(2);
glClear(GL_COLOR_BUFFER_BIT);
opened(); //when cpu case is opened shows internal components
sleep(1);
operate(); //data transfer between various components
}

最佳答案

问题类似于:Pausing in OpenGL successively

glutPostRedisplay只需在 glut 中设置一个标志即可在下一个循环中调用显示回调。它实际上并没有绘制任何东西。

我怀疑你正在寻找的功能是 glutSwapBuffers 。如果没有双缓冲,几何图形将直接绘制到屏幕上(尽管对 GPU 的“绘制”命令已缓冲您想要的 glFlush )。这通常会导致闪烁,因为您看到的东西后来被更接近的几何体覆盖(由于深度缓冲区)。双缓冲通过渲染到离屏缓冲区然后立即显示结果来解决这个问题。确保GLUT_DOUBLE被传递到glutInit这样你就有了一个后台缓冲区。

当你sleep()时ing,您的应用程序将无法捕获和处理事件。假设您想关闭窗口。在 sleep 恢复之前,整个事情都会毫无 react 。 sleep 仍然很重要,这样您就不会占用 CPU。我会将这些概念分开。

  1. 使用 idle 进行循环/轮询直到您的延迟时间过去。然后调用glutPostRedisplay 。添加glutSwapBuffersdisplay如果您使用双缓冲。
  2. 写一个framerate limiter这会调用 sleep ,这样你就不会占用周期。

在设定的延迟后绘制不同事物的一个简单方法是编写一个小型状态机...

int state = STATE_INIT;
float timer = 0.0f;

void idle()
{
//insert framerate limiter here

//calculate time since last frame, perhaps using glutGet(GLUT_ELAPSED_TIME)
float deltaTime = ...

timer -= deltaTime;
if (timer < 0.0f)
{
switch (state)
{
case STATE_INIT:
state = STATE_DRAW_FIRST_THING;
timer = 123.0f;
...
}
glutPostRedisplay();
}
}

void display()
{
...
if (state == STATE_DRAW_FIRST_THING)
{
...
}
...
glutSwapBuffers();
}

随着您的应用程序变得越来越大,我怀疑这是否可维护,并且您会想要更强大的东西,但在那之前这是一个好的开始。

只需更改 void (*currentView)(void); idle中的回调函数会在 display 中节省一些硬编码。您可能想要创建一个面向对象的 state machine 。除了 bool 状态之外,您可能还想研究动画和关键帧插值。与其对所有内容进行硬编码,不如将几何图形、关键帧和状态序列存储在文件中,这是分离代码和数据的好方法。只要您使用库,XML 就非常适合用于此目的。

关于c - 为什么 GlutPostRedisplay 和 sleep 函数在此代码中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451058/

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